Code Duplication    Length = 64-65 lines in 2 locations

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

@@ 29-93 (lines=65) @@
26
 * @author Beñat Espiña <[email protected]>
27
 * @author Gorka Laucirica <[email protected]>
28
 */
29
class ByInvitationSignUpUserHandler
30
{
31
    /**
32
     * The user password encoder.
33
     *
34
     * @var UserPasswordEncoder
35
     */
36
    private $encoder;
37
38
    /**
39
     * The user factory.
40
     *
41
     * @var UserFactorySignUp
42
     */
43
    private $factory;
44
45
    /**
46
     * The user repository.
47
     *
48
     * @var UserRepository
49
     */
50
    private $userRepository;
51
52
    /**
53
     * Constructor.
54
     *
55
     * @param UserRepository      $aUserRepository The user repository
56
     * @param UserPasswordEncoder $anEncoder       The password encoder
57
     * @param UserFactorySignUp   $aFactory        The user sign up factory
58
     */
59
    public function __construct(
60
        UserRepository $aUserRepository,
61
        UserPasswordEncoder $anEncoder,
62
        UserFactorySignUp $aFactory
63
    ) {
64
        $this->userRepository = $aUserRepository;
65
        $this->encoder = $anEncoder;
66
        $this->factory = $aFactory;
67
    }
68
69
    /**
70
     * Handles the given command.
71
     *
72
     * @param ByInvitationSignUpUserCommand $aCommand The command
73
     *
74
     * @throws UserDoesNotExistException when the user does not exist
75
     */
76
    public function __invoke(ByInvitationSignUpUserCommand $aCommand)
77
    {
78
        $user = $this->userRepository->userOfInvitationToken(
79
            new UserToken($aCommand->invitationToken())
80
        );
81
        if (null === $user) {
82
            throw new UserDoesNotExistException();
83
        }
84
85
        foreach ($aCommand->roles() as $role) {
86
            $user->grant(new UserRole($role));
87
        }
88
        $user->changePassword(UserPassword::fromPlain($aCommand->password(), $this->encoder));
89
        $user->enableAccount();
90
91
        $this->userRepository->persist($user);
92
    }
93
}
94

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

@@ 29-92 (lines=64) @@
26
 * @author Beñat Espiña <[email protected]>
27
 * @author Gorka Laucirica <[email protected]>
28
 */
29
class ByInvitationWithConfirmationSignUpUserHandler
30
{
31
    /**
32
     * The user password encoder.
33
     *
34
     * @var UserPasswordEncoder
35
     */
36
    private $encoder;
37
38
    /**
39
     * The user sign up factory.
40
     *
41
     * @var UserFactorySignUp
42
     */
43
    private $factory;
44
45
    /**
46
     * The user repository.
47
     *
48
     * @var UserRepository
49
     */
50
    private $userRepository;
51
52
    /**
53
     * Constructor.
54
     *
55
     * @param UserRepository      $aUserRepository The user repository
56
     * @param UserPasswordEncoder $anEncoder       The password encoder
57
     * @param UserFactorySignUp   $aFactory        The user sign up factory
58
     */
59
    public function __construct(
60
        UserRepository $aUserRepository,
61
        UserPasswordEncoder $anEncoder,
62
        UserFactorySignUp $aFactory
63
    ) {
64
        $this->userRepository = $aUserRepository;
65
        $this->encoder = $anEncoder;
66
        $this->factory = $aFactory;
67
    }
68
69
    /**
70
     * Handles the given command.
71
     *
72
     * @param ByInvitationWithConfirmationSignUpUserCommand $aCommand The command
73
     *
74
     * @throws UserDoesNotExistException when the user id is already exists
75
     */
76
    public function __invoke(ByInvitationWithConfirmationSignUpUserCommand $aCommand)
77
    {
78
        $user = $this->userRepository->userOfInvitationToken(
79
            new UserToken($aCommand->invitationToken())
80
        );
81
        if (null === $user) {
82
            throw new UserDoesNotExistException();
83
        }
84
85
        foreach ($aCommand->roles() as $role) {
86
            $user->grant(new UserRole($role));
87
        }
88
        $user->changePassword(UserPassword::fromPlain($aCommand->password(), $this->encoder));
89
90
        $this->userRepository->persist($user);
91
    }
92
}
93