Completed
Push — master ( d7130a...41d031 )
by Luc
10:22
created

EventLDProjector::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 20
nc 1
nop 9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace CultuurNet\UDB3\Event\ReadModel\JSONLD;
4
5
use Broadway\Domain\DateTime;
6
use Broadway\Domain\DomainMessage;
7
use Broadway\Domain\Metadata;
8
use Broadway\EventHandling\EventListenerInterface;
9
use CultuurNet\UDB3\Cdb\EventItemFactory;
10
use CultuurNet\UDB3\EntityNotFoundException;
11
use CultuurNet\UDB3\Event\Events\AudienceUpdated;
12
use CultuurNet\UDB3\Event\Events\BookingInfoUpdated;
13
use CultuurNet\UDB3\Event\Events\ContactPointUpdated;
14
use CultuurNet\UDB3\Event\Events\DescriptionTranslated;
15
use CultuurNet\UDB3\Event\Events\DescriptionUpdated;
16
use CultuurNet\UDB3\Event\Events\EventCopied;
17
use CultuurNet\UDB3\Event\Events\EventCreated;
18
use CultuurNet\UDB3\Event\Events\EventDeleted;
19
use CultuurNet\UDB3\Event\Events\EventImportedFromUDB2;
20
use CultuurNet\UDB3\Event\Events\EventUpdatedFromUDB2;
21
use CultuurNet\UDB3\Event\Events\ImageAdded;
22
use CultuurNet\UDB3\Event\Events\ImageRemoved;
23
use CultuurNet\UDB3\Event\Events\Image\ImagesImportedFromUDB2;
24
use CultuurNet\UDB3\Event\Events\Image\ImagesUpdatedFromUDB2;
25
use CultuurNet\UDB3\Event\Events\ImageUpdated;
26
use CultuurNet\UDB3\Event\Events\LabelAdded;
27
use CultuurNet\UDB3\Event\Events\LabelRemoved;
28
use CultuurNet\UDB3\Event\Events\MainImageSelected;
29
use CultuurNet\UDB3\Event\Events\MajorInfoUpdated;
30
use CultuurNet\UDB3\Event\Events\Moderation\Approved;
31
use CultuurNet\UDB3\Event\Events\Moderation\FlaggedAsDuplicate;
32
use CultuurNet\UDB3\Event\Events\Moderation\FlaggedAsInappropriate;
33
use CultuurNet\UDB3\Event\Events\Moderation\Published;
34
use CultuurNet\UDB3\Event\Events\Moderation\Rejected;
35
use CultuurNet\UDB3\Event\Events\OrganizerDeleted;
36
use CultuurNet\UDB3\Event\Events\OrganizerUpdated;
37
use CultuurNet\UDB3\Event\Events\PriceInfoUpdated;
38
use CultuurNet\UDB3\Event\Events\TitleTranslated;
39
use CultuurNet\UDB3\Event\Events\TypicalAgeRangeDeleted;
40
use CultuurNet\UDB3\Event\Events\TypicalAgeRangeUpdated;
41
use CultuurNet\UDB3\Event\EventType;
42
use CultuurNet\UDB3\Event\ReadModel\DocumentGoneException;
43
use CultuurNet\UDB3\Event\ReadModel\DocumentRepositoryInterface;
44
use CultuurNet\UDB3\Event\EventServiceInterface;
45
use CultuurNet\UDB3\Event\ValueObjects\Audience;
46
use CultuurNet\UDB3\Event\ValueObjects\AudienceType;
47
use CultuurNet\UDB3\Iri\IriGeneratorInterface;
48
use CultuurNet\UDB3\Language;
49
use CultuurNet\UDB3\Offer\AvailableTo;
50
use CultuurNet\UDB3\Offer\IriOfferIdentifierFactoryInterface;
51
use CultuurNet\UDB3\Offer\ReadModel\JSONLD\OfferLDProjector;
52
use CultuurNet\UDB3\Offer\ReadModel\JSONLD\OfferUpdate;
53
use CultuurNet\UDB3\Offer\WorkflowStatus;
54
use CultuurNet\UDB3\Organizer\OrganizerProjectedToJSONLD;
55
use CultuurNet\UDB3\OrganizerService;
56
use CultuurNet\UDB3\Place\Events\PlaceProjectedToJSONLD;
57
use CultuurNet\UDB3\PlaceService;
58
use CultuurNet\UDB3\ReadModel\JsonDocument;
59
use CultuurNet\UDB3\ReadModel\JsonDocumentMetaDataEnricherInterface;
60
use CultuurNet\UDB3\Theme;
61
use Symfony\Component\Serializer\SerializerInterface;
62
use ValueObjects\StringLiteral\StringLiteral;
63
use ValueObjects\Web\Url;
64
65
/**
66
 * Projects state changes on Event entities to a JSON-LD read model in a
67
 * document repository.
68
 *
69
 * Implements PlaceServiceInterface and OrganizerServiceInterface to do a double
70
 * dispatch with CdbXMLImporter.
71
 */
