Completed
Pull Request — master (#366)
by Beñat
04:52
created

SignUpUserHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 30
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 9 1
A checkUserExists() 0 10 3
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\Notifier\Application\Command\Inbox;
16
17
use Kreta\Notifier\Domain\Model\Inbox\User;
18
use Kreta\Notifier\Domain\Model\Inbox\UserAlreadyExists;
19
use Kreta\Notifier\Domain\Model\Inbox\UserDoesNotExist;
20
use Kreta\Notifier\Domain\Model\Inbox\UserId;
21
use Kreta\Notifier\Domain\Model\Inbox\UserRepository;
22
23
class SignUpUserHandler
24
{
25
    private $repository;
26
27
    public function __construct(UserRepository $repository)
28
    {
29
        $this->repository = $repository;
30
    }
31
32
    public function __invoke(SignUpUserCommand $command)
33
    {
34
        $userId = UserId::generate($command->userId());
35
36
        $this->checkUserExists($userId);
37
38
        $user = User::signUp($userId);
39
        $this->repository->save($user);
40
    }
41
42
    private function checkUserExists(UserId $userId) : void
43
    {
44
        try {
45
            $user = $this->repository->get($userId);
46
            if ($user instanceof User) {
47
                throw new UserAlreadyExists($user->id());
48
            }
49
        } catch (UserDoesNotExist $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
50
        }
51
    }
52
}
53