Completed
Pull Request — master (#183)
by Beñat
10:30
created

AddUserHandler::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace Kreta\TaskManager\Application\User;
16
17
use Kreta\TaskManager\Domain\Model\User\User;
18
use Kreta\TaskManager\Domain\Model\User\UserAlreadyExistsException;
19
use Kreta\TaskManager\Domain\Model\User\UserId;
20
use Kreta\TaskManager\Domain\Model\User\UserRepository;
21
22
class AddUserHandler
23
{
24
    private $repository;
25
26
    public function __construct(UserRepository $repository)
27
    {
28
        $this->repository = $repository;
29
    }
30
31
    public function __invoke(AddUserCommand $command)
32
    {
33
        $userId = UserId::generate($command->userId());
34
        $user = $this->repository->userOfId($userId);
35
        if ($user instanceof User) {
36
            throw new UserAlreadyExistsException($user->id());
37
        }
38
39
        $this->repository->persist(
40
            new User($userId)
41
        );
42
    }
43
}
44