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\Infrastructure\User\Handler; |
15
|
|
|
|
16
|
|
|
use Administration\Domain\User\Command\EditUser; |
17
|
|
|
use Administration\Infrastructure\Persistence\DoctrineOrm\Repositories\DoctrineUserRepository; |
18
|
|
|
use Core\Domain\Model\User; |
19
|
|
|
use Core\Domain\Protocol\Common\Command\CommandHandlerProtocol; |
20
|
|
|
use Doctrine\ORM\NonUniqueResultException; |
21
|
|
|
use Doctrine\ORM\ORMException; |
22
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
23
|
|
|
|
24
|
|
|
class EditUserHandler implements CommandHandlerProtocol |
25
|
|
|
{ |
26
|
|
|
private UserPasswordEncoderInterface $passwordEncoder; |
27
|
|
|
private DoctrineUserRepository $userRepository; |
28
|
|
|
|
29
|
|
|
public function __construct(UserPasswordEncoderInterface $passwordEncoder, DoctrineUserRepository $userRepository) |
30
|
|
|
{ |
31
|
|
|
$this->passwordEncoder = $passwordEncoder; |
32
|
|
|
$this->userRepository = $userRepository; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @throws NonUniqueResultException |
37
|
|
|
* @throws ORMException |
38
|
|
|
*/ |
39
|
|
|
public function __invoke(EditUser $command): void |
40
|
|
|
{ |
41
|
|
|
$userToUpdate = $this->userRepository->findOneByUuid($command->uuid()->toString()); |
42
|
|
|
|
43
|
|
|
if (null === $userToUpdate) { |
44
|
|
|
throw new \DomainException('User provided does not exist!'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->updateUser($command, $userToUpdate); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function updateUser(EditUser $command, User $user): User |
51
|
|
|
{ |
52
|
|
|
if ($user->username() !== $command->username()) { |
|
|
|
|
53
|
|
|
$user->renameUser($command->username()); |
54
|
|
|
} |
55
|
|
|
if ($user->email() !== $command->email()) { |
56
|
|
|
$user->changeEmail($command->email()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$user->changePassword( |
60
|
|
|
$this->passwordEncoder->encodePassword( |
61
|
|
|
$user, |
62
|
|
|
$command->password() |
63
|
|
|
) |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
if ($user->roles() !== $command->roles()) { |
67
|
|
|
$user->assignRoles($command->roles()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $user; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|