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 #64 from creasico/feat-app-user
Browse files Browse the repository at this point in the history
Menghapus `Creasi\Base\Models\User` dan gunakan `App\Models\User`
  • Loading branch information
feryardiant authored Oct 14, 2023
2 parents b3a632f + ab5910e commit ce3e6af
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 31 deletions.
10 changes: 6 additions & 4 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<User>
* @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.
Expand Down Expand Up @@ -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();
}

Expand Down
6 changes: 2 additions & 4 deletions src/Models/Concerns/WithCredential.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Creasi\Base\Models\Concerns;

use Creasi\Base\Models\User;

/**
* @mixin \Creasi\Base\Contracts\HasCredential
*/
Expand All @@ -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'));
}
}
19 changes: 19 additions & 0 deletions src/Models/Concerns/WithIdentity.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\Personnel;

/**
* @mixin \Creasi\Base\Contracts\HasIdentity
*/
trait WithIdentity
{
/**
* @return \Illuminate\Database\Eloquent\Relations\HasOne|Personnel
*/
public function identity()
{
return $this->hasOne(Personnel::class);
}
}
2 changes: 1 addition & 1 deletion src/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 6 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
24 changes: 4 additions & 20 deletions src/Models/User.php → tests/Fixtures/User.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

namespace Creasi\Base\Models;
namespace Creasi\Tests\Fixtures;

use Creasi\Base\Contracts\HasIdentity;
use Creasi\Base\Models\Concerns\WithIdentity;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
Expand All @@ -21,30 +22,13 @@
*/
class User extends Authenticatable implements HasIdentity
{
use HasApiTokens;
use HasFactory;
use Notifiable;
use HasApiTokens;

protected $fillable = ['name', 'email', 'password'];

protected $hidden = ['password', 'remember_token'];

protected $casts = [
'email_verified_at' => '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);
}
}
2 changes: 1 addition & 1 deletion tests/Models/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ce3e6af

Please sign in to comment.