Completed
Push — master ( 1b016a...f598f3 )
by Beñat
59s
created

AddUserHandler   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 5
dl 0
loc 22
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 12 2
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\Command\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