72
class EventLDProjector extends OfferLDProjector implements
73
    EventListenerInterface,
74
    PlaceServiceInterface,
75
    OrganizerServiceInterface
76
{
77
    /**
78
     * @var PlaceService
79
     */
80
    protected $placeService;
81
82
    /**
83
     * @var EventServiceInterface
84
     */
85
    protected $eventService;
86
87
    /**
88
     * @var IriOfferIdentifierFactoryInterface
89
     */
90
    protected $iriOfferIdentifierFactory;
91
92
    /**
93
     * @var CdbXMLImporter
94
     */
95
    protected $cdbXMLImporter;
96
97
    /**
98
     * @param DocumentRepositoryInterface $repository
99
     * @param IriGeneratorInterface $iriGenerator
100
     * @param EventServiceInterface $eventService
101
     * @param PlaceService $placeService
102
     * @param OrganizerService $organizerService
103
     * @param SerializerInterface $mediaObjectSerializer
104
     * @param IriOfferIdentifierFactoryInterface $iriOfferIdentifierFactory
105
     * @param CdbXMLImporter $cdbXMLImporter
106
     * @param JsonDocumentMetaDataEnricherInterface $jsonDocumentMetaDataEnricher
107
     */
108
    public function __construct(
109
        DocumentRepositoryInterface $repository,
110
        IriGeneratorInterface $iriGenerator,
111
        EventServiceInterface $eventService,
112
        PlaceService $placeService,
113
        OrganizerService $organizerService,
114
        SerializerInterface $mediaObjectSerializer,
115
        IriOfferIdentifierFactoryInterface $iriOfferIdentifierFactory,
116
        CdbXMLImporter $cdbXMLImporter,
117
        JsonDocumentMetaDataEnricherInterface $jsonDocumentMetaDataEnricher
118
    ) {
119
        parent::__construct(
120
            $repository,
121
            $iriGenerator,
122
            $organizerService,
123
            $mediaObjectSerializer,
124
            $jsonDocumentMetaDataEnricher
125
        );
126
127
        $this->placeService = $placeService;
128
        $this->eventService = $eventService;
129
        $this->cdbXMLImporter = $cdbXMLImporter;
130
131
        $this->iriOfferIdentifierFactory = $iriOfferIdentifierFactory;
132
    }
133
134
    /**
135
     * @param string $id
136
     * @return JsonDocument
137
     */
138 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...
139
    {
140
        $document = new JsonDocument($id);
141
142
        $offerLd = $document->getBody();
143
        $offerLd->{'@id'} = $this->iriGenerator->iri($id);
144
        $offerLd->{'@context'} = '/contexts/event';
145
146
        return $document->withBody($offerLd);
147
    }
148
149
    protected function applyOrganizerProjectedToJSONLD(OrganizerProjectedToJSONLD $organizerProjectedToJSONLD)
150
    {
151
        $eventIds = $this->eventsOrganizedByOrganizer(
152
            $organizerProjectedToJSONLD->getId()
153
        );
154
155
        $organizer = $this->organizerService->getEntity(
156
            $organizerProjectedToJSONLD->getId()
157
        );
158
159
        $documents = [];
160
161 View Code Duplication
        foreach ($eventIds as $eventId) {
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...
162
            $document = $this->loadDocumentFromRepositoryByItemId(
163
                $eventId
164
            );
165
            $eventLD = $document->getBody();
166
167
            $newEventLD = clone $eventLD;
168
            $newEventLD->organizer = json_decode($organizer);
169
170
            if ($newEventLD != $eventLD) {
171
                $documents[] = $document->withBody($newEventLD);
172
            }
173
        }
174
175
        return $documents;
176
    }
177
178
    protected function applyPlaceProjectedToJSONLD(
179
        PlaceProjectedToJSONLD $placeProjectedToJSONLD
180
    ) {
181
        $identifier = $this->iriOfferIdentifierFactory->fromIri(
182
            Url::fromNative($placeProjectedToJSONLD->getIri())
183
        );
184
185
        $eventsLocatedAtPlace = $this->eventsLocatedAtPlace(
186
            $identifier->getId()
187
        );
188
189
        $placeJSONLD = $this->placeService->getEntity(
190
            $identifier->getId()
191
        );
192
193
        $documents = [];
194
195 View Code Duplication
        foreach ($eventsLocatedAtPlace as $eventId) {
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...
196
            $document = $this->loadDocumentFromRepositoryByItemId(
197
                $eventId
198
            );
199
            $eventLD = $document->getBody();
200
201
            $newEventLD = clone $eventLD;
202
            $newEventLD->location = json_decode($placeJSONLD);
203
204
            if ($newEventLD != $eventLD) {
205
                $documents[] = $document->withBody($newEventLD);
206
            }
207
        }
208
209
        return $documents;
210
    }
211
212
    /**
213
     * @param string $organizerId
214
     * @return string[]
215
     */
216
    protected function eventsOrganizedByOrganizer($organizerId)
217
    {
218
        return $this->eventService->eventsOrganizedByOrganizer(
219
            $organizerId
220
        );
221
    }
222
223
    /**
224
     * @param string $placeId
225
     * @return string[]
226
     */
227
    protected function eventsLocatedAtPlace($placeId)
228
    {
229
        return $this->eventService->eventsLocatedAtPlace(
230
            $placeId
231
        );
232
    }
233
234
    /**
235
     * @param EventImportedFromUDB2 $eventImportedFromUDB2
236
     * @return JsonDocument
237
     */
238
    protected function applyEventImportedFromUDB2(
239
        EventImportedFromUDB2 $eventImportedFromUDB2
240
    ) {
241
        return $this->applyEventCdbXmlFromUDB2(
242
            $eventImportedFromUDB2->getEventId(),
243
            $eventImportedFromUDB2->getCdbXmlNamespaceUri(),
244
            $eventImportedFromUDB2->getCdbXml()
245
        );
246
    }
247
248
    /**
249
     * @param EventUpdatedFromUDB2 $eventUpdatedFromUDB2
250
     * @return JsonDocument
251
     */
252
    protected function applyEventUpdatedFromUDB2(
253
        EventUpdatedFromUDB2 $eventUpdatedFromUDB2
254
    ) {
255
        return $this->applyEventCdbXmlFromUDB2(
256
            $eventUpdatedFromUDB2->getEventId(),
257
            $eventUpdatedFromUDB2->getCdbXmlNamespaceUri(),
258
            $eventUpdatedFromUDB2->getCdbXml()
259
        );
260
    }
261
262
    /**
263
     * Helper function to save JSONLD document from entryapi cdbxml.
264
     *
265
     * @param string $eventId
266
     * @param string $cdbXmlNamespaceUri
267
     * @param string $cdbXml
268
     * @param DomainMessage $domainMessage
269
     * @return JsonDocument
270
     */
271
    protected function applyEventFromCdbXml(
272
        $eventId,
273
        $cdbXmlNamespaceUri,
274
        $cdbXml,
275
        $domainMessage
276
    ) {
277
        $document = $this->newDocument($eventId);
278
279
        $eventLd = $this->projectEventCdbXmlToObject(
280
            $document->getBody(),
281
            $eventId,
282
            $cdbXmlNamespaceUri,
283
            $cdbXml
284
        );
285
286
        // Add creation date and update date from metadata.
287
        $created = $this->getCreated($domainMessage);
288
        $eventLd->created = $created;
289
        $eventLd->modified = $created;
290
291
        // Add creator.
292
        $eventLd->creator = $this->getAuthorFromMetadata($domainMessage->getMetadata())->toNative();
293
294
        // Add publisher, which is the consumer name.
295
        $eventLd->publisher = $this->getConsumerFromMetadata($domainMessage->getMetadata())->toNative();
296
297
        return $document->withBody($eventLd);
298
    }
299
300
    /**
301
     * Helper function to save a JSON-LD document from cdbxml coming from UDB2.
302
     *
303
     * @param string $eventId
304
     * @param string $cdbXmlNamespaceUri
305
     * @param string $cdbXml
306
     *
307
     * @return JsonDocument
308
     */
309
    protected function applyEventCdbXmlFromUDB2(
310
        $eventId,
311
        $cdbXmlNamespaceUri,
312
        $cdbXml
313
    ) {
314
        $document = $this->newDocument($eventId);
315
316
        $eventLd = $this->projectEventCdbXmlToObject(
317
            $document->getBody(),
318
            $eventId,
319
            $cdbXmlNamespaceUri,
320
            $cdbXml
321
        );
322
323
        return $document->withBody($eventLd);
324
    }
325
326
    /**
327
     * @param \stdClass $jsonLd
328
     * @param string $eventId
329
     * @param string $cdbXmlNamespaceUri
330
     * @param string $cdbXml
331
     *
332
     * @return \stdClass
333
     */
334
    protected function projectEventCdbXmlToObject(
335
        \stdClass $jsonLd,
336
        $eventId,
337
        $cdbXmlNamespaceUri,
338
        $cdbXml
339
    ) {
340
        $udb2Event = EventItemFactory::createEventFromCdbXml(
341
            $cdbXmlNamespaceUri,
342
            $cdbXml
343
        );
344
345
        $jsonLd = $this->cdbXMLImporter->documentWithCdbXML(
346
            $jsonLd,
347
            $udb2Event,
348
            $this,
349
            $this,
350
            $this->slugger
351
        );
352
353
        $this->setMainLanguage($jsonLd, new Language('nl'));
354
355
        // Because we can not properly track media coming from UDB2 we simply
356
        // ignore it and give priority to content added through UDB3.
357
        // It's possible that an event has been deleted in udb3, but never
358
        // in udb2. If an update comes for that event from udb2, it should
359
        // be imported again. This is intended by design.
360
        // @see https://jira.uitdatabank.be/browse/III-1092
361
        try {
362
            $document = $this->loadDocumentFromRepositoryByItemId($eventId);
363
        } catch (DocumentGoneException $documentGoneException) {
364
            $document = $this->newDocument($eventId);
365
        }
366
367
        $media = $this->UDB3Media($document);
368
        if (!empty($media)) {
369
            $jsonLd->mediaObject = $media;
370
        }
371
372
        // Because UDB2 cannot keep track of UDB3 places as a location
373
        // ignore it and give priority to content added through UDB3.
374
        $location = $this->UDB3Location($document);
375
        if (!empty($location)) {
376
            $jsonLd->location = $location;
377
        }
378
379
        return $jsonLd;
380
    }
381
382
    /**
383
     * Return the media of an event if it already exists.
384
     *
385
     * @param JsonDocument $document The JsonDocument.
386
     *
387
     * @return array
388
     *  A list of media objects.
389
     */
390
    private function UDB3Media($document)
391
    {
392
        $media = [];
393
394
        if ($document) {
395
            $item = $document->getBody();
396
            // At the moment we do not include any media coming from UDB2.
397
            // If the mediaObject property contains data it's coming from UDB3.
398
            $item->mediaObject = isset($item->mediaObject) ? $item->mediaObject : [];
399
        }
400
401
        return $media;
402
    }
403
404
    /**
405
     * Return the location of an event if it already exists.
406
     *
407
     * @param JsonDocument $document The JsonDocument.
408
     *
409
     * @return array|null
410
     *  The location
411
     */
412
    private function UDB3Location($document)
413
    {
414
        $location = null;
415
416
        if ($document) {
417
            $item = $document->getBody();
418
            $location = isset($item->location) ? $item->location : null;
419
        }
420
421
        return $location;
422
    }
423
424
    /**
425
     * @param EventCreated $eventCreated
426
     * @param DomainMessage $domainMessage
427
     * @return JsonDocument
428
     */
429
    protected function applyEventCreated(
430
        EventCreated $eventCreated,
431
        DomainMessage $domainMessage
432
    ) {
433
        $document = $this->newDocument($eventCreated->getEventId());
434
        $jsonLD = $document->getBody();
435
436
        $jsonLD->{'@id'} = $this->iriGenerator->iri(
437
            $eventCreated->getEventId()
438
        );
439
440
        $this->setMainLanguage($jsonLD, new Language('nl'));
441
442
        $jsonLD->name['nl'] = $eventCreated->getTitle();
443
        $jsonLD->location = array(
444
                '@type' => 'Place',
445
            ) + (array)$this->placeJSONLD(
446
                $eventCreated->getLocation()->getCdbid()
447
            );
448
449
        $calendarJsonLD = $eventCreated->getCalendar()->toJsonLd();
450
        $jsonLD = (object)array_merge((array)$jsonLD, $calendarJsonLD);
451
452
        $availableTo = AvailableTo::createFromCalendar($eventCreated->getCalendar());
453
        $jsonLD->availableTo = (string)$availableTo;
454
455
        // Same as.
456
        $jsonLD->sameAs = $this->generateSameAs(
457
            $eventCreated->getEventId(),
458
            reset($jsonLD->name)
459
        );
460
461
        $eventType = $eventCreated->getEventType();
462
        $jsonLD->terms = [
463
            $eventType->toJsonLd()
464
        ];
465
466
        $theme = $eventCreated->getTheme();
467
        if (!empty($theme)) {
468
            $jsonLD->terms[] = $theme->toJsonLd();
469
        }
470
471
        $created = $this->getCreated($domainMessage);
472
        $jsonLD->created = $created;
473
        $jsonLD->modified = $created;
474
475
        $metaData = $domainMessage->getMetadata()->serialize();
476 View Code Duplication
        if (isset($metaData['user_email'])) {
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...
477
            $jsonLD->creator = $metaData['user_email'];
478
        } elseif (isset($metaData['user_nick'])) {
479
            $jsonLD->creator = $metaData['user_nick'];
480
        }
481
482
        $jsonLD->workflowStatus = WorkflowStatus::DRAFT()->getName();
483
484
        $defaultAudience = new Audience(AudienceType::EVERYONE());
485
        $jsonLD->audience = $defaultAudience->serialize();
486
487
        return $document->withBody($jsonLD);
488
    }
489
490
    /**
491
     * @param EventCopied $eventCopied
492
     * @param DomainMessage $domainMessage
493
     * @return JsonDocument
494
     */
495
    protected function applyEventCopied(
496
        EventCopied $eventCopied,
497
        DomainMessage $domainMessage
498
    ) {
499
        $originalDocument = $this->repository->get($eventCopied->getOriginalEventId());
500
        $eventJsonLD = $originalDocument->getBody();
501
502
        // Set the created and modified date.
503
        $created = $this->getCreated($domainMessage);
504
        $eventJsonLD->created = $created;
505
        $eventJsonLD->modified = $created;
506
507
        // Set the creator.
508
        $eventJsonLD->creator = $this->getAuthorFromMetadata($domainMessage->getMetadata())->toNative();
509
510
        // Set the id.
511
        $eventJsonLD->{'@id'} = $this->iriGenerator->iri($eventCopied->getItemId());
512
513
        // Set the new calendar.
514
        $eventJsonLD = (object) array_merge(
515
            (array) $eventJsonLD,
516
            $eventCopied->getCalendar()->toJsonLd()
517
        );
518
519
        // Set workflow status.
520
        $eventJsonLD->workflowStatus = WorkflowStatus::DRAFT()->getName();
521
522
        // Remove labels.
523
        unset($eventJsonLD->labels);
524
        unset($eventJsonLD->hiddenLabels);
525
526
        // Set available to and from.
527
        $availableTo = AvailableTo::createFromCalendar($eventCopied->getCalendar());
528
        $eventJsonLD->availableTo = (string) $availableTo;
529
        unset($eventJsonLD->availableFrom);
530
531
        $newDocument = new JsonDocument($eventCopied->getItemId());
532
        $newDocument = $newDocument->withBody($eventJsonLD);
533
        return $newDocument;
534
    }
535
536
    /**
537
     * @param EventDeleted $eventDeleted
538
     * @return null
539
     */
540
    protected function applyEventDeleted(EventDeleted $eventDeleted)
541
    {
542
        $this->repository->remove($eventDeleted->getItemId());
543
        return null;
544
    }
545
546
    /**
547
     * Apply the major info updated command to the projector.
548
     * @param MajorInfoUpdated $majorInfoUpdated
549
     * @return JsonDocument
550
     */
551
    protected function applyMajorInfoUpdated(MajorInfoUpdated $majorInfoUpdated)
552
    {
553
        $document = $this
554
            ->loadDocumentFromRepository($majorInfoUpdated)
555
            ->apply(OfferUpdate::calendar($majorInfoUpdated->getCalendar()));
0 ignored issues
show
Compatibility introduced by
$majorInfoUpdated->getCalendar() of type object<CultuurNet\UDB3\CalendarInterface> is not a sub-type of object<CultuurNet\UDB3\Calendar>. It seems like you assume a concrete implementation of the interface CultuurNet\UDB3\CalendarInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
556
557
        $jsonLD = $document->getBody();
558
559
        $jsonLD->name->nl = $majorInfoUpdated->getTitle();
560
        $jsonLD->location = array(
561
          '@type' => 'Place',
562
        ) + (array)$this->placeJSONLD($majorInfoUpdated->getLocation()->getCdbid());
563
564
        $availableTo = AvailableTo::createFromCalendar($majorInfoUpdated->getCalendar());
565
        $jsonLD->availableTo = (string)$availableTo;
566
567
        // Remove old theme and event type.
568
        $jsonLD->terms = array_filter($jsonLD->terms, function ($term) {
569
            return $term->domain !== EventType::DOMAIN &&  $term->domain !== Theme::DOMAIN;
570
        });
571
        $jsonLD->terms = array_values($jsonLD->terms);
572
573
        $eventType = $majorInfoUpdated->getEventType();
574
        $jsonLD->terms[] = $eventType->toJsonLd();
575
576
        $theme = $majorInfoUpdated->getTheme();
577
        if (!empty($theme)) {
578
            $jsonLD->terms[] = $theme->toJsonLd();
579
        }
580
581
        return $document->withBody($jsonLD);
582
    }
583
584
    /**
585
     * @param AudienceUpdated $audienceUpdated
586
     * @return JsonDocument
587
     */
588
    protected function applyAudienceUpdated(AudienceUpdated $audienceUpdated)
589
    {
590
        $document = $this->loadDocumentFromRepository($audienceUpdated);
591
        $jsonLD = $document->getBody();
592
593
        $jsonLD->audience = $audienceUpdated->getAudience()->serialize();
594
595
        return $document->withBody($jsonLD);
596
    }
597
598
    /**
599
     * @inheritdoc
600
     */
601
    public function placeJSONLD($placeId)
602
    {
603
        if (empty($placeId)) {
604
            return array();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(); (array) is incompatible with the return type declared by the interface CultuurNet\UDB3\Event\Re...eInterface::placeJSONLD of type stdClass.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
605
        }
606
607
        try {
608
            $placeJSONLD = $this->placeService->getEntity(
609
                $placeId
610
            );
611
612
            return json_decode($placeJSONLD);
613
        } catch (EntityNotFoundException $e) {
614
            // In case the place can not be found at the moment, just add its ID
615
            return array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('@id' => $t...ervice->iri($placeId)); (array<string,string>) is incompatible with the return type declared by the interface CultuurNet\UDB3\Event\Re...eInterface::placeJSONLD of type stdClass.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
616
                '@id' => $this->placeService->iri($placeId)
617
            );
618
        } catch (DocumentGoneException $e) {
619
            // In case the place can not be found at the moment, just add its ID
620
            return array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('@id' => $t...ervice->iri($placeId)); (array<string,string>) is incompatible with the return type declared by the interface CultuurNet\UDB3\Event\Re...eInterface::placeJSONLD of type stdClass.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
621
                '@id' => $this->placeService->iri($placeId)
622
            );
623
        }
