Code Duplication    Length = 84-85 lines in 2 locations

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

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

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

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