Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #76 from creasico/feat-user-devices
Browse files Browse the repository at this point in the history
feat: initialize `user_devices` resources
  • Loading branch information
feryardiant authored Dec 25, 2023
2 parents 46a326c + bd30ac9 commit 55b2fe9
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
*/
public function up(): void
{
Schema::create('user_devices', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->string('token');
});

Schema::create('profiles', function (Blueprint $table) {
$table->id();
$table->nullableMorphs('identity');
Expand Down
16 changes: 16 additions & 0 deletions src/Contracts/HasDevices.php
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();
}
14 changes: 14 additions & 0 deletions src/Events/UserDeviceRegistered.php
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
) {
// .
}
}
32 changes: 32 additions & 0 deletions src/Listeners/RegisterUserDevice.php
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));
}
}
19 changes: 19 additions & 0 deletions src/Models/Concerns/WithDevices.php
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);
}
}
26 changes: 26 additions & 0 deletions src/Models/UserDevice.php
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');
}
}
6 changes: 6 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use Creasi\Base\Models\Entity;
use Creasi\Base\Models\Enums\BusinessRelativeType;
use Creasi\Base\View\Composers\TranslationsComposer;
use Illuminate\Auth\Events\Authenticated;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\View;
Expand Down Expand Up @@ -64,6 +66,10 @@ public function register(): void
}

$this->registerBindings();

$this->booting(function (): void {
Event::listen(Authenticated::class, Listeners\RegisterUserDevice::class);
});
}

protected function registerPublishables(): void
Expand Down
2 changes: 2 additions & 0 deletions tests/Fixtures/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Creasi\Tests\Fixtures;

use Creasi\Base\Contracts\HasIdentity;
use Creasi\Base\Models\Concerns\WithDevices;
use Creasi\Base\Models\Concerns\WithIdentity;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand All @@ -25,6 +26,7 @@ class User extends Authenticatable implements HasIdentity
use HasApiTokens;
use HasFactory;
use Notifiable;
use WithDevices;
use WithIdentity;

protected static function newFactory()
Expand Down

0 comments on commit 55b2fe9

Please sign in to comment.