Completed
Push — master ( 785fac...ceacae )
by Beñat
03:05
created

ByInvitationSignUpUserHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 11
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 11
rs 9.4285
cc 1
eloc 9
nc 1
nop 4
1
<?php
2
3
/*
4
 * This file is part of the BenGorUser 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
namespace BenGorUser\User\Application\Command\SignUp;
14
15
use BenGorUser\User\Domain\Model\Exception\UserAlreadyExistException;
16
use BenGorUser\User\Domain\Model\Exception\UserGuestDoesNotExistException;
17
use BenGorUser\User\Domain\Model\UserFactory;
18
use BenGorUser\User\Domain\Model\UserGuestRepository;
19
use BenGorUser\User\Domain\Model\UserId;
20
use BenGorUser\User\Domain\Model\UserPassword;
21
use BenGorUser\User\Domain\Model\UserPasswordEncoder;
22
use BenGorUser\User\Domain\Model\UserRepository;
23
use BenGorUser\User\Domain\Model\UserRole;
24
use BenGorUser\User\Domain\Model\UserToken;
25
26
/**
27
 * By invitation sign up user user command handler class.
28
 *
29
 * @author Beñat Espiña <[email protected]>
30
 * @author Gorka Laucirica <[email protected]>
31
 */
32 View Code Duplication
class ByInvitationSignUpUserHandler
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
{
34
    /**
35
     * The user password encoder.
36
     *
37
     * @var UserPasswordEncoder
38
     */
39
    private $encoder;
40
41
    /**
42
     * The user factory.
43
     *
44
     * @var UserFactory
45
     */
46
    private $factory;
47
48
    /**
49
     * The user repository.
50
     *
51
     * @var UserRepository
52
     */
53
    private $userRepository;
54
55
    /**
56
     * The user guest repository.
57
     *
58
     * @var UserGuestRepository
59
     */
60
    private $userGuestRepository;
61
62
    /**
63
     * Constructor.
64
     *
65
     * @param UserRepository      $aUserRepository      The user repository
66
     * @param UserPasswordEncoder $anEncoder            The password encoder
67
     * @param UserFactory         $aFactory             The user factory
68
     * @param UserGuestRepository $aUserGuestRepository The user guest repository
69
     */
70
    public function __construct(
71
        UserRepository $aUserRepository,
72
        UserPasswordEncoder $anEncoder,
73
        UserFactory $aFactory,
74
        UserGuestRepository $aUserGuestRepository
75
    ) {
76
        $this->userRepository = $aUserRepository;
77
        $this->encoder = $anEncoder;
78
        $this->factory = $aFactory;
79
        $this->userGuestRepository = $aUserGuestRepository;
80
    }
81
82
    /**
83
     * Handles the given command.
84
     *
85
     * @param ByInvitationSignUpUserCommand $aCommand The command
86
     *
87
     * @throws UserAlreadyExistException      when the user id is already exists
88
     * @throws UserGuestDoesNotExistException when the user guest does not exist
89
     */
90
    public function __invoke(ByInvitationSignUpUserCommand $aCommand)
91
    {
92
        $id = new UserId($aCommand->id());
93
        if (null !== $this->userRepository->userOfId($id)) {
94
            throw new UserAlreadyExistException();
95
        }
96
        $userGuest = $this->userGuestRepository->userGuestOfInvitationToken(
97
            new UserToken($aCommand->invitationToken())
98
        );
99
        if (null === $userGuest) {
100
            throw new UserGuestDoesNotExistException();
101
        }
102
        $email = $userGuest->email();
103
        $this->userGuestRepository->remove($userGuest);
104
105
        $userRoles = array_map(function ($role) {
106
            return new UserRole($role);
107
        }, $aCommand->roles());
108
109
        $user = $this->factory->register(
110
            $id,
111
            $email,
112
            UserPassword::fromPlain($aCommand->password(), $this->encoder),
113
            $userRoles
114
        );
115
        $user->enableAccount();
116
117
        $this->userRepository->persist($user);
118
    }
119
}
120