Completed
Pull Request — master (#290)
by Luc
05:35
created

OrganizerLDProjector::applyTitleUpdated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
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\Organizer\Events\AddressUpdated;
15
use CultuurNet\UDB3\Organizer\Events\ContactPointUpdated;
16
use CultuurNet\UDB3\Organizer\Events\LabelAdded;
17
use CultuurNet\UDB3\Organizer\Events\LabelRemoved;
18
use CultuurNet\UDB3\Organizer\Events\OrganizerCreated;
19
use CultuurNet\UDB3\Organizer\Events\OrganizerCreatedWithUniqueWebsite;
20
use CultuurNet\UDB3\Organizer\Events\OrganizerDeleted;
21
use CultuurNet\UDB3\Organizer\Events\OrganizerImportedFromUDB2;
22
use CultuurNet\UDB3\Organizer\Events\OrganizerUpdatedFromUDB2;
23
use CultuurNet\UDB3\Organizer\Events\TitleUpdated;
24
use CultuurNet\UDB3\Organizer\Events\WebsiteUpdated;
25
use CultuurNet\UDB3\Organizer\ReadModel\JSONLD\CdbXMLImporter;
26
use CultuurNet\UDB3\ReadModel\JsonDocument;
27
28
class OrganizerLDProjector extends ActorLDProjector
29
{
30
    /**
31
     * @var CdbXMLImporter
32
     */
33
    private $cdbXMLImporter;
34
35
    /**
36
     * @param DocumentRepositoryInterface $repository
37
     * @param IriGeneratorInterface $iriGenerator
38
     * @param EventBusInterface $eventBus
39
     */
40
    public function __construct(
41
        DocumentRepositoryInterface $repository,
42
        IriGeneratorInterface $iriGenerator,
43
        EventBusInterface $eventBus
44
    ) {
45
        parent::__construct(
46
            $repository,
47
            $iriGenerator,
48
            $eventBus
49
        );
50
51
        $this->cdbXMLImporter = new CdbXMLImporter();
52
    }
53
54
    /**
55
     * @param OrganizerImportedFromUDB2 $organizerImportedFromUDB2
56
     */
57 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...
58
        OrganizerImportedFromUDB2 $organizerImportedFromUDB2
59
    ) {
60
        $udb2Actor = ActorItemFactory::createActorFromCdbXml(
61
            $organizerImportedFromUDB2->getCdbXmlNamespaceUri(),
62
            $organizerImportedFromUDB2->getCdbXml()
63
        );
64
65
        $document = $this->newDocument($organizerImportedFromUDB2->getActorId());
66
        $actorLd = $document->getBody();
67
68
        $actorLd = $this->cdbXMLImporter->documentWithCdbXML(
69
            $actorLd,
70
            $udb2Actor
71
        );
72
73
        $this->repository->save($document->withBody($actorLd));
74
    }
75
76
    /**
77
     * @param OrganizerCreated $organizerCreated
78
     * @param DomainMessage $domainMessage
79
     */
80
    protected function applyOrganizerCreated(OrganizerCreated $organizerCreated, DomainMessage $domainMessage)
81
    {
82
        $document = $this->newDocument($organizerCreated->getOrganizerId());
83
84
        $jsonLD = $document->getBody();
85
86
        $jsonLD->{'@id'} = $this->iriGenerator->iri(
87
            $organizerCreated->getOrganizerId()
88
        );
89
90
        $jsonLD->name = $organizerCreated->getTitle();
91
92
        $addresses = $organizerCreated->getAddresses();
93
        $jsonLD->addresses = array();
94
        foreach ($addresses as $address) {
95
            $jsonLD->addresses[] = array(
96
                'addressCountry' => $address->getCountry(),
97
                'addressLocality' => $address->getLocality(),
98
                'postalCode' => $address->getPostalCode(),
99
                'streetAddress' => $address->getStreetAddress(),
100
            );
101
        }
102
103
        $jsonLD->phone = $organizerCreated->getPhones();
104
        $jsonLD->email = $organizerCreated->getEmails();
105
        $jsonLD->url = $organizerCreated->getUrls();
106
107
        $recordedOn = $domainMessage->getRecordedOn()->toString();
108
        $jsonLD->created = \DateTime::createFromFormat(
109
            DateTime::FORMAT_STRING,
110
            $recordedOn
111
        )->format('c');
112
113
        $metaData = $domainMessage->getMetadata()->serialize();
114 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...
115
            $jsonLD->creator = "{$metaData['user_id']} ({$metaData['user_nick']})";
116
        }
117
118
        $this->repository->save($document->withBody($jsonLD));
119
    }
