Completed
Push — master ( 728b57...1a78ff )
by Beñat
03:57
created

WithConfirmationSignUpUserHandler::__invoke()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 24
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 24
loc 24
rs 8.9713
cc 2
eloc 15
nc 2
nop 1
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\Service\SignUp;
14
15
use BenGorUser\User\Application\DataTransformer\UserDataTransformer;
16
use BenGorUser\User\Domain\Model\Exception\UserAlreadyExistException;
17
use BenGorUser\User\Domain\Model\UserEmail;
18
use BenGorUser\User\Domain\Model\UserFactory;
19
use BenGorUser\User\Domain\Model\UserPassword;
20
use BenGorUser\User\Domain\Model\UserPasswordEncoder;
21
use BenGorUser\User\Domain\Model\UserRepository;
22
use BenGorUser\User\Domain\Model\UserRole;
23
24
/**
25
 * With confirmation sign up user user command handler class.
26
 *
27
 * @author Beñat Espiña <[email protected]>
28
 * @author Gorka Laucirica <[email protected]>
29
 */
30 View Code Duplication
class WithConfirmationSignUpUserHandler
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...
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