Completed
Pull Request — master (#306)
by Luc
04:30
created

OrganizerLDProjector::applyTitleTranslated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\Organizer;
4
5
use Broadway\Domain\DateTime;
6
use Broadway\Domain\DomainMessage;
7
use Broadway\EventHandling\EventBusInterface;
8
use CultuurNet\UDB3\Actor\ActorLDProjector;
9
use CultuurNet\UDB3\Cdb\ActorItemFactory;
10
use CultuurNet\UDB3\Event\ReadModel\DocumentGoneException;
11
use CultuurNet\UDB3\Event\ReadModel\DocumentRepositoryInterface;
12
use CultuurNet\UDB3\Iri\IriGeneratorInterface;
13
use CultuurNet\UDB3\Label;
14
use CultuurNet\UDB3\Language;
15
use CultuurNet\UDB3\Organizer\Events\AddressUpdated;
16
use CultuurNet\UDB3\Organizer\Events\ContactPointUpdated;
17
use CultuurNet\UDB3\Organizer\Events\LabelAdded;
18
use CultuurNet\UDB3\Organizer\Events\LabelRemoved;
19
use CultuurNet\UDB3\Organizer\Events\OrganizerCreated;
20
use CultuurNet\UDB3\Organizer\Events\OrganizerCreatedWithUniqueWebsite;
21
use CultuurNet\UDB3\Organizer\Events\OrganizerDeleted;
22
use CultuurNet\UDB3\Organizer\Events\OrganizerImportedFromUDB2;
23
use CultuurNet\UDB3\Organizer\Events\OrganizerUpdatedFromUDB2;
24
use CultuurNet\UDB3\Organizer\Events\TitleTranslated;
25
use CultuurNet\UDB3\Organizer\Events\TitleUpdated;
26
use CultuurNet\UDB3\Organizer\Events\WebsiteUpdated;
27
use CultuurNet\UDB3\Organizer\ReadModel\JSONLD\CdbXMLImporter;
28
use CultuurNet\UDB3\ReadModel\JsonDocument;
29
30
class OrganizerLDProjector extends ActorLDProjector
31
{
32
    /**
33
     * @var CdbXMLImporter
34
     */
35
    private $cdbXMLImporter;
36
37
    /**
38
     * @param DocumentRepositoryInterface $repository
39
     * @param IriGeneratorInterface $iriGenerator
40
     * @param EventBusInterface $eventBus
41
     */
42
    public function __construct(
43
        DocumentRepositoryInterface $repository,
44
        IriGeneratorInterface $iriGenerator,
45
        EventBusInterface $eventBus
46
    ) {
47
        parent::__construct(
48
            $repository,
49
            $iriGenerator,
50
            $eventBus
51
        );
52
53
        $this->cdbXMLImporter = new CdbXMLImporter();
54
    }
55
56
    /**
57
     * @param OrganizerImportedFromUDB2 $organizerImportedFromUDB2
58
     */
59 View Code Duplication
    public function applyOrganizerImportedFromUDB2(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
60
        OrganizerImportedFromUDB2 $organizerImportedFromUDB2
61
    ) {
62
        $udb2Actor = ActorItemFactory::createActorFromCdbXml(
63
            $organizerImportedFromUDB2->getCdbXmlNamespaceUri(),
64
            $organizerImportedFromUDB2->getCdbXml()
65
        );
66
67
        $document = $this->newDocument($organizerImportedFromUDB2->getActorId());
68
        $actorLd = $document->getBody();
69
70
        $actorLd = $this->cdbXMLImporter->documentWithCdbXML(
71
            $actorLd,
72
            $udb2Actor
73
        );
74
75
        $this->repository->save($document->withBody($actorLd));
76
    }
77
78
    /**
79
     * @param OrganizerCreated $organizerCreated
80
     * @param DomainMessage $domainMessage
81
     */
82
    protected function applyOrganizerCreated(OrganizerCreated $organizerCreated, DomainMessage $domainMessage)
83
    {
84
        $document = $this->newDocument($organizerCreated->getOrganizerId());
85
86
        $jsonLD = $document->getBody();
87
88
        $jsonLD->{'@id'} = $this->iriGenerator->iri(
89
            $organizerCreated->getOrganizerId()
90
        );
91
92
        $jsonLD->name['nl'] = $organizerCreated->getTitle();
93
94
        $addresses = $organizerCreated->getAddresses();
95
        $jsonLD->addresses = array();
96
        foreach ($addresses as $address) {
97
            $jsonLD->addresses[] = array(
98
                'addressCountry' => $address->getCountry(),
99
                'addressLocality' => $address->getLocality(),
100
                'postalCode' => $address->getPostalCode(),
101
                'streetAddress' => $address->getStreetAddress(),
102
            );
103
        }
104
105
        $jsonLD->phone = $organizerCreated->getPhones();
106
        $jsonLD->email = $organizerCreated->getEmails();
107
        $jsonLD->url = $organizerCreated->getUrls();
108
109
        $recordedOn = $domainMessage->getRecordedOn()->toString();
110
        $jsonLD->created = \DateTime::createFromFormat(
111
            DateTime::FORMAT_STRING,
112
            $recordedOn
113
        )->format('c');
114
115
        $metaData = $domainMessage->getMetadata()->serialize();
116 View Code Duplication
        if (isset($metaData['user_id']) && isset($metaData['user_nick'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
117
            $jsonLD->creator = "{$metaData['user_id']} ({$metaData['user_nick']})";
118
        }
119
120
        $this->repository->save($document->withBody($jsonLD));
121
    }
122
123
    /**
124
     * @param OrganizerCreatedWithUniqueWebsite $organizerCreated
125
     * @param DomainMessage $domainMessage
126
     */
127
    protected function applyOrganizerCreatedWithUniqueWebsite(
128
        OrganizerCreatedWithUniqueWebsite $organizerCreated,
129
        DomainMessage $domainMessage
130
    ) {
131
        $document = $this->newDocument($organizerCreated->getOrganizerId());
132
133
        $jsonLD = $document->getBody();
134
135
        $jsonLD->{'@id'} = $this->iriGenerator->iri(
136
            $organizerCreated->getOrganizerId()
137
        );
138
139
        $jsonLD->url = (string) $organizerCreated->getWebsite();
140
        $jsonLD->name = $organizerCreated->getTitle();
141
142
        $recordedOn = $domainMessage->getRecordedOn()->toString();
143
        $jsonLD->created = \DateTime::createFromFormat(
144
            DateTime::FORMAT_STRING,
145
            $recordedOn
146
        )->format('c');
147
148
        $metaData = $domainMessage->getMetadata()->serialize();
149 View Code Duplication
        if (isset($metaData['user_id']) && isset($metaData['user_nick'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
150
            $jsonLD->creator = "{$metaData['user_id']} ({$metaData['user_nick']})";
151
        }
152
153
        $this->repository->save($document->withBody($jsonLD));
154
    }
155
156
    /**
157
     * @param WebsiteUpdated $websiteUpdated
158
     */
159
    protected function applyWebsiteUpdated(WebsiteUpdated $websiteUpdated)
160
    {
161
        $organizerId = $websiteUpdated->getOrganizerId();
162
163
        $document = $this->repository->get($organizerId);
164
165
        $jsonLD = $document->getBody();
166
        $jsonLD->url = (string) $websiteUpdated->getWebsite();
167
168
        $this->repository->save($document->withBody($jsonLD));
169
    }
170
171
    /**
172
     * @param TitleUpdated $titleUpdated
173
     */
174
    protected function applyTitleUpdated(TitleUpdated $titleUpdated)
175
    {
176
        $this->applyTitle($titleUpdated, new Language('nl'));
177
    }
178
179
    /**
180
     * @param TitleTranslated $titleTranslated
181
     */
182
    protected function applyTitleTranslated(TitleTranslated $titleTranslated)
183
    {
184
        $this->applyTitle($titleTranslated, $titleTranslated->getLanguage());
185
    }
186
187
    /**
188
     * @param TitleUpdated $titleUpdated
189
     * @param Language $language
190
     */
191 View Code Duplication
    private function applyTitle(TitleUpdated $titleUpdated, Language $language)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
192
    {
193
        $organizerId = $titleUpdated->getOrganizerId();
194
195
        $document = $this->repository->get($organizerId);
196
197
        $jsonLD = $document->getBody();
198
        $jsonLD->name->{$language->getCode()} = $titleUpdated->getTitle()->toNative();
199
200
        $this->repository->save($document->withBody($jsonLD));
201
    }
202
203
    /**
204
     * @param AddressUpdated $addressUpdated
205
     */
206 View Code Duplication
    protected function applyAddressUpdated(AddressUpdated $addressUpdated)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
207
    {
208
        $organizerId = $addressUpdated->getOrganizerId();
209
        $address = $addressUpdated->getAddress();
210
211
        $document = $this->repository->get($organizerId);
212
213
        $jsonLD = $document->getBody();
214
        $jsonLD->address = $address->toJsonLd();
215
216
        $this->repository->save($document->withBody($jsonLD));
217
    }
218
219
    /**
220
     * @param ContactPointUpdated $contactPointUpdated
221
     */
222 View Code Duplication
    protected function applyContactPointUpdated(ContactPointUpdated $contactPointUpdated)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
223
    {
224
        $organizerId = $contactPointUpdated->getOrganizerId();
225
        $contactPoint = $contactPointUpdated->getContactPoint();
226
227
        $document = $this->repository->get($organizerId);
228
229
        $jsonLD = $document->getBody();
230
        $jsonLD->contactPoint = $contactPoint->toJsonLd();
231
232
        $this->repository->save($document->withBody($jsonLD));
233
    }
234
235
    /**
236
     * @param OrganizerUpdatedFromUDB2 $organizerUpdatedFromUDB2
237
     */
238 View Code Duplication
    public function applyOrganizerUpdatedFromUDB2(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
239
        OrganizerUpdatedFromUDB2 $organizerUpdatedFromUDB2
240
    ) {
241
        // It's possible that an organizer has been deleted in udb3, but never
242
        // in udb2. If an update comes for that organizer from udb2, it should
243
        // be imported again. This is intended by design.
244
        // @see https://jira.uitdatabank.be/browse/III-1092
245
        try {
246
            $document = $this->loadDocumentFromRepository(
247
                $organizerUpdatedFromUDB2
248
            );
249
        } catch (DocumentGoneException $e) {
250
            $document = $this->newDocument($organizerUpdatedFromUDB2->getActorId());
251
        }
252
253
        $udb2Actor = ActorItemFactory::createActorFromCdbXml(
254
            $organizerUpdatedFromUDB2->getCdbXmlNamespaceUri(),
255
            $organizerUpdatedFromUDB2->getCdbXml()
256
        );
257
258
        $actorLd = $this->cdbXMLImporter->documentWithCdbXML(
259
            $document->getBody(),
260
            $udb2Actor
261
        );
262
263
        $this->repository->save($document->withBody($actorLd));
264
    }
265
266
    /**
267
     * @param LabelAdded $labelAdded
268
     */
269 View Code Duplication
    public function applyLabelAdded(LabelAdded $labelAdded)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
270
    {
271
        $document = $this->repository->get($labelAdded->getOrganizerId());
272
273
        $jsonLD = $document->getBody();
274
275
        // Check the visibility of the label to update the right property.
276
        $labelsProperty = $labelAdded->getLabel()->isVisible() ? 'labels' : 'hiddenLabels';
277
278
        $labels = isset($jsonLD->{$labelsProperty}) ? $jsonLD->{$labelsProperty} : [];
279
        $label = (string) $labelAdded->getLabel();
280
281
        $labels[] = $label;
282
        $jsonLD->{$labelsProperty} = array_unique($labels);
283
284
        $this->repository->save($document->withBody($jsonLD));
285
    }
286
287
    /**
288
     * @param LabelRemoved $labelRemoved
289
     */
290 View Code Duplication
    public function applyLabelRemoved(LabelRemoved $labelRemoved)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
291
    {
292
        $document = $this->repository->get($labelRemoved->getOrganizerId());
293
        $jsonLD = $document->getBody();
294
295
        // Don't presume that the label visibility is correct when removing.
296
        // So iterate over both the visible and invisible labels.
297
        $labelsProperties = ['labels', 'hiddenLabels'];
298
299
        foreach ($labelsProperties as $labelsProperty) {
300
            if (isset($jsonLD->{$labelsProperty}) && is_array($jsonLD->{$labelsProperty})) {
301
                $jsonLD->{$labelsProperty} = array_filter(
302
                    $jsonLD->{$labelsProperty},
303
                    function ($label) use ($labelRemoved) {
304
                        return !$labelRemoved->getLabel()->equals(
305
                            new Label($label)
306
                        );
307
                    }
308
                );
309
310
                // Ensure array keys start with 0 so json_encode() does encode it
311
                // as an array and not as an object.
312
                if (count($jsonLD->{$labelsProperty}) > 0) {
313
                    $jsonLD->{$labelsProperty} = array_values($jsonLD->{$labelsProperty});
314
                } else {
315
                    unset($jsonLD->{$labelsProperty});
316
                }
317
            }
318
        }
319
320
        $this->repository->save($document->withBody($jsonLD));
321
    }
322
323
    /**
324
     * @param OrganizerDeleted $organizerDeleted
325
     */
326
    public function applyOrganizerDeleted(
327
        OrganizerDeleted $organizerDeleted
328
    ) {
329
        $this->repository->remove($organizerDeleted->getOrganizerId());
330
    }
331
332
    /**
333
     * @param string $id
334
     * @return JsonDocument
335
     */
336 View Code Duplication
    protected function newDocument($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
337
    {
338
        $document = new JsonDocument($id);
339
340
        $organizerLd = $document->getBody();
341
        $organizerLd->{'@id'} = $this->iriGenerator->iri($id);
342
        $organizerLd->{'@context'} = '/contexts/organizer';
343
344
        return $document->withBody($organizerLd);
345
    }
346
}
347