add User persistence: model, migration, eloquent + fake repo

UserModel maps users table (id, email unique, password_hash,
is_admin bool default false). EloquentUserRepository implements
UserRepository: create from CreateUserDto, find by id,
findByEmail. toDomain() materializes a User entity wrapping email
in EmailAddress vo. FakeUserRepository: in-memory map keyed by
auto-incrementing id, returns defensive copies on read (per
youngstartup pattern). composer stan script now passes
--no-progress for cleaner ci output.
This commit is contained in:
yisroel 2026-05-06 15:10:21 +03:00
parent 533320fcac
commit eca73213f5
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
5 changed files with 171 additions and 1 deletions

View file

@ -0,0 +1,43 @@
<?php
namespace App\User;
use App\Shared\ValueObject\EmailAddress;
class EloquentUserRepository implements UserRepository
{
public function create(CreateUserDto $dto): User
{
$model = UserModel::create([
'email' => $dto->email->value(),
'password_hash' => $dto->passwordHash,
'is_admin' => $dto->isAdmin,
]);
return $this->toDomain($model);
}
public function find(int $id): ?User
{
$model = UserModel::find($id);
return $model === null ? null : $this->toDomain($model);
}
public function findByEmail(EmailAddress $email): ?User
{
$model = UserModel::where('email', $email->value())->first();
return $model === null ? null : $this->toDomain($model);
}
private function toDomain(UserModel $model): User
{
return new User(
id: $model->id,
email: new EmailAddress($model->email),
passwordHash: $model->password_hash,
isAdmin: $model->is_admin,
);
}
}

View file

@ -0,0 +1,38 @@
<?php
namespace App\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
/**
* @property int $id
* @property string $email
* @property string $password_hash
* @property bool $is_admin
*
* @method static Builder<static>|UserModel newModelQuery()
* @method static Builder<static>|UserModel newQuery()
* @method static Builder<static>|UserModel query()
* @method static Builder<static>|UserModel whereId($value)
* @method static Builder<static>|UserModel whereEmail($value)
* @method static Builder<static>|UserModel whereIsAdmin($value)
*
* @mixin \Eloquent
*/
class UserModel extends Model
{
protected $table = 'users';
public $timestamps = false;
protected $fillable = [
'email',
'password_hash',
'is_admin',
];
protected $casts = [
'is_admin' => 'boolean',
];
}