624
    }
625
626
    private function generateSameAs($eventId, $name)
627
    {
628
        $eventSlug = $this->slugger->slug($name);
629
        return array(
630
            'http://www.uitinvlaanderen.be/agenda/e/' . $eventSlug . '/' . $eventId,
631
        );
632
    }
633
634 View Code Duplication
    private function getAuthorFromMetadata(Metadata $metadata)
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...
635
    {
636
        $properties = $metadata->serialize();
637
638
        if (isset($properties['user_nick'])) {
639
            return new StringLiteral($properties['user_nick']);
640
        }
641
    }
642
643 View Code Duplication
    private function getConsumerFromMetadata(Metadata $metadata)
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...
644
    {
645
        $properties = $metadata->serialize();
646
647
        if (isset($properties['consumer']['name'])) {
648
            return new StringLiteral($properties['consumer']['name']);
649
        }
650
    }
651
652
    /**
653
     * @param DomainMessage $domainMessage
654
     * @return string
655
     */
656
    private function getCreated(DomainMessage $domainMessage)
657
    {
658
        $recordedOn = $domainMessage->getRecordedOn()->toString();
659
660
        return \DateTime::createFromFormat(
661
            DateTime::FORMAT_STRING,
662
            $recordedOn
663
        )->format('c');
664
    }
