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\Infrastructure\Persistence; |
14
|
|
|
|
15
|
|
|
use BenGorUser\User\Domain\Model\User; |
16
|
|
|
use BenGorUser\User\Domain\Model\UserEmail; |
17
|
|
|
use BenGorUser\User\Domain\Model\UserId; |
18
|
|
|
use BenGorUser\User\Domain\Model\UserRepository; |
19
|
|
|
use BenGorUser\User\Domain\Model\UserToken; |
20
|
|
|
use BenGorUser\User\Infrastructure\Domain\Model\UserEventBus; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* In memory user repository class. |
24
|
|
|
* |
25
|
|
|
* @author Beñat Espiña <[email protected]> |
26
|
|
|
* @author Gorka Laucirica <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
final class InMemoryUserRepository implements UserRepository |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Array which contains the users. |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $users; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The user event bus, it can be null |
39
|
|
|
* |
40
|
|
|
* @var UserEventBus|null |
41
|
|
|
*/ |
42
|
|
|
private $eventBus; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constructor. |
46
|
|
|
* |
47
|
|
|
* @param UserEventBus|null $anEventBus The user event bus, it can be null |
48
|
|
|
*/ |
49
|
|
|
public function __construct(UserEventBus $anEventBus = null) |
50
|
|
|
{ |
51
|
|
|
$this->users = []; |
52
|
|
|
$this->eventBus = $anEventBus; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function userOfId(UserId $anId) |
59
|
|
|
{ |
60
|
|
|
if (isset($this->users[$anId->id()])) { |
61
|
|
|
return $this->users[$anId->id()]; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function userOfEmail(UserEmail $anEmail) |
69
|
|
|
{ |
70
|
|
|
foreach ($this->users as $user) { |
71
|
|
|
if (true === $user->email()->equals($anEmail)) { |
72
|
|
|
return $user; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function userOfConfirmationToken(UserToken $aConfirmationToken) |
81
|
|
|
{ |
82
|
|
|
foreach ($this->users as $user) { |
83
|
|
|
if (true === $user->confirmationToken()->equals($aConfirmationToken)) { |
84
|
|
|
return $user; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function userOfInvitationToken(UserToken $anInvitationToken) |
93
|
|
|
{ |
94
|
|
|
foreach ($this->users as $user) { |
95
|
|
|
if (true === $user->invitationToken()->equals($anInvitationToken)) { |
96
|
|
|
return $user; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function userOfRememberPasswordToken(UserToken $aRememberPasswordToken) |
105
|
|
|
{ |
106
|
|
|
foreach ($this->users as $user) { |
107
|
|
|
if (true === $user->rememberPasswordToken()->equals($aRememberPasswordToken)) { |
108
|
|
|
return $user; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
|
View Code Duplication |
public function persist(User $aUser) |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$this->users[$aUser->id()->id()] = $aUser; |
119
|
|
|
|
120
|
|
|
if ($this->eventBus instanceof UserEventBus) { |
121
|
|
|
$this->handle($aUser->events()); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
|
View Code Duplication |
public function remove(User $aUser) |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
unset($this->users[$aUser->id()->id()]); |
131
|
|
|
|
132
|
|
|
if ($this->eventBus instanceof UserEventBus) { |
133
|
|
|
$this->handle($aUser->events()); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
|
|
public function size() |
141
|
|
|
{ |
142
|
|
|
return count($this->users); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Handles the given events with event bus. |
147
|
|
|
* |
148
|
|
|
* @param array $events A collection of user domain events |
149
|
|
|
*/ |
150
|
|
|
private function handle($events) |
151
|
|
|
{ |
152
|
|
|
foreach ($events as $event) { |
153
|
|
|
$this->eventBus->handle($event); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
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.