Completed
Pull Request — master (#239)
by Luc
04:44
created

OrganizerLDProjector::guardLabelRepository()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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