665
666
    /**
667
     * @return string
668
     */
669
    protected function getLabelAddedClassName()
670
    {
671
        return LabelAdded::class;
672
    }
673
674
    /**
675
     * @return string
676
     */
677
    protected function getLabelRemovedClassName()
678
    {
679
        return LabelRemoved::class;
680
    }
681
682
    /**
683
     * @return string
684
     */
685
    protected function getImageAddedClassName()
686
    {
687
        return ImageAdded::class;
688
    }
689
690
    /**
691
     * @return string
692
     */
693
    protected function getImageRemovedClassName()
694
    {
695
        return ImageRemoved::class;
696
    }
697
698
    /**
699
     * @return string
700
     */
701
    protected function getImageUpdatedClassName()
702
    {
703
        return ImageUpdated::class;
704
    }
705
706
    protected function getMainImageSelectedClassName()
707
    {
708
        return MainImageSelected::class;
709
    }
710
711
    /**
712
     * @return string
713
     */
714
    protected function getTitleTranslatedClassName()
715
    {
716
        return TitleTranslated::class;
717
    }
718
719
    /**
720
     * @return string
721
     */
722
    protected function getDescriptionTranslatedClassName()
723
    {
724
        return DescriptionTranslated::class;
725
    }
726
727
    /**
728
     * @return string
729
     */
