Passed
Push — develop ( 37c69c...faa16c )
by Laurent
03:14 queued 37s
created

EditUserHandler::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
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()) {
0 ignored issues
show
introduced by
The condition $user->username() !== $command->username() is always true.
Loading history...
44
            $user->renameUser($command->username());
45
        }
46
        if ($user->email() !== $command->email()) {
0 ignored issues
show
introduced by
The condition $user->email() !== $command->email() is always true.
Loading history...
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