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