730
    protected function getOrganizerUpdatedClassName()
731
    {
732
        return OrganizerUpdated::class;
733
    }
734
735
    /**
736
     * @return string
737
     */
738
    protected function getOrganizerDeletedClassName()
739
    {
740
        return OrganizerDeleted::class;
741
    }
742
743
    protected function getBookingInfoUpdatedClassName()
744
    {
745
        return BookingInfoUpdated::class;
746
    }
747
748
    /**
749
     * @return string
750
     */
751
    protected function getPriceInfoUpdatedClassName()
752
    {
753
        return PriceInfoUpdated::class;
754
    }
755
756
    protected function getContactPointUpdatedClassName()
757
    {
758
        return ContactPointUpdated::class;
759
    }
760
761
    protected function getDescriptionUpdatedClassName()
762
    {
763
        return DescriptionUpdated::class;
764
    }
765
766
    protected function getTypicalAgeRangeUpdatedClassName()
767
    {
768
        return TypicalAgeRangeUpdated::class;
769
    }
770
771
    protected function getTypicalAgeRangeDeletedClassName()
772
    {
773
        return TypicalAgeRangeDeleted::class;
774
    }
775
776
    protected function getPublishedClassName()
777
    {
778
        return Published::class;
779
    }
780
781
    protected function getApprovedClassName()
782
    {
783
        return Approved::class;
784
    }
785
786
    protected function getRejectedClassName()
787
    {
788
        return Rejected::class;
789
    }
790
791
    protected function getFlaggedAsDuplicateClassName()
792
    {
793
        return FlaggedAsDuplicate::class;
794
    }
795
796
    protected function getFlaggedAsInappropriateClassName()
797
    {
798
        return FlaggedAsInappropriate::class;
799
    }
800
801
    protected function getImagesImportedFromUdb2ClassName()
802
    {
803
        return ImagesImportedFromUDB2::class;
804
    }
805
806
    protected function getImagesUpdatedFromUdb2ClassName()
807
    {
808
        return ImagesUpdatedFromUDB2::class;
809
    }
810
}
811