This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
generated from creasico/laravel-package
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from creasico/feat-user-devices
feat: initialize `user_devices` resources
- Loading branch information
Showing
8 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Creasi\Base\Contracts; | ||
|
||
/** | ||
* @property-read \Illuminate\Database\Eloquent\Collection<int, \Creasi\Base\Models\UserDevice> $devices | ||
* | ||
* @mixin \Illuminate\Database\Eloquent\Model | ||
*/ | ||
interface HasDevices | ||
{ | ||
/** | ||
* @return \Illuminate\Database\Eloquent\Relations\HasMany | ||
*/ | ||
public function devices(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Creasi\Base\Events; | ||
|
||
use Creasi\Base\Models\UserDevice; | ||
|
||
class UserDeviceRegistered | ||
{ | ||
public function __construct( | ||
public UserDevice $device | ||
) { | ||
// . | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Creasi\Base\Listeners; | ||
|
||
use Creasi\Base\Events\UserDeviceRegistered; | ||
use Illuminate\Auth\Events\Authenticated; | ||
use Illuminate\Http\Request; | ||
|
||
class RegisterUserDevice | ||
{ | ||
public function __construct( | ||
private Request $request | ||
) { | ||
// . | ||
} | ||
|
||
public function handle(Authenticated $event) | ||
{ | ||
if (! $this->request->filled('device')) { | ||
return; | ||
} | ||
|
||
/** @var \Creasi\Base\Contracts\HasDevices */ | ||
$user = $event->user; | ||
|
||
$device = $user->devices()->firstOrCreate([ | ||
'device' => $this->request->input('device'), | ||
]); | ||
|
||
\event(new UserDeviceRegistered($device)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Creasi\Base\Models\Concerns; | ||
|
||
use Creasi\Base\Models\UserDevice; | ||
|
||
/** | ||
* @mixin \Illuminate\Foundation\Auth\User | ||
*/ | ||
trait WithDevices | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function devices() | ||
{ | ||
return $this->hasMany(UserDevice::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Creasi\Base\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model as EloquentModel; | ||
|
||
/** | ||
* @property string $token | ||
* @property-read \App\Models\User $user | ||
*/ | ||
class UserDevice extends EloquentModel | ||
{ | ||
protected $fillable = [ | ||
'token', | ||
]; | ||
|
||
public $timestamps = false; | ||
|
||
/** | ||
* @return \Illuminate\Database\Eloquent\Relations\MorphTo|Personnel | ||
*/ | ||
public function user() | ||
{ | ||
return $this->belongsTo('\App\Models\User'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters