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\Invite; |
14
|
|
|
|
15
|
|
|
use BenGorUser\User\Domain\Model\Exception\UserAlreadyExistException; |
16
|
|
|
use BenGorUser\User\Domain\Model\UserEmail; |
17
|
|
|
use BenGorUser\User\Domain\Model\UserFactoryInvite; |
18
|
|
|
use BenGorUser\User\Domain\Model\UserId; |
19
|
|
|
use BenGorUser\User\Domain\Model\UserRepository; |
20
|
|
|
use BenGorUser\User\Domain\Model\UserRole; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Invite user command handler class. |
24
|
|
|
* |
25
|
|
|
* @author Beñat Espiña <[email protected]> |
26
|
|
|
* @author Gorka Laucirica <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class InviteUserHandler |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* The user invite factory. |
32
|
|
|
* |
33
|
|
|
* @var UserFactoryInvite |
34
|
|
|
*/ |
35
|
|
|
private $factory; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The user repository. |
39
|
|
|
* |
40
|
|
|
* @var UserRepository |
41
|
|
|
*/ |
42
|
|
|
private $repository; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constructor. |
46
|
|
|
* |
47
|
|
|
* @param UserRepository $aRepository The user repository |
48
|
|
|
* @param UserFactoryInvite $aFactory The user invite factory |
49
|
|
|
*/ |
50
|
|
|
public function __construct(UserRepository $aRepository, UserFactoryInvite $aFactory) |
51
|
|
|
{ |
52
|
|
|
$this->repository = $aRepository; |
53
|
|
|
$this->factory = $aFactory; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Handles the given command. |
58
|
|
|
* |
59
|
|
|
* @param InviteUserCommand $aCommand The command |
60
|
|
|
* |
61
|
|
|
* @throws UserAlreadyExistException when the user already exists |
62
|
|
|
*/ |
63
|
|
|
public function __invoke(InviteUserCommand $aCommand) |
64
|
|
|
{ |
65
|
|
|
$email = new UserEmail($aCommand->email()); |
66
|
|
|
|
67
|
|
|
$user = $this->repository->userOfEmail($email); |
68
|
|
|
if (null !== $user) { |
69
|
|
|
throw new UserAlreadyExistException(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$user = $this->factory->build( |
73
|
|
|
new UserId($aCommand->id()), |
74
|
|
|
$email, |
75
|
|
|
array_map(function ($role) { |
76
|
|
|
return new UserRole($role); |
77
|
|
|
}, $aCommand->roles()) |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
$this->repository->persist($user); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|