1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the G.L.S.R. Apps package. |
7
|
|
|
* |
8
|
|
|
* (c) Dev-Int Création <[email protected]>. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Administration\Domain\User\Handler; |
15
|
|
|
|
16
|
|
|
use Administration\Domain\Protocol\Repository\UserRepositoryProtocol; |
17
|
|
|
use Administration\Domain\User\Command\EditUser; |
18
|
|
|
use Administration\Domain\User\Model\User; |
19
|
|
|
use Core\Domain\Protocol\Common\Command\CommandHandlerProtocol; |
20
|
|
|
|
21
|
|
|
final class EditUserHandler implements CommandHandlerProtocol |
22
|
|
|
{ |
23
|
|
|
private UserRepositoryProtocol $repository; |
24
|
|
|
|
25
|
|
|
public function __construct(UserRepositoryProtocol $repository) |
26
|
|
|
{ |
27
|
|
|
$this->repository = $repository; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function __invoke(EditUser $command): void |
31
|
|
|
{ |
32
|
|
|
$userToUpdate = $this->repository->findOneByUuid($command->uuid()->toString()); |
33
|
|
|
|
34
|
|
|
if (null === $userToUpdate) { |
35
|
|
|
throw new \DomainException('User provided does not exist!'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$this->updateUser($command, $userToUpdate); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private function updateUser(EditUser $command, User $user): User |
42
|
|
|
{ |
43
|
|
|
if ($user->username() !== $command->username()) { |
|
|
|
|
44
|
|
|
$user->renameUser($command->username()); |
45
|
|
|
} |
46
|
|
|
if ($user->email() !== $command->email()) { |
|
|
|
|
47
|
|
|
$user->changeEmail($command->email()); |
48
|
|
|
} |
49
|
|
|
if ($user->password() !== $command->password()) { |
50
|
|
|
$user->changePassword($command->password()); |
51
|
|
|
} |
52
|
|
|
if ($user->roles() !== $command->roles()) { |
53
|
|
|
$user->assignRoles($command->roles()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $user; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|