1 | <?php |
||
13 | class SourcedUser extends EventSourcedAggregateRoot |
||
14 | { |
||
15 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
16 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
17 | ///////////////////////////////////////////// PROPERTIES /////////////////////////////////////////////////// |
||
18 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
19 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
20 | |||
21 | /** |
||
22 | * @var ApplicationUserId |
||
23 | */ |
||
24 | private $id; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private $name; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | private $preferredLanguage; |
||
35 | |||
36 | /** |
||
37 | * @var ThirdPartyAccount[] |
||
38 | */ |
||
39 | private $thirdPartyAccounts; |
||
40 | |||
41 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
42 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
43 | ///////////////////////////////////////// PRIVATE CONSTRUCTOR ////////////////////////////////////////////// |
||
44 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
45 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
46 | |||
47 | /** |
||
48 | * Constructor |
||
49 | */ |
||
50 | 12 | private function __construct() |
|
54 | |||
55 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
56 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
57 | //////////////////////////////////////////// DOMAIN METHODS //////////////////////////////////////////////// |
||
58 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
59 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
60 | |||
61 | /** |
||
62 | * @param ThirdPartyAccount $thirdPartyUser |
||
63 | */ |
||
64 | 6 | public function linkToThirdPartyAccount(ThirdPartyAccount $thirdPartyUser) |
|
65 | { |
||
66 | 6 | if ($this->isThirdPartyAccountAlreadyLinked($thirdPartyUser->getSource())) { |
|
67 | 3 | $this->apply(new ThirdPartyAccountReplacedEvent($this->id, $thirdPartyUser)); |
|
68 | 2 | } |
|
69 | |||
70 | 6 | $this->apply(new ThirdPartyAccountLinkedEvent($this->id, $thirdPartyUser)); |
|
71 | 6 | } |
|
72 | |||
73 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
74 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
75 | ////////////////////////////////////////////// ACCESSORS /////////////////////////////////////////////////// |
||
76 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
77 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
78 | |||
79 | /** |
||
80 | * Returns the user id |
||
81 | * |
||
82 | * @return ApplicationUserId |
||
83 | */ |
||
84 | 3 | public function getId() |
|
88 | |||
89 | /** |
||
90 | * @return string |
||
91 | */ |
||
92 | 9 | public function getAggregateRootId() |
|
96 | |||
97 | /** |
||
98 | * Returns the user name |
||
99 | * |
||
100 | * @return string |
||
101 | */ |
||
102 | 3 | public function getName() |
|
106 | |||
107 | /** |
||
108 | * Returns the preferred language |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | 3 | public function getPreferredLanguage() |
|
116 | |||
117 | /** |
||
118 | * @return ThirdPartyAccount[] |
||
119 | */ |
||
120 | 6 | public function getThirdPartyAccounts() |
|
124 | |||
125 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
126 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
127 | ////////////////////////////////////////// PRIVATE METHODS ///////////////////////////////////////////////// |
||
128 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
129 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
130 | |||
131 | /** |
||
132 | * Initialize the game |
||
133 | * |
||
134 | * @param ApplicationUserId $id |
||
135 | * @param string $name |
||
136 | * @param string $language |
||
137 | */ |
||
138 | 9 | private function initialize(ApplicationUserId $id, $name, $language) |
|
142 | |||
143 | /** |
||
144 | * @param Source $source |
||
145 | * |
||
146 | * @return bool |
||
147 | */ |
||
148 | 6 | private function isThirdPartyAccountAlreadyLinked(Source $source) |
|
152 | |||
153 | /** |
||
154 | * @param ThirdPartyAccount $account |
||
155 | */ |
||
156 | 6 | private function setThirdPartyUser(ThirdPartyAccount $account) |
|
160 | |||
161 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
162 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
163 | //////////////////////////////////////////// APPLY EVENTS ////////////////////////////////////////////////// |
||
164 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
165 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
166 | |||
167 | /** |
||
168 | * Apply the user created event |
||
169 | * |
||
170 | * @param \MessageApp\Event\UserCreatedEvent $event |
||
171 | */ |
||
172 | 9 | protected function applyUserCreatedEvent(UserCreatedEvent $event) |
|
179 | |||
180 | /** |
||
181 | * Apply the third party account linked event |
||
182 | * |
||
183 | * @param ThirdPartyAccountLinkedEvent $event |
||
184 | */ |
||
185 | 6 | protected function applyThirdPartyAccountLinkedEvent(ThirdPartyAccountLinkedEvent $event) |
|
189 | |||
190 | /** |
||
191 | * Apply the third party account replaced event |
||
192 | * |
||
193 | * @param ThirdPartyAccountReplacedEvent $event |
||
194 | */ |
||
195 | 3 | protected function applyThirdPartyAccountReplacedEvent(ThirdPartyAccountReplacedEvent $event) |
|
199 | |||
200 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
201 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
202 | ///////////////////////////////////////// STATIC CONSTRUCTOR /////////////////////////////////////////////// |
||
203 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
204 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
205 | |||
206 | /** |
||
207 | * Create a new instance |
||
208 | * |
||
209 | * @param ApplicationUserId $id |
||
210 | * @param string $name |
||
211 | * @param string $language |
||
212 | * |
||
213 | * @return SourcedUser |
||
214 | */ |
||
215 | 9 | public static function createUser(ApplicationUserId $id, $name, $language) |
|
226 | |||
227 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
228 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
229 | /////////////////////////////////////////// RECONSTITUTION ///////////////////////////////////////////////// |
||
230 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
231 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
232 | |||
233 | /** |
||
234 | * Static construction method for reconstitution |
||
235 | * |
||
236 | * @return SourcedUser |
||
237 | */ |
||
238 | 3 | public static function instantiateForReconstitution() |
|
242 | } |
||
243 |