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\WriteRepositoryInterface; |
14
|
|
|
use CultuurNet\UDB3\Label\ValueObjects\LabelName; |
15
|
|
|
use CultuurNet\UDB3\Label\ValueObjects\RelationType; |
16
|
|
|
use CultuurNet\UDB3\Offer\Events\AbstractLabelEvent as OfferAbstractLabelEvent; |
17
|
|
|
use CultuurNet\UDB3\Organizer\Events\AbstractLabelEvent as OrganizerAbstractLabelEvent; |
18
|
|
|
use CultuurNet\UDB3\Organizer\Events\OrganizerImportedFromUDB2; |
19
|
|
|
use CultuurNet\UDB3\Organizer\Events\OrganizerUpdatedFromUDB2; |
20
|
|
|
use CultuurNet\UDB3\Place\Events\PlaceImportedFromUDB2; |
21
|
|
|
use CultuurNet\UDB3\Place\Events\PlaceUpdatedFromUDB2; |
22
|
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
23
|
|
|
use ValueObjects\String\String as StringLiteral; |
24
|
|
|
|
25
|
|
|
class Projector extends AbstractProjector |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var WriteRepositoryInterface |
29
|
|
|
*/ |
30
|
|
|
private $writeRepository; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var LabelEventRelationTypeResolverInterface |
34
|
|
|
*/ |
35
|
|
|
private $offerTypeResolver; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Projector constructor. |
39
|
|
|
* @param WriteRepositoryInterface $writeRepository |
40
|
|
|
* @param LabelEventRelationTypeResolverInterface $labelEventOfferTypeResolver |
41
|
|
|
*/ |
42
|
|
|
public function __construct( |
43
|
|
|
WriteRepositoryInterface $writeRepository, |
44
|
|
|
LabelEventRelationTypeResolverInterface $labelEventOfferTypeResolver |
45
|
|
|
) { |
46
|
|
|
$this->writeRepository = $writeRepository; |
47
|
|
|
$this->offerTypeResolver = $labelEventOfferTypeResolver; |
48
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritdoc |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function applyLabelAdded($labelAdded, Metadata $metadata) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$LabelRelation = $this->createLabelRelation($labelAdded); |
57
|
|
|
|
58
|
|
|
try { |
59
|
|
|
if (!is_null($LabelRelation)) { |
60
|
|
|
$this->writeRepository->save( |
61
|
|
|
$LabelRelation->getLabelName(), |
62
|
|
|
$LabelRelation->getRelationType(), |
63
|
|
|
$LabelRelation->getRelationId() |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
} catch (UniqueConstraintViolationException $exception) { |
67
|
|
|
// By design to catch unique exception. |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @inheritdoc |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
public function applyLabelDeleted($labelDeleted, Metadata $metadata) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$labelRelation = $this->createLabelRelation($labelDeleted); |
77
|
|
|
|
78
|
|
|
if (!is_null($labelRelation)) { |
79
|
|
|
$this->writeRepository->deleteByLabelNameAndRelationId( |
80
|
|
|
$labelRelation->getLabelName(), |
81
|
|
|
$labelRelation->getRelationId() |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param EventImportedFromUDB2 $eventImportedFromUDB2 |
88
|
|
|
*/ |
89
|
|
|
public function applyEventImportedFromUDB2( |
90
|
|
|
EventImportedFromUDB2 $eventImportedFromUDB2 |
91
|
|
|
) { |
92
|
|
|
$event = EventItemFactory::createEventFromCdbXml( |
93
|
|
|
$eventImportedFromUDB2->getCdbXmlNamespaceUri(), |
94
|
|
|
$eventImportedFromUDB2->getCdbXml() |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
$this->updateLabelRelationFromCdbItem($event, RelationType::EVENT()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param PlaceImportedFromUDB2 $placeImportedFromUDB2 |
102
|
|
|
*/ |
103
|
|
|
public function applyPlaceImportedFromUDB2( |
104
|
|
|
PlaceImportedFromUDB2 $placeImportedFromUDB2 |
105
|
|
|
) { |
106
|
|
|
$place = ActorItemFactory::createActorFromCdbXml( |
107
|
|
|
$placeImportedFromUDB2->getCdbXmlNamespaceUri(), |
108
|
|
|
$placeImportedFromUDB2->getCdbXml() |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$this->updateLabelRelationFromCdbItem($place, RelationType::PLACE()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param OrganizerImportedFromUDB2 $organizerImportedFromUDB2 |
116
|
|
|
*/ |
117
|
|
|
public function applyOrganizerImportedFromUDB2( |
118
|
|
|
OrganizerImportedFromUDB2 $organizerImportedFromUDB2 |
119
|
|
|
) { |
120
|
|
|
$organizer = ActorItemFactory::createActorFromCdbXml( |
121
|
|
|
$organizerImportedFromUDB2->getCdbXmlNamespaceUri(), |
122
|
|
|
$organizerImportedFromUDB2->getCdbXml() |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
$this->updateLabelRelationFromCdbItem($organizer, RelationType::ORGANIZER()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param EventUpdatedFromUDB2 $eventUpdatedFromUDB2 |
130
|
|
|
*/ |
131
|
|
|
public function applyEventUpdatedFromUDB2( |
132
|
|
|
EventUpdatedFromUDB2 $eventUpdatedFromUDB2 |
133
|
|
|
) { |
134
|
|
|
$event = EventItemFactory::createEventFromCdbXml( |
135
|
|
|
$eventUpdatedFromUDB2->getCdbXmlNamespaceUri(), |
136
|
|
|
$eventUpdatedFromUDB2->getCdbXml() |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
$this->updateLabelRelationFromCdbItem($event, RelationType::EVENT()); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param PlaceUpdatedFromUDB2 $placeUpdatedFromUDB2 |
144
|
|
|
*/ |
145
|
|
|
public function applyPlaceUpdatedFromUDB2( |
146
|
|
|
PlaceUpdatedFromUDB2 $placeUpdatedFromUDB2 |
147
|
|
|
) { |
148
|
|
|
$place = ActorItemFactory::createActorFromCdbXml( |
149
|
|
|
$placeUpdatedFromUDB2->getCdbXmlNamespaceUri(), |
150
|
|
|
$placeUpdatedFromUDB2->getCdbXml() |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
$this->updateLabelRelationFromCdbItem($place, RelationType::PLACE()); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param OrganizerUpdatedFromUDB2 $organizerUpdatedFromUDB2 |
158
|
|
|
*/ |
159
|
|
|
public function applyOrganizerUpdatedFromUDB2( |
160
|
|
|
OrganizerUpdatedFromUDB2 $organizerUpdatedFromUDB2 |
161
|
|
|
) { |
162
|
|
|
$organizer = ActorItemFactory::createActorFromCdbXml( |
163
|
|
|
$organizerUpdatedFromUDB2->getCdbXmlNamespaceUri(), |
164
|
|
|
$organizerUpdatedFromUDB2->getCdbXml() |
165
|
|
|
); |
166
|
|
|
|
167
|
|
|
$this->updateLabelRelationFromCdbItem($organizer, RelationType::ORGANIZER()); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param \CultureFeed_Cdb_Item_Base $cdbItem |
172
|
|
|
* @param RelationType $relationType |
173
|
|
|
*/ |
174
|
|
|
private function updateLabelRelationFromCdbItem( |
175
|
|
|
\CultureFeed_Cdb_Item_Base $cdbItem, |
176
|
|
|
RelationType $relationType |
177
|
|
|
) { |
178
|
|
|
$relationId = new StringLiteral($cdbItem->getCdbId()); |
179
|
|
|
|
180
|
|
|
$this->writeRepository->deleteByRelationId($relationId); |
181
|
|
|
|
182
|
|
|
$keywords = $cdbItem->getKeywords(); |
183
|
|
|
foreach ($keywords as $keyword) { |
184
|
|
|
$this->writeRepository->save( |
185
|
|
|
new LabelName($keyword), |
186
|
|
|
$relationType, |
187
|
|
|
$relationId |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param OfferAbstractLabelEvent|OrganizerAbstractLabelEvent $labelEvent |
194
|
|
|
* @return LabelRelation |
195
|
|
|
*/ |
196
|
|
|
private function createLabelRelation($labelEvent) |
197
|
|
|
{ |
198
|
|
|
$labelRelation = null; |
|
|
|
|
199
|
|
|
|
200
|
|
|
$labelName = new LabelName((string) $labelEvent->getLabel()); |
201
|
|
|
$relationType = $this->offerTypeResolver->getRelationType($labelEvent); |
202
|
|
|
if ($labelEvent instanceof OrganizerAbstractLabelEvent) { |
203
|
|
|
$relationId = new StringLiteral($labelEvent->getOrganizerId()); |
204
|
|
|
} else { |
205
|
|
|
$relationId = new StringLiteral($labelEvent->getItemId()); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$labelRelation = new LabelRelation( |
209
|
|
|
$labelName, |
210
|
|
|
$relationType, |
211
|
|
|
$relationId |
212
|
|
|
); |
213
|
|
|
|
214
|
|
|
return $labelRelation; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
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.