1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\Organizer; |
4
|
|
|
|
5
|
|
|
use Broadway\CommandHandling\CommandHandlerInterface; |
6
|
|
|
use Broadway\Repository\RepositoryInterface; |
7
|
|
|
use CultuurNet\UDB3\Label; |
8
|
|
|
use CultuurNet\UDB3\Label\ValueObjects\Visibility; |
9
|
|
|
use CultuurNet\UDB3\Organizer\Commands\AbstractLabelCommand; |
10
|
|
|
use CultuurNet\UDB3\Organizer\Commands\AddLabel; |
11
|
|
|
use CultuurNet\UDB3\Organizer\Commands\CreateOrganizer; |
12
|
|
|
use CultuurNet\UDB3\Organizer\Commands\DeleteOrganizer; |
13
|
|
|
use CultuurNet\UDB3\Organizer\Commands\ImportLabels; |
14
|
|
|
use CultuurNet\UDB3\Organizer\Commands\RemoveLabel; |
15
|
|
|
use CultuurNet\UDB3\Organizer\Commands\UpdateAddress; |
16
|
|
|
use CultuurNet\UDB3\Organizer\Commands\UpdateContactPoint; |
17
|
|
|
use CultuurNet\UDB3\Organizer\Commands\UpdateTitle; |
18
|
|
|
use CultuurNet\UDB3\Organizer\Commands\UpdateWebsite; |
19
|
|
|
use ValueObjects\StringLiteral\StringLiteral; |
20
|
|
|
use CultuurNet\UDB3\Label\ReadModels\JSON\Repository\ReadRepositoryInterface; |
21
|
|
|
|
22
|
|
|
class OrganizerCommandHandler implements CommandHandlerInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var RepositoryInterface |
26
|
|
|
*/ |
27
|
|
|
private $organizerRepository; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ReadRepositoryInterface |
31
|
|
|
*/ |
32
|
|
|
private $labelRepository; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var OrganizerRelationServiceInterface[] |
36
|
|
|
*/ |
37
|
|
|
private $organizerRelationServices; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param RepositoryInterface $organizerRepository |
41
|
|
|
* @param ReadRepositoryInterface $labelRepository |
42
|
|
|
*/ |
43
|
|
|
public function __construct( |
44
|
|
|
RepositoryInterface $organizerRepository, |
45
|
|
|
ReadRepositoryInterface $labelRepository |
46
|
|
|
) { |
47
|
|
|
$this->organizerRepository = $organizerRepository; |
48
|
|
|
$this->labelRepository = $labelRepository; |
49
|
|
|
$this->organizerRelationServices = []; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param OrganizerRelationServiceInterface $relationService |
54
|
|
|
* @return OrganizerCommandHandler |
55
|
|
|
*/ |
56
|
|
|
public function withOrganizerRelationService(OrganizerRelationServiceInterface $relationService) |
57
|
|
|
{ |
58
|
|
|
$c = clone $this; |
59
|
|
|
$c->organizerRelationServices[] = $relationService; |
60
|
|
|
return $c; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
protected function getCommandHandlerMethods() |
67
|
|
|
{ |
68
|
|
|
return [ |
69
|
|
|
CreateOrganizer::class => 'createOrganizer', |
70
|
|
|
UpdateWebsite::class => 'updateWebsite', |
71
|
|
|
UpdateTitle::class => 'updateTitle', |
72
|
|
|
UpdateAddress::class => 'updateAddress', |
73
|
|
|
UpdateContactPoint::class => 'updateContactPoint', |
74
|
|
|
DeleteOrganizer::class => 'deleteOrganizer', |
75
|
|
|
AddLabel::class => 'addLabel', |
76
|
|
|
RemoveLabel::class => 'removeLabel', |
77
|
|
|
ImportLabels::class => 'importLabels', |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param mixed $command |
83
|
|
|
*/ |
84
|
|
|
public function handle($command) |
85
|
|
|
{ |
86
|
|
|
$class = get_class($command); |
87
|
|
|
$handlers = $this->getCommandHandlerMethods(); |
88
|
|
|
|
89
|
|
|
if (isset($handlers[$class])) { |
90
|
|
|
$method = $handlers[$class]; |
91
|
|
|
$this->{$method}($command); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function createOrganizer(CreateOrganizer $createOrganizer) |
96
|
|
|
{ |
97
|
|
|
$organizer = Organizer::create( |
98
|
|
|
$createOrganizer->getOrganizerId(), |
99
|
|
|
$createOrganizer->getMainLanguage(), |
100
|
|
|
$createOrganizer->getWebsite(), |
101
|
|
|
$createOrganizer->getTitle() |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
$this->organizerRepository->save($organizer); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param UpdateWebsite $updateWebsite |
109
|
|
|
*/ |
110
|
|
|
protected function updateWebsite(UpdateWebsite $updateWebsite) |
111
|
|
|
{ |
112
|
|
|
$organizer = $this->loadOrganizer($updateWebsite->getOrganizerId()); |
113
|
|
|
|
114
|
|
|
$organizer->updateWebsite($updateWebsite->getWebsite()); |
115
|
|
|
|
116
|
|
|
$this->organizerRepository->save($organizer); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param UpdateTitle $updateTitle |
121
|
|
|
*/ |
122
|
|
|
protected function updateTitle(UpdateTitle $updateTitle) |
123
|
|
|
{ |
124
|
|
|
$organizer = $this->loadOrganizer($updateTitle->getOrganizerId()); |
125
|
|
|
|
126
|
|
|
$organizer->updateTitle( |
127
|
|
|
$updateTitle->getTitle(), |
128
|
|
|
$updateTitle->getLanguage() |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
$this->organizerRepository->save($organizer); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param UpdateAddress $updateAddress |
136
|
|
|
*/ |
137
|
|
|
protected function updateAddress(UpdateAddress $updateAddress) |
138
|
|
|
{ |
139
|
|
|
$organizer = $this->loadOrganizer($updateAddress->getOrganizerId()); |
140
|
|
|
|
141
|
|
|
$organizer->updateAddress($updateAddress->getAddress()); |
142
|
|
|
|
143
|
|
|
$this->organizerRepository->save($organizer); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param UpdateContactPoint $updateContactPoint |
148
|
|
|
*/ |
149
|
|
|
protected function updateContactPoint(UpdateContactPoint $updateContactPoint) |
150
|
|
|
{ |
151
|
|
|
$organizer = $this->loadOrganizer($updateContactPoint->getOrganizerId()); |
152
|
|
|
|
153
|
|
|
$organizer->updateContactPoint($updateContactPoint->getContactPoint()); |
154
|
|
|
|
155
|
|
|
$this->organizerRepository->save($organizer); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param AddLabel $addLabel |
160
|
|
|
*/ |
161
|
|
|
protected function addLabel(AddLabel $addLabel) |
162
|
|
|
{ |
163
|
|
|
$organizer = $this->loadOrganizer($addLabel->getOrganizerId()); |
164
|
|
|
|
165
|
|
|
$organizer->addLabel($this->createLabel($addLabel)); |
166
|
|
|
|
167
|
|
|
$this->organizerRepository->save($organizer); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param RemoveLabel $removeLabel |
172
|
|
|
*/ |
173
|
|
|
protected function removeLabel(RemoveLabel $removeLabel) |
174
|
|
|
{ |
175
|
|
|
$organizer = $this->loadOrganizer($removeLabel->getOrganizerId()); |
176
|
|
|
|
177
|
|
|
$organizer->removeLabel($this->createLabel($removeLabel)); |
178
|
|
|
|
179
|
|
|
$this->organizerRepository->save($organizer); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param ImportLabels $importLabels |
184
|
|
|
*/ |
185
|
|
|
protected function importLabels(ImportLabels $importLabels) |
186
|
|
|
{ |
187
|
|
|
$organizer = $this->loadOrganizer($importLabels->getOrganizerId()); |
188
|
|
|
|
189
|
|
|
$organizer->importLabels($importLabels->getLabels()); |
190
|
|
|
|
191
|
|
|
$this->organizerRepository->save($organizer); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param AbstractLabelCommand $labelCommand |
196
|
|
|
* @return Label |
197
|
|
|
*/ |
198
|
|
View Code Duplication |
private function createLabel(AbstractLabelCommand $labelCommand) |
|
|
|
|
199
|
|
|
{ |
200
|
|
|
$labelName = new StringLiteral((string) $labelCommand->getLabel()); |
201
|
|
|
$label = $this->labelRepository->getByName($labelName); |
202
|
|
|
|
203
|
|
|
return new Label( |
204
|
|
|
$labelName->toNative(), |
205
|
|
|
$label->getVisibility() === Visibility::VISIBLE() |
206
|
|
|
); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param DeleteOrganizer $deleteOrganizer |
211
|
|
|
*/ |
212
|
|
|
public function deleteOrganizer(DeleteOrganizer $deleteOrganizer) |
213
|
|
|
{ |
214
|
|
|
$id = $deleteOrganizer->getOrganizerId(); |
215
|
|
|
|
216
|
|
|
// First remove all relations to the given organizer. |
217
|
|
|
foreach ($this->organizerRelationServices as $relationService) { |
218
|
|
|
$relationService->deleteOrganizer($id); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
// Delete the organizer itself. |
222
|
|
|
$organizer = $this->loadOrganizer($id); |
223
|
|
|
|
224
|
|
|
$organizer->delete(); |
225
|
|
|
|
226
|
|
|
$this->organizerRepository->save($organizer); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Makes it easier to type hint to Organizer. |
231
|
|
|
* |
232
|
|
|
* @param string $id |
233
|
|
|
* @return Organizer |
234
|
|
|
*/ |
235
|
|
|
protected function loadOrganizer($id) |
236
|
|
|
{ |
237
|
|
|
return $this->organizerRepository->load($id); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
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.