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\Command\SignUp; |
14
|
|
|
|
15
|
|
|
use BenGorUser\User\Domain\Model\Exception\UserAlreadyExistException; |
16
|
|
|
use BenGorUser\User\Domain\Model\UserEmail; |
17
|
|
|
use BenGorUser\User\Domain\Model\UserFactorySignUp; |
18
|
|
|
use BenGorUser\User\Domain\Model\UserId; |
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
|
|
|
* 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 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
|
|
|
|
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.