1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\Label\ReadModels\Relations; |
4
|
|
|
|
5
|
|
|
use Broadway\Domain\Metadata; |
6
|
|
|
use CultuurNet\UDB3\Cdb\ActorItemFactory; |
7
|
|
|
use CultuurNet\UDB3\Cdb\EventItemFactory; |
8
|
|
|
use CultuurNet\UDB3\Event\Events\EventImportedFromUDB2; |
9
|
|
|
use CultuurNet\UDB3\Event\Events\EventUpdatedFromUDB2; |
10
|
|
|
use CultuurNet\UDB3\Label\LabelEventRelationTypeResolverInterface; |
11
|
|
|
use CultuurNet\UDB3\Label\ReadModels\AbstractProjector; |
12
|
|
|
use CultuurNet\UDB3\Label\ReadModels\Relations\Repository\LabelRelation; |
13
|
|
|
use CultuurNet\UDB3\Label\ReadModels\Relations\Repository\ReadRepositoryInterface; |
14
|
|
|
use CultuurNet\UDB3\Label\ReadModels\Relations\Repository\WriteRepositoryInterface; |
15
|
|
|
use CultuurNet\UDB3\Label\ValueObjects\LabelName; |
16
|
|
|
use CultuurNet\UDB3\Label\ValueObjects\RelationType; |
17
|
|
|
use CultuurNet\UDB3\LabelCollection; |
18
|
|
|
use CultuurNet\UDB3\LabelEventInterface; |
19
|
|
|
use CultuurNet\UDB3\LabelsImportedEventInterface; |
20
|
|
|
use CultuurNet\UDB3\Model\ValueObject\Taxonomy\Label\Label; |
21
|
|
|
use CultuurNet\UDB3\Organizer\Events\OrganizerImportedFromUDB2; |
22
|
|
|
use CultuurNet\UDB3\Organizer\Events\OrganizerUpdatedFromUDB2; |
23
|
|
|
use CultuurNet\UDB3\Place\Events\PlaceImportedFromUDB2; |
24
|
|
|
use CultuurNet\UDB3\Place\Events\PlaceUpdatedFromUDB2; |
25
|
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
26
|
|
|
use ValueObjects\StringLiteral\StringLiteral; |
27
|
|
|
|
28
|
|
|
class Projector extends AbstractProjector |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var WriteRepositoryInterface |
32
|
|
|
*/ |
33
|
|
|
private $writeRepository; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ReadRepositoryInterface |
37
|
|
|
*/ |
38
|
|
|
private $readRepository; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var LabelEventRelationTypeResolverInterface |
42
|
|
|
*/ |
43
|
|
|
private $offerTypeResolver; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Projector constructor. |
47
|
|
|
* @param WriteRepositoryInterface $writeRepository |
48
|
|
|
* @param ReadRepositoryInterface $readRepository |
49
|
|
|
* @param LabelEventRelationTypeResolverInterface $labelEventOfferTypeResolver |
50
|
|
|
*/ |
51
|
|
|
public function __construct( |
52
|
|
|
WriteRepositoryInterface $writeRepository, |
53
|
|
|
ReadRepositoryInterface $readRepository, |
54
|
|
|
LabelEventRelationTypeResolverInterface $labelEventOfferTypeResolver |
55
|
|
|
) { |
56
|
|
|
$this->writeRepository = $writeRepository; |
57
|
|
|
$this->readRepository = $readRepository; |
58
|
|
|
$this->offerTypeResolver = $labelEventOfferTypeResolver; |
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritdoc |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
public function applyLabelAdded(LabelEventInterface $labelAdded, Metadata $metadata) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$LabelRelation = $this->createLabelRelation($labelAdded); |
68
|
|
|
|
69
|
|
|
try { |
70
|
|
|
if (!is_null($LabelRelation)) { |
71
|
|
|
$this->writeRepository->save( |
72
|
|
|
$LabelRelation->getLabelName(), |
73
|
|
|
$LabelRelation->getRelationType(), |
74
|
|
|
$LabelRelation->getRelationId(), |
75
|
|
|
false |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
} catch (UniqueConstraintViolationException $exception) { |
79
|
|
|
// By design to catch unique exception. |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritdoc |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function applyLabelRemoved(LabelEventInterface $labelRemoved, Metadata $metadata) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$labelRelation = $this->createLabelRelation($labelRemoved); |
89
|
|
|
|
90
|
|
|
if (!is_null($labelRelation)) { |
91
|
|
|
$this->writeRepository->deleteByLabelNameAndRelationId( |
92
|
|
|
$labelRelation->getLabelName(), |
93
|
|
|
$labelRelation->getRelationId() |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @inheritdoc |
100
|
|
|
*/ |
101
|
|
|
public function applyLabelsImported(LabelsImportedEventInterface $labelsImported, Metadata $metadata) |
102
|
|
|
{ |
103
|
|
|
foreach ($labelsImported->getLabels()->toArray() as $label) { |
104
|
|
|
/** @var Label $label */ |
105
|
|
|
$this->writeRepository->save( |
106
|
|
|
new LabelName($label->getName()->toString()), |
107
|
|
|
$this->offerTypeResolver->getRelationTypeForImport($labelsImported), |
108
|
|
|
new StringLiteral($labelsImported->getItemId()), |
109
|
|
|
true |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param EventImportedFromUDB2 $eventImportedFromUDB2 |
116
|
|
|
*/ |
117
|
|
|
public function applyEventImportedFromUDB2( |
118
|
|
|
EventImportedFromUDB2 $eventImportedFromUDB2 |
119
|
|
|
) { |
120
|
|
|
$event = EventItemFactory::createEventFromCdbXml( |
121
|
|
|
$eventImportedFromUDB2->getCdbXmlNamespaceUri(), |
122
|
|
|
$eventImportedFromUDB2->getCdbXml() |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
$this->updateLabelRelationFromCdbItem($event, RelationType::EVENT()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param PlaceImportedFromUDB2 $placeImportedFromUDB2 |
130
|
|
|
*/ |
131
|
|
|
public function applyPlaceImportedFromUDB2( |
132
|
|
|
PlaceImportedFromUDB2 $placeImportedFromUDB2 |
133
|
|
|
) { |
134
|
|
|
$place = ActorItemFactory::createActorFromCdbXml( |
135
|
|
|
$placeImportedFromUDB2->getCdbXmlNamespaceUri(), |
136
|
|
|
$placeImportedFromUDB2->getCdbXml() |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
$this->updateLabelRelationFromCdbItem($place, RelationType::PLACE()); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param OrganizerImportedFromUDB2 $organizerImportedFromUDB2 |
144
|
|
|
*/ |
145
|
|
|
public function applyOrganizerImportedFromUDB2( |
146
|
|
|
OrganizerImportedFromUDB2 $organizerImportedFromUDB2 |
147
|
|
|
) { |
148
|
|
|
$organizer = ActorItemFactory::createActorFromCdbXml( |
149
|
|
|
$organizerImportedFromUDB2->getCdbXmlNamespaceUri(), |
150
|
|
|
$organizerImportedFromUDB2->getCdbXml() |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
$this->updateLabelRelationFromCdbItem($organizer, RelationType::ORGANIZER()); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param EventUpdatedFromUDB2 $eventUpdatedFromUDB2 |
158
|
|
|
*/ |
159
|
|
|
public function applyEventUpdatedFromUDB2( |
160
|
|
|
EventUpdatedFromUDB2 $eventUpdatedFromUDB2 |
161
|
|
|
) { |
162
|
|
|
$event = EventItemFactory::createEventFromCdbXml( |
163
|
|
|
$eventUpdatedFromUDB2->getCdbXmlNamespaceUri(), |
164
|
|
|
$eventUpdatedFromUDB2->getCdbXml() |
165
|
|
|
); |
166
|
|
|
|
167
|
|
|
$this->updateLabelRelationFromCdbItem($event, RelationType::EVENT()); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param PlaceUpdatedFromUDB2 $placeUpdatedFromUDB2 |
172
|
|
|
*/ |
173
|
|
|
public function applyPlaceUpdatedFromUDB2( |
174
|
|
|
PlaceUpdatedFromUDB2 $placeUpdatedFromUDB2 |
175
|
|
|
) { |
176
|
|
|
$place = ActorItemFactory::createActorFromCdbXml( |
177
|
|
|
$placeUpdatedFromUDB2->getCdbXmlNamespaceUri(), |
178
|
|
|
$placeUpdatedFromUDB2->getCdbXml() |
179
|
|
|
); |
180
|
|
|
|
181
|
|
|
$this->updateLabelRelationFromCdbItem($place, RelationType::PLACE()); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param OrganizerUpdatedFromUDB2 $organizerUpdatedFromUDB2 |
186
|
|
|
*/ |
187
|
|
|
public function applyOrganizerUpdatedFromUDB2( |
188
|
|
|
OrganizerUpdatedFromUDB2 $organizerUpdatedFromUDB2 |
189
|
|
|
) { |
190
|
|
|
$organizer = ActorItemFactory::createActorFromCdbXml( |
191
|
|
|
$organizerUpdatedFromUDB2->getCdbXmlNamespaceUri(), |
192
|
|
|
$organizerUpdatedFromUDB2->getCdbXml() |
193
|
|
|
); |
194
|
|
|
|
195
|
|
|
$this->updateLabelRelationFromCdbItem($organizer, RelationType::ORGANIZER()); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param \CultureFeed_Cdb_Item_Base $cdbItem |
200
|
|
|
* @param RelationType $relationType |
201
|
|
|
*/ |
202
|
|
|
private function updateLabelRelationFromCdbItem( |
203
|
|
|
\CultureFeed_Cdb_Item_Base $cdbItem, |
204
|
|
|
RelationType $relationType |
205
|
|
|
) { |
206
|
|
|
$relationId = new StringLiteral($cdbItem->getCdbId()); |
207
|
|
|
|
208
|
|
|
// Never delete the UDB3 labels on an update. |
209
|
|
|
$this->writeRepository->deleteImportedByRelationId($relationId); |
210
|
|
|
|
211
|
|
|
$keywords = $cdbItem->getKeywords(); |
212
|
|
|
$labelCollection = LabelCollection::fromStrings($keywords); |
213
|
|
|
|
214
|
|
|
// Calculate the UDB2 imported labels. |
215
|
|
|
$udb3Labels = array_map( |
216
|
|
|
function (LabelRelation $labelRelation) { |
217
|
|
|
return $labelRelation->getLabelName()->toNative(); |
218
|
|
|
}, |
219
|
|
|
$this->readRepository->getLabelRelationsForItem($relationId) |
220
|
|
|
); |
221
|
|
|
$udb2Labels = array_udiff( |
222
|
|
|
$labelCollection->asArray(), |
223
|
|
|
$udb3Labels, |
224
|
|
|
'strcasecmp' |
225
|
|
|
); |
226
|
|
|
|
227
|
|
|
// Only save the UDB2 labels, because the UDB3 labels are still present. |
228
|
|
|
foreach ($udb2Labels as $label) { |
229
|
|
|
$this->writeRepository->save( |
230
|
|
|
new LabelName((string) $label), |
231
|
|
|
$relationType, |
232
|
|
|
$relationId, |
233
|
|
|
true |
234
|
|
|
); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param LabelEventInterface $labelEvent |
240
|
|
|
* @return LabelRelation |
241
|
|
|
*/ |
242
|
|
|
private function createLabelRelation(LabelEventInterface $labelEvent) |
243
|
|
|
{ |
244
|
|
|
$labelRelation = null; |
|
|
|
|
245
|
|
|
|
246
|
|
|
$labelName = new LabelName((string) $labelEvent->getLabel()); |
247
|
|
|
$relationType = $this->offerTypeResolver->getRelationType($labelEvent); |
248
|
|
|
$relationId = new StringLiteral($labelEvent->getItemId()); |
249
|
|
|
|
250
|
|
|
$labelRelation = new LabelRelation( |
251
|
|
|
$labelName, |
252
|
|
|
$relationType, |
253
|
|
|
$relationId, |
254
|
|
|
false |
255
|
|
|
); |
256
|
|
|
|
257
|
|
|
return $labelRelation; |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
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.