Code Duplication    Length = 72-73 lines in 2 locations

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

@@ 30-102 (lines=73) @@
27
 * @author Beñat Espiña <[email protected]>
28
 * @author Gorka Laucirica <[email protected]>
29
 */
30
class SignUpUserHandler
31
{
32
    /**
33
     * The user password encoder.
34
     *
35
     * @var UserPasswordEncoder
36
     */
37
    private $encoder;
38
39
    /**
40
     * The user sign up factory.
41
     *
42
     * @var UserFactorySignUp
43
     */
44
    private $factory;
45
46
    /**
47
     * The user repository.
48
     *
49
     * @var UserRepository
50
     */
51
    private $repository;
52
53
    /**
54
     * Constructor.
55
     *
56
     * @param UserRepository      $aRepository The user repository
57
     * @param UserPasswordEncoder $anEncoder   The password encoder
58
     * @param UserFactorySignUp   $aFactory    The user sign up factory
59
     */
60
    public function __construct(
61
        UserRepository $aRepository,
62
        UserPasswordEncoder $anEncoder,
63
        UserFactorySignUp $aFactory
64
    ) {
65
        $this->repository = $aRepository;
66
        $this->encoder = $anEncoder;
67
        $this->factory = $aFactory;
68
    }
69
70
    /**
71
     * Handles the given command.
72
     *
73
     * @param SignUpUserCommand $aCommand The command
74
     *
75
     * @throws UserAlreadyExistException when the user id is already exists
76
     */
77
    public function __invoke(SignUpUserCommand $aCommand)
78
    {
79
        $id = new UserId($aCommand->id());
80
        if (null !== $this->repository->userOfId($id)) {
81
            throw new UserAlreadyExistException();
82
        }
83
        $email = new UserEmail($aCommand->email());
84
        if (null !== $this->repository->userOfEmail($email)) {
85
            throw new UserAlreadyExistException();
86
        }
87
88
        $userRoles = array_map(function ($role) {
89
            return new UserRole($role);
90
        }, $aCommand->roles());
91
92
        $user = $this->factory->build(
93
            $id,
94
            $email,
95
            UserPassword::fromPlain($aCommand->password(), $this->encoder),
96
            $userRoles
97
        );
98
        $user->enableAccount();
99
100
        $this->repository->persist($user);
101
    }
102
}
103

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

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