1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MessageApp\User\Entity; |
4
|
|
|
|
5
|
|
|
use Broadway\EventSourcing\EventSourcedAggregateRoot; |
6
|
|
|
use MessageApp\Event\ThirdPartyAccountLinkedEvent; |
7
|
|
|
use MessageApp\Event\ThirdPartyAccountReplacedEvent; |
8
|
|
|
use MessageApp\Event\UserCreatedEvent; |
9
|
|
|
use MessageApp\User\ApplicationUser as OriginalAppUser; |
10
|
|
|
use MessageApp\User\ApplicationUserId; |
11
|
|
|
use MessageApp\User\ThirdParty\Account as ThirdPartyAccount; |
12
|
|
|
use MessageApp\User\ThirdParty\Source; |
13
|
|
|
|
14
|
|
|
class ApplicationUser extends EventSourcedAggregateRoot implements OriginalAppUser |
15
|
|
|
{ |
16
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
17
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
18
|
|
|
///////////////////////////////////////////// PROPERTIES /////////////////////////////////////////////////// |
19
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
20
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var ApplicationUserId |
24
|
|
|
*/ |
25
|
|
|
private $id; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $name; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $preferredLanguage; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var ThirdPartyAccount[] |
39
|
|
|
*/ |
40
|
|
|
private $thirdPartyAccounts; |
41
|
|
|
|
42
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
43
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
44
|
|
|
///////////////////////////////////////// PRIVATE CONSTRUCTOR ////////////////////////////////////////////// |
45
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
46
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Constructor |
50
|
|
|
*/ |
51
|
6 |
|
private function __construct() |
52
|
|
|
{ |
53
|
6 |
|
$this->thirdPartyAccounts = []; |
54
|
6 |
|
} |
55
|
|
|
|
56
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
57
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
58
|
|
|
//////////////////////////////////////////// DOMAIN METHODS //////////////////////////////////////////////// |
59
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
60
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param ThirdPartyAccount $thirdPartyUser |
64
|
|
|
*/ |
65
|
|
|
public function linkToThirdPartyAccount(ThirdPartyAccount $thirdPartyUser) |
66
|
|
|
{ |
67
|
|
|
if ($this->isThirdPartyAccountAlreadyLinked($thirdPartyUser->getSource())) { |
68
|
|
|
$this->apply(new ThirdPartyAccountReplacedEvent($this->id, $thirdPartyUser)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->apply(new ThirdPartyAccountLinkedEvent($this->id, $thirdPartyUser)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
75
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
76
|
|
|
////////////////////////////////////////////// ACCESSORS /////////////////////////////////////////////////// |
77
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
78
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns the user id |
82
|
|
|
* |
83
|
|
|
* @return ApplicationUserId |
84
|
|
|
*/ |
85
|
3 |
|
public function getId() |
86
|
|
|
{ |
87
|
3 |
|
return $this->id; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
3 |
|
public function getAggregateRootId() |
94
|
|
|
{ |
95
|
3 |
|
return $this->id; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns the user name |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
3 |
|
public function getName() |
104
|
|
|
{ |
105
|
3 |
|
return $this->name; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns the preferred language |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
3 |
|
public function getPreferredLanguage() |
114
|
|
|
{ |
115
|
3 |
|
return $this->preferredLanguage; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
119
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
120
|
|
|
////////////////////////////////////////// PRIVATE METHODS ///////////////////////////////////////////////// |
121
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
122
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Initialize the game |
126
|
|
|
* |
127
|
|
|
* @param ApplicationUserId $id |
128
|
|
|
* @param string $name |
129
|
|
|
* @param string $language |
130
|
|
|
*/ |
131
|
3 |
|
private function initialize(ApplicationUserId $id, $name, $language) |
132
|
|
|
{ |
133
|
3 |
|
$this->apply(new UserCreatedEvent($id, $name, $language)); |
134
|
3 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param Source $source |
138
|
|
|
* |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
private function isThirdPartyAccountAlreadyLinked(Source $source) |
142
|
|
|
{ |
143
|
|
|
return array_key_exists((string) $source, $this->thirdPartyAccounts); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param ThirdPartyAccount $account |
148
|
|
|
*/ |
149
|
|
|
private function setThirdPartyUser(ThirdPartyAccount $account) |
150
|
|
|
{ |
151
|
|
|
$this->thirdPartyAccounts[(string) $account->getSource()] = $account; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
155
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
156
|
|
|
//////////////////////////////////////////// APPLY EVENTS ////////////////////////////////////////////////// |
157
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
158
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Apply the user created event |
162
|
|
|
* |
163
|
|
|
* @param \MessageApp\Event\UserCreatedEvent $event |
164
|
|
|
*/ |
165
|
3 |
|
protected function applyUserCreatedEvent(UserCreatedEvent $event) |
166
|
|
|
{ |
167
|
3 |
|
$this->id = $event->getUserId(); |
168
|
3 |
|
$this->name = $event->getUsername(); |
169
|
3 |
|
$this->preferredLanguage = $event->getPreferredLanguage(); |
170
|
3 |
|
$this->thirdPartyAccounts = []; |
171
|
3 |
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Apply the third party account linked event |
175
|
|
|
* |
176
|
|
|
* @param ThirdPartyAccountLinkedEvent $event |
177
|
|
|
*/ |
178
|
|
|
protected function applyThirdPartyAccountLinkedEvent(ThirdPartyAccountLinkedEvent $event) |
179
|
|
|
{ |
180
|
|
|
$this->setThirdPartyUser($event->getThirdPartyAccount()); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Apply the third party account replaced event |
185
|
|
|
* |
186
|
|
|
* @param ThirdPartyAccountReplacedEvent $event |
187
|
|
|
*/ |
188
|
|
|
protected function applyThirdPartyAccountReplacedEvent(ThirdPartyAccountReplacedEvent $event) |
189
|
|
|
{ |
190
|
|
|
$this->setThirdPartyUser($event->getThirdPartyAccount()); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
194
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
195
|
|
|
///////////////////////////////////////// STATIC CONSTRUCTOR /////////////////////////////////////////////// |
196
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
197
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Create a new instance |
201
|
|
|
* |
202
|
|
|
* @param ApplicationUserId $id |
203
|
|
|
* @param string $name |
204
|
|
|
* @param string $language |
205
|
|
|
* |
206
|
|
|
* @return ApplicationUser |
207
|
|
|
*/ |
208
|
3 |
|
public static function createUser(ApplicationUserId $id, $name, $language) |
209
|
|
|
{ |
210
|
3 |
|
$user = new self(); |
211
|
3 |
|
$user->initialize( |
212
|
2 |
|
$id, |
213
|
2 |
|
$name, |
214
|
|
|
$language |
215
|
2 |
|
); |
216
|
|
|
|
217
|
3 |
|
return $user; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
221
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
222
|
|
|
/////////////////////////////////////////// RECONSTITUTION ///////////////////////////////////////////////// |
223
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
224
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Static construction method for reconstitution |
228
|
|
|
* |
229
|
|
|
* @return ApplicationUser |
230
|
|
|
*/ |
231
|
3 |
|
public static function instantiateForReconstitution() |
232
|
|
|
{ |
233
|
3 |
|
return new self(); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|