120
121
    /**
122
     * @param OrganizerCreatedWithUniqueWebsite $organizerCreated
123
     * @param DomainMessage $domainMessage
124
     */
125
    protected function applyOrganizerCreatedWithUniqueWebsite(
126
        OrganizerCreatedWithUniqueWebsite $organizerCreated,
127
        DomainMessage $domainMessage
128
    ) {
129
        $document = $this->newDocument($organizerCreated->getOrganizerId());
130
131
        $jsonLD = $document->getBody();
132
133
        $jsonLD->{'@id'} = $this->iriGenerator->iri(
134
            $organizerCreated->getOrganizerId()
135
        );
136
137
        $jsonLD->url = (string) $organizerCreated->getWebsite();
138
        $jsonLD->name = $organizerCreated->getTitle();
139
140
        $recordedOn = $domainMessage->getRecordedOn()->toString();
141
        $jsonLD->created = \DateTime::createFromFormat(
142
            DateTime::FORMAT_STRING,
143
            $recordedOn
144
        )->format('c');
145
146
        $metaData = $domainMessage->getMetadata()->serialize();
147 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...
148
            $jsonLD->creator = "{$metaData['user_id']} ({$metaData['user_nick']})";
149
        }
150
151
        $this->repository->save($document->withBody($jsonLD));
152
    }
153
154
    /**
155
     * @param WebsiteUpdated $websiteUpdated
156
     */
157 View Code Duplication
    protected function applyWebsiteUpdated(WebsiteUpdated $websiteUpdated)
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...
158
    {
159
        $organizerId = $websiteUpdated->getOrganizerId();
160
161
        $document = $this->repository->get($organizerId);
162
163
        $jsonLD = $document->getBody();
164
        $jsonLD->url = (string) $websiteUpdated->getWebsite();
165
166
        $this->repository->save($document->withBody($jsonLD));
167
    }
168
169
    /**
170
     * @param TitleUpdated $titleUpdated
171
     */
172 View Code Duplication
    protected function applyTitleUpdated(TitleUpdated $titleUpdated)
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...
173
    {
174
        $organizerId = $titleUpdated->getOrganizerId();
175
176
        $document = $this->repository->get($organizerId);
177
178
        $jsonLD = $document->getBody();
179
        $jsonLD->name = $titleUpdated->getTitle()->toNative();
180
181
        $this->repository->save($document->withBody($jsonLD));
182
    }
183
184
    /**
185
     * @param AddressUpdated $addressUpdated
186
     */
187 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...
188
    {
189
        $organizerId = $addressUpdated->getOrganizerId();
190
        $address = $addressUpdated->getAddress();
191
192
        $document = $this->repository->get($organizerId);
193
194
        $jsonLD = $document->getBody();
195
        $jsonLD->address = $address->toJsonLd();
196
197
        $this->repository->save($document->withBody($jsonLD));
198
    }
199
200
    /**
201
     * @param ContactPointUpdated $contactPointUpdated
202
     */
203 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...
204
    {
205
        $organizerId = $contactPointUpdated->getOrganizerId();
206
        $contactPoint = $contactPointUpdated->getContactPoint();
207
208
        $document = $this->repository->get($organizerId);
209
210
        $jsonLD = $document->getBody();
211
        $jsonLD->contactPoint = $contactPoint->toJsonLd();
212
213
        $this->repository->save($document->withBody($jsonLD));
214
    }
215
216
    /**
217
     * @param OrganizerUpdatedFromUDB2 $organizerUpdatedFromUDB2
218
     */
