From ab5910e8e74c8191b29569d38154b2515560fd59 Mon Sep 17 00:00:00 2001 From: Fery Wardiyanto Date: Sat, 14 Oct 2023 17:55:45 +0700 Subject: [PATCH] feat(model): get rid of `User` mdoel use app one instead I don't thing it's a good idea to redefine or change the way user model handled. That said the `User` model definition is should be in app-level, not in library-level. So that the app developer has the flexibility they desire to customize the way their app should be Signed-off-by: Fery Wardiyanto --- database/factories/UserFactory.php | 10 ++++++---- src/Models/Concerns/WithCredential.php | 6 ++---- src/Models/Concerns/WithIdentity.php | 19 +++++++++++++++++++ src/Repository.php | 2 +- src/ServiceProvider.php | 6 ++++++ {src/Models => tests/Fixtures}/User.php | 24 ++++-------------------- tests/Models/UserTest.php | 2 +- tests/TestCase.php | 2 +- 8 files changed, 40 insertions(+), 31 deletions(-) create mode 100644 src/Models/Concerns/WithIdentity.php rename {src/Models => tests/Fixtures}/User.php (66%) diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 5c01c73..0d07295 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -3,16 +3,18 @@ namespace Database\Factories; use Creasi\Base\Models\Personnel; -use Creasi\Base\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Support\Str; /** - * @extends Factory + * @extends Factory<\App\Model\User> */ class UserFactory extends Factory { - protected $model = User::class; + public function modelName() + { + return app('creasi.base.user_model'); + } /** * Define the model's default state. @@ -42,7 +44,7 @@ public function unverified(): static public function withIdentity(\Closure $cb = null): static { - if (null === $cb) { + if ($cb === null) { $cb = fn (PersonnelFactory $identity) => $identity->withProfile(); } diff --git a/src/Models/Concerns/WithCredential.php b/src/Models/Concerns/WithCredential.php index 527c481..d278c40 100644 --- a/src/Models/Concerns/WithCredential.php +++ b/src/Models/Concerns/WithCredential.php @@ -2,8 +2,6 @@ namespace Creasi\Base\Models\Concerns; -use Creasi\Base\Models\User; - /** * @mixin \Creasi\Base\Contracts\HasCredential */ @@ -24,10 +22,10 @@ public function initializeWithCredential(): void } /** - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|User + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Creasi\Base\Models\User */ public function user() { - return $this->belongsTo(User::class); + return $this->belongsTo(app('creasi.base.user_model')); } } diff --git a/src/Models/Concerns/WithIdentity.php b/src/Models/Concerns/WithIdentity.php new file mode 100644 index 0000000..efa0106 --- /dev/null +++ b/src/Models/Concerns/WithIdentity.php @@ -0,0 +1,19 @@ +hasOne(Personnel::class); + } +} diff --git a/src/Repository.php b/src/Repository.php index f2fa23b..baa9e66 100644 --- a/src/Repository.php +++ b/src/Repository.php @@ -7,7 +7,7 @@ use Creasi\Base\Contracts\Stakeholder; use Creasi\Base\Models\Entity; use Creasi\Base\Models\Enums\BusinessRelativeType; -use Creasi\Base\Models\User; +use Illuminate\Foundation\Auth\User; use Illuminate\Http\Request; use Illuminate\Routing\Router; diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 6e8d723..dfde052 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -103,6 +103,12 @@ protected function defineRoutes(): void protected function registerBindings() { + $this->app->bind('creasi.base.user_model', function ($app) { + $provider = $app['config']["auth.guards.{$app['auth']->getDefaultDriver()}.provider"]; + + return $app['config']["auth.providers.$provider.model"]; + }); + $this->app->bind(Entity::class, function ($app) { $repo = $app->make(Repository::class); diff --git a/src/Models/User.php b/tests/Fixtures/User.php similarity index 66% rename from src/Models/User.php rename to tests/Fixtures/User.php index 3cfb3f5..24bdae4 100644 --- a/src/Models/User.php +++ b/tests/Fixtures/User.php @@ -1,8 +1,9 @@ 'immutable_datetime', - ]; - - protected $appends = []; + use WithIdentity; public function password(): Attribute { return Attribute::set(fn (string $value) => \bcrypt($value)); } - - /** - * @return \Illuminate\Database\Eloquent\Relations\HasOne|Personnel - */ - public function identity() - { - return $this->hasOne(Personnel::class); - } } diff --git a/tests/Models/UserTest.php b/tests/Models/UserTest.php index 53eef31..70fb1e5 100644 --- a/tests/Models/UserTest.php +++ b/tests/Models/UserTest.php @@ -3,7 +3,7 @@ namespace Creasi\Tests\Models; use Creasi\Base\Models\Personnel; -use Creasi\Base\Models\User; +use Creasi\Tests\Fixtures\User; use Creasi\Tests\TestCase; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\Test; diff --git a/tests/TestCase.php b/tests/TestCase.php index d9d6d3d..9cd1c01 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -3,9 +3,9 @@ namespace Creasi\Tests; use Closure; -use Creasi\Base\Models\User; use Creasi\Base\ServiceProvider; use Creasi\Nusa\ServiceProvider as NusaServiceProvider; +use Creasi\Tests\Fixtures\User; use Database\Factories\PersonnelFactory; use Illuminate\Contracts\Config\Repository; use Illuminate\Foundation\Testing\RefreshDatabase;