55 lines
1.2 KiB
PHP
55 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\User;
|
|
|
|
use App\Shared\ValueObject\EmailAddress;
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
class EloquentUserRepository implements UserRepository
|
|
{
|
|
public function create(CreateUserDto $dto): User
|
|
{
|
|
$model = UserModel::create([
|
|
'email' => $dto->email->value(),
|
|
'password' => Hash::make($dto->password),
|
|
]);
|
|
|
|
return $this->toDomain($model);
|
|
}
|
|
|
|
public function find(int $id): ?User
|
|
{
|
|
$model = UserModel::find($id);
|
|
if ($model === null) {
|
|
return null;
|
|
}
|
|
|
|
return $this->toDomain($model);
|
|
}
|
|
|
|
public function findByCredentials(
|
|
EmailAddress $email,
|
|
string $password,
|
|
): ?User
|
|
{
|
|
$model = UserModel::query()
|
|
->where('email', $email->value())
|
|
->first();
|
|
if (
|
|
$model === null
|
|
|| ! Hash::check($password, $model->password)
|
|
) {
|
|
return null;
|
|
}
|
|
|
|
return $this->toDomain($model);
|
|
}
|
|
|
|
private function toDomain(UserModel $model): User
|
|
{
|
|
return new User(
|
|
id: $model->id,
|
|
email: new EmailAddress($model->email),
|
|
);
|
|
}
|
|
}
|