219 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...
220
        OrganizerUpdatedFromUDB2 $organizerUpdatedFromUDB2
221
    ) {
222
        // It's possible that an organizer has been deleted in udb3, but never
223
        // in udb2. If an update comes for that organizer from udb2, it should
224
        // be imported again. This is intended by design.
225
        // @see https://jira.uitdatabank.be/browse/III-1092
226
        try {
227
            $document = $this->loadDocumentFromRepository(
228
                $organizerUpdatedFromUDB2
229
            );
230
        } catch (DocumentGoneException $e) {
231
            $document = $this->newDocument($organizerUpdatedFromUDB2->getActorId());
232
        }
233
234
        $udb2Actor = ActorItemFactory::createActorFromCdbXml(
235
            $organizerUpdatedFromUDB2->getCdbXmlNamespaceUri(),
236
            $organizerUpdatedFromUDB2->getCdbXml()
237
        );
238
239
        $actorLd = $this->cdbXMLImporter->documentWithCdbXML(
240
            $document->getBody(),
241
            $udb2Actor
242
        );
243
244
        $this->repository->save($document->withBody($actorLd));
245
    }
246
247
    /**
248
     * @param LabelAdded $labelAdded
249
     */
250 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...
251
    {
252
        $document = $this->repository->get($labelAdded->getOrganizerId());
253
254
        $jsonLD = $document->getBody();
255
256
        // Check the visibility of the label to update the right property.
257
        $labelsProperty = $labelAdded->getLabel()->isVisible() ? 'labels' : 'hiddenLabels';
258
259
        $labels = isset($jsonLD->{$labelsProperty}) ? $jsonLD->{$labelsProperty} : [];
260
        $label = (string) $labelAdded->getLabel();
261
262
        $labels[] = $label;
263
        $jsonLD->{$labelsProperty} = array_unique($labels);
264
265
        $this->repository->save($document->withBody($jsonLD));
266
    }
267
268
    /**
269
     * @param LabelRemoved $labelRemoved
270
     */
271 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...
272
    {
273
        $document = $this->repository->get($labelRemoved->getOrganizerId());
274
        $jsonLD = $document->getBody();
275
276
        // Don't presume that the label visibility is correct when removing.
277
        // So iterate over both the visible and invisible labels.
278
        $labelsProperties = ['labels', 'hiddenLabels'];
279
280
        foreach ($labelsProperties as $labelsProperty) {
281
            if (isset($jsonLD->{$labelsProperty}) && is_array($jsonLD->{$labelsProperty})) {
282
                $jsonLD->{$labelsProperty} = array_filter(
283
                    $jsonLD->{$labelsProperty},
284
                    function ($label) use ($labelRemoved) {
285
                        return !$labelRemoved->getLabel()->equals(
286
                            new Label($label)
287
                        );
288
                    }
289
                );
290
291
                // Ensure array keys start with 0 so json_encode() does encode it
292
                // as an array and not as an object.
293
                if (count($jsonLD->{$labelsProperty}) > 0) {
294
                    $jsonLD->{$labelsProperty} = array_values($jsonLD->{$labelsProperty});
295
                } else {
296
                    unset($jsonLD->{$labelsProperty});
297
                }
298
            }
299
        }
300
301
        $this->repository->save($document->withBody($jsonLD));
302
    }
303
304
    /**
305
     * @param OrganizerDeleted $organizerDeleted
306
     */
307
    public function applyOrganizerDeleted(
308
        OrganizerDeleted $organizerDeleted
309
    ) {
310
        $this->repository->remove($organizerDeleted->getOrganizerId());
311
    }
312
313
    /**
314
     * @param string $id
315
     * @return JsonDocument
316
     */
317 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...
318
    {
319
        $document = new JsonDocument($id);
320
321
        $organizerLd = $document->getBody();
322
        $organizerLd->{'@id'} = $this->iriGenerator->iri($id);
323
        $organizerLd->{'@context'} = '/contexts/organizer';
324
325
        return $document->withBody($organizerLd);
326
    }
327
}
328