58 lines
1.4 KiB
PHP
58 lines
1.4 KiB
PHP
|
<?php declare(strict_types=1);
|
||
|
|
||
|
namespace App\Services\User;
|
||
|
|
||
|
use App\Dto\User\ManyRoleDto;
|
||
|
use App\Models\User;
|
||
|
use Illuminate\Support\Carbon;
|
||
|
use Illuminate\Support\Facades\Hash;
|
||
|
use Illuminate\Support\Str;
|
||
|
|
||
|
final readonly class UserCommandHandler
|
||
|
{
|
||
|
public function handleStore(array $data, int|string $password): User
|
||
|
{
|
||
|
$data['email'] = Str::lower($data['email']);
|
||
|
$data['password'] = $this->hashPassword($password);
|
||
|
$user = User::create($data);
|
||
|
|
||
|
return $user;
|
||
|
}
|
||
|
|
||
|
public function handleUpdate(User $user, array $data): User
|
||
|
{
|
||
|
if (isset($data['email'])) {
|
||
|
$data['email'] = Str::lower($data['email']);
|
||
|
}
|
||
|
|
||
|
if (isset($data['password'])) {
|
||
|
unset($data['password']);
|
||
|
}
|
||
|
|
||
|
$user->update($data);
|
||
|
$user->touch();
|
||
|
|
||
|
return $user;
|
||
|
}
|
||
|
|
||
|
public function handleConfirmationByEmail(User $user): void
|
||
|
{
|
||
|
$user->update(['email_verified_at' => new Carbon('NOW')]);
|
||
|
}
|
||
|
|
||
|
public function handleUpdatePassword(User $user, int|string $password): void
|
||
|
{
|
||
|
$user->update(['password' => $this->hashPassword($password)]);
|
||
|
}
|
||
|
|
||
|
public function handleSyncRoles(User $user, ManyRoleDto $roles): void
|
||
|
{
|
||
|
$user->roles()->sync($roles->toArray());
|
||
|
}
|
||
|
|
||
|
private function hashPassword(int|string $password): string
|
||
|
{
|
||
|
return Hash::make($password);
|
||
|
}
|
||
|
}
|