Code Duplication    Length = 87-88 lines in 2 locations

src/BenGorUser/User/Application/Command/SignUp/ByInvitationSignUpUserHandler.php 1 location

@@ 32-119 (lines=88) @@
29
 * @author Beñat Espiña <[email protected]>
30
 * @author Gorka Laucirica <[email protected]>
31
 */
32
class ByInvitationSignUpUserHandler
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

src/BenGorUser/User/Application/Command/SignUp/ByInvitationWithConfirmationSignUpUserHandler.php 1 location

@@ 32-118 (lines=87) @@
29
 * @author Beñat Espiña <[email protected]>
30
 * @author Gorka Laucirica <[email protected]>
31
 */
32
class ByInvitationWithConfirmationSignUpUserHandler
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 ByInvitationWithConfirmationSignUpUserCommand $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(ByInvitationWithConfirmationSignUpUserCommand $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
116
        $this->userRepository->persist($user);
117
    }
118
}
119