Completed
Pull Request — master (#352)
by Luc
06:55
created

Event::getMediaObjects()   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 0
1
<?php
2
3
namespace CultuurNet\UDB3\Event;
4
5
use Broadway\EventSourcing\EventSourcedAggregateRoot;
6
use CultuurNet\Geocoding\Coordinate\Coordinates;
7
use CultuurNet\UDB3\BookingInfo;
8
use CultuurNet\UDB3\Calendar;
9
use CultuurNet\UDB3\CalendarInterface;
10
use CultuurNet\UDB3\Cdb\EventItemFactory;
11
use CultuurNet\UDB3\Cdb\UpdateableWithCdbXmlInterface;
12
use CultuurNet\UDB3\ContactPoint;
13
use CultuurNet\UDB3\Event\Events\AudienceUpdated;
14
use CultuurNet\UDB3\Event\Events\BookingInfoUpdated;
15
use CultuurNet\UDB3\Event\Events\CalendarUpdated;
16
use CultuurNet\UDB3\Event\Events\Concluded;
17
use CultuurNet\UDB3\Event\Events\ContactPointUpdated;
18
use CultuurNet\UDB3\Event\Events\DescriptionTranslated;
19
use CultuurNet\UDB3\Event\Events\DescriptionUpdated;
20
use CultuurNet\UDB3\Event\Events\EventCdbXMLInterface;
21
use CultuurNet\UDB3\Event\Events\EventCopied;
22
use CultuurNet\UDB3\Event\Events\EventCreated;
23
use CultuurNet\UDB3\Event\Events\EventDeleted;
24
use CultuurNet\UDB3\Event\Events\EventImportedFromUDB2;
25
use CultuurNet\UDB3\Event\Events\EventUpdatedFromUDB2;
26
use CultuurNet\UDB3\Event\Events\FacilitiesUpdated;
27
use CultuurNet\UDB3\Event\Events\GeoCoordinatesUpdated;
28
use CultuurNet\UDB3\Event\Events\ImageAdded;
29
use CultuurNet\UDB3\Event\Events\ImageRemoved;
30
use CultuurNet\UDB3\Event\Events\Image\ImagesImportedFromUDB2;
31
use CultuurNet\UDB3\Event\Events\Image\ImagesUpdatedFromUDB2;
32
use CultuurNet\UDB3\Event\Events\ImageUpdated;
33
use CultuurNet\UDB3\Event\Events\LabelAdded;
34
use CultuurNet\UDB3\Event\Events\LabelRemoved;
35
use CultuurNet\UDB3\Event\Events\LabelsImported;
36
use CultuurNet\UDB3\Event\Events\LocationUpdated;
37
use CultuurNet\UDB3\Event\Events\MainImageSelected;
38
use CultuurNet\UDB3\Event\Events\MajorInfoUpdated;
39
use CultuurNet\UDB3\Event\Events\Moderation\Approved;
40
use CultuurNet\UDB3\Event\Events\Moderation\FlaggedAsDuplicate;
41
use CultuurNet\UDB3\Event\Events\Moderation\FlaggedAsInappropriate;
42
use CultuurNet\UDB3\Event\Events\Moderation\Published;
43
use CultuurNet\UDB3\Event\Events\Moderation\Rejected;
44
use CultuurNet\UDB3\Event\Events\OrganizerDeleted;
45
use CultuurNet\UDB3\Event\Events\OrganizerUpdated;
46
use CultuurNet\UDB3\Event\Events\PriceInfoUpdated;
47
use CultuurNet\UDB3\Event\Events\ThemeUpdated;
48
use CultuurNet\UDB3\Event\Events\TitleTranslated;
49
use CultuurNet\UDB3\Event\Events\TitleUpdated;
50
use CultuurNet\UDB3\Event\Events\TypeUpdated;
51
use CultuurNet\UDB3\Event\Events\TypicalAgeRangeDeleted;
52
use CultuurNet\UDB3\Event\Events\TypicalAgeRangeUpdated;
53
use CultuurNet\UDB3\Event\ValueObjects\Audience;
54
use CultuurNet\UDB3\Label;
55
use CultuurNet\UDB3\LabelCollection;
56
use CultuurNet\UDB3\Language;
57
use CultuurNet\UDB3\Location\Location;
58
use CultuurNet\UDB3\Location\LocationId;
59
use CultuurNet\UDB3\Media\ImageCollection;
60
use CultuurNet\UDB3\Media\Image;
61
use CultuurNet\UDB3\Model\ValueObject\Taxonomy\Label\Labels;
62
use CultuurNet\UDB3\Offer\AgeRange;
63
use CultuurNet\UDB3\Offer\Offer;
64
use CultuurNet\UDB3\Offer\WorkflowStatus;
65
use CultuurNet\UDB3\PriceInfo\PriceInfo;
66
use CultuurNet\UDB3\Theme;
67
use CultuurNet\UDB3\Title;
68
use ValueObjects\Identity\UUID;
69
use ValueObjects\Person\Age;
70
use ValueObjects\StringLiteral\StringLiteral;
71
72
class Event extends Offer implements UpdateableWithCdbXmlInterface
73
{
74
    /**
75
     * @var string
76
     */
77
    protected $eventId;
78
79
    /**
80
     * @var Audience
81
     */
82
    private $audience;
83
84
    /**
85
     * @var LocationId
86
     */
87
    private $locationId;
88
89
    /**
90
     * @var boolean
91
     */
92
    private $concluded = false;
93
94
    public function __construct()
95
    {
96
        parent::__construct();
97
    }
98
99
    /**
100
     * Factory method to create a new event.
101
     *
102
     * @param $eventId
103
     * @param Language $mainLanguage
104
     * @param Title $title
105
     * @param EventType $eventType
106
     * @param Location $location
107
     * @param CalendarInterface $calendar
108
     * @param Theme|null $theme
109
     * @param \DateTimeImmutable|null $publicationDate
110
     * @return Event
111
     */
112 View Code Duplication
    public static function create(
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...
113
        $eventId,
114
        Language $mainLanguage,
115
        Title $title,
116
        EventType $eventType,
117
        Location $location,
118
        CalendarInterface $calendar,
119
        Theme $theme = null,
120
        \DateTimeImmutable $publicationDate = null
121
    ) {
122
        $event = new self();
123
124
        $event->apply(
125
            new EventCreated(
126
                $eventId,
127
                $mainLanguage,
128
                $title,
129
                $eventType,
130
                $location,
131
                $calendar,
132
                $theme,
133
                $publicationDate
134
            )
135
        );
136
137
        return $event;
138
    }
139
140
    /**
141
     * @param string $newEventId
142
     * @param CalendarInterface $calendar
143
     *
144
     * @return Event
145
     */
146
    public function copy($newEventId, CalendarInterface $calendar)
147
    {
148
        if ($this->hasUncommittedEvents()) {
149
            throw new \RuntimeException('I refuse to copy, there are uncommitted events present.');
150
        }
151
152
        // The copied event will have a playhead of the original event + 1
153
        $copy = clone $this;
154
155
        $copy->apply(
156
            new EventCopied(
157
                $newEventId,
158
                $this->eventId,
159
                $calendar
160
            )
161
        );
162
163
        return $copy;
164
    }
165
166
    /**
167
     * @param string $eventId
168
     * @param string $cdbXml
169
     * @param string $cdbXmlNamespaceUri
170
     * @return Event
171
     */
172
    public static function importFromUDB2(
173
        $eventId,
174
        $cdbXml,
175
        $cdbXmlNamespaceUri
176
    ) {
177
        $event = new self();
178
        $event->apply(
179
            new EventImportedFromUDB2(
180
                $eventId,
181
                $cdbXml,
182
                $cdbXmlNamespaceUri
183
            )
184
        );
185
186
        return $event;
187
    }
188
189
    /**
190
     * {@inheritdoc}
191
     */
192
    public function getAggregateRootId()
193
    {
194
        return $this->eventId;
195
    }
196
197
    protected function applyEventCreated(EventCreated $eventCreated)
198
    {
199
        $this->eventId = $eventCreated->getEventId();
200
        $this->locationId = new LocationId($eventCreated->getLocation()->getCdbid());
201
        $this->mainLanguage = $eventCreated->getMainLanguage();
202
        $this->workflowStatus = WorkflowStatus::DRAFT();
203
    }
204
205
    /**
206
     * @param EventCopied $eventCopied
207
     */
208
    protected function applyEventCopied(EventCopied $eventCopied)
209
    {
210
        $this->eventId = $eventCopied->getItemId();
211
        $this->workflowStatus = WorkflowStatus::DRAFT();
212
        $this->labels = new LabelCollection();
213
    }
214
215
    protected function applyEventImportedFromUDB2(
216
        EventImportedFromUDB2 $eventImported
217
    ) {
218
        $this->eventId = $eventImported->getEventId();
219
        // When importing from UDB2 the default main language is always 'nl'.
220
        $this->mainLanguage = new Language('nl');
221
        $this->setUDB2Data($eventImported);
222
    }
223
224
    /**
225
     * @param EventUpdatedFromUDB2 $eventUpdated
226
     */
227
    protected function applyEventUpdatedFromUDB2(
228
        EventUpdatedFromUDB2 $eventUpdated
229
    ) {
230
        // Note: when updating from UDB2 never change the main language.
231
        $this->setUDB2Data($eventUpdated);
232
    }
233
234
    /**
235
     * @param EventCdbXMLInterface $eventCdbXML
236
     */
237
    protected function setUDB2Data(
238
        EventCdbXMLInterface $eventCdbXML
239
    ) {
240
        $udb2Event = EventItemFactory::createEventFromCdbXml(
241
            $eventCdbXML->getCdbXmlNamespaceUri(),
242
            $eventCdbXML->getCdbXml()
243
        );
244
245
        // Just clear the facilities.
246
        $this->facilities = [];
247
248
        // Just clear the location id after an import or update.
249
        $this->locationId = null;
250
251
        // Just clear the contact point.
252
        $this->contactPoint = null;
253
254
        // Just clear the calendar.
255
        $this->calendar = null;
256
257
        // Correctly set the age range to avoid issues with deleting age range.
258
        // after an update from UDB2.
259
        $this->typicalAgeRange = new AgeRange(
260
            $udb2Event->getAgeFrom() ? new Age($udb2Event->getAgeFrom()) : null,
261
            $udb2Event->getAgeTo() ? new Age($udb2Event->getAgeTo()) : null
262
        );
263
264
        // Just clear the booking info.
265
        $this->bookingInfo = null;
266
267
        $this->importWorkflowStatus($udb2Event);
268
        $this->labels = LabelCollection::fromKeywords($udb2Event->getKeywords(true));
269
    }
270
271
    /**
272
     * Update the major info.
273
     *
274
     * @param Title $title
275
     * @param EventType $eventType
276
     * @param Location $location
277
     * @param CalendarInterface $calendar
278
     * @param Theme|null $theme
279
     */
280
    public function updateMajorInfo(
281
        Title $title,
282
        EventType $eventType,
283
        Location $location,
284
        CalendarInterface $calendar,
285
        Theme $theme = null
286
    ) {
287
        $this->apply(new MajorInfoUpdated($this->eventId, $title, $eventType, $location, $calendar, $theme));
288
    }
289
290
    /**
291
     * @param LocationId $locationId
292
     */
293
    public function updateLocation(LocationId $locationId)
294
    {
295
        if (is_null($this->locationId) || !$this->locationId->sameValueAs($locationId)) {
296
            $this->apply(new LocationUpdated($this->eventId, $locationId));
297
        }
298
    }
299
300
    /**
301
     * @param LocationUpdated $locationUpdated
302
     */
303
    public function applyLocationUpdated(LocationUpdated $locationUpdated)
304
    {
305
        $this->locationId = $locationUpdated->getLocationId();
306
    }
307
308
    /**
309
     * @param Audience $audience
310
     */
311
    public function updateAudience(
312
        Audience $audience
313
    ) {
314
        if (is_null($this->audience) || !$this->audience->equals($audience)) {
315
            $this->apply(new AudienceUpdated(
316
                $this->eventId,
317
                $audience
318
            ));
319
        }
320
    }
321
322
    /**
323
     * @param AudienceUpdated $audienceUpdated
324
     */
325
    public function applyAudienceUpdated(AudienceUpdated $audienceUpdated)
326
    {
327
        $this->audience= $audienceUpdated->getAudience();
328
    }
329
330
    /**
331
     * @inheritDoc
332
     * @return ImagesImportedFromUDB2
333
     */
334
    protected function createImagesImportedFromUDB2(ImageCollection $images)
335
    {
336
        return new ImagesImportedFromUDB2($this->eventId, $images);
337
    }
338
339
    /**
340
     * @inheritDoc
341
     * @return ImagesUpdatedFromUDB2
342
     */
343
    protected function createImagesUpdatedFromUDB2(ImageCollection $images)
344
    {
345
        return new ImagesUpdatedFromUDB2($this->eventId, $images);
346
    }
347
348
    /**
349
     * @inheritdoc
350
     */
351
    public function updateWithCdbXml($cdbXml, $cdbXmlNamespaceUri)
352
    {
353
        $this->apply(
354
            new EventUpdatedFromUDB2(
355
                $this->eventId,
356
                $cdbXml,
357
                $cdbXmlNamespaceUri
358
            )
359
        );
360
    }
361
362
    /**
363
     * @param Label $label
364
     * @return LabelAdded
365
     */
366
    protected function createLabelAddedEvent(Label $label)
367
    {
368
        return new LabelAdded($this->eventId, $label);
369
    }
370
371
    /**
372
     * @param Label $label
373
     * @return LabelRemoved
374
     */
375
    protected function createLabelRemovedEvent(Label $label)
376
    {
377
        return new LabelRemoved($this->eventId, $label);
378
    }
379
380
    /**
381
     * @inheritdoc
382
     */
383
    protected function createLabelsImportedEvent(Labels $labels)
384
    {
385
        return new LabelsImported($this->eventId, $labels);
386
    }
387
388
    /**
389
     * @param Image $image
390
     * @return ImageAdded
391
     */
392
    protected function createImageAddedEvent(Image $image)
393
    {
394
        return new ImageAdded($this->eventId, $image);
395
    }
396
397
    /**
398
     * @param Image $image
399
     * @return ImageRemoved
400
     */
401
    protected function createImageRemovedEvent(Image $image)
402
    {
403
        return new ImageRemoved($this->eventId, $image);
404
    }
405
406
    /**
407
     * @param UUID $mediaObjectId
408
     * @param StringLiteral $description
409
     * @param StringLiteral $copyrightHolder
410
     * @return ImageUpdated
411
     */
412
    protected function createImageUpdatedEvent(
413
        UUID $mediaObjectId,
414
        StringLiteral $description,
415
        StringLiteral $copyrightHolder
416
    ) {
417
        return new ImageUpdated(
418
            $this->eventId,
419
            $mediaObjectId,
420
            $description,
421
            $copyrightHolder
422
        );
423
    }
424
425
    /**
426
     * @param Image $image
427
     * @return MainImageSelected
428
     */
429
    protected function createMainImageSelectedEvent(Image $image)
430
    {
431
        return new MainImageSelected($this->eventId, $image);
432
    }
433
434
    /**
435
     * @param Language $language
436
     * @param StringLiteral $title
437
     * @return TitleTranslated
438
     */
439
    protected function createTitleTranslatedEvent(Language $language, StringLiteral $title)
440
    {
441
        return new TitleTranslated($this->eventId, $language, $title);
442
    }
443
444
    /**
445
     * @param Title $title
446
     * @return TitleUpdated
447
     */
448
    protected function createTitleUpdatedEvent(Title $title)
449
    {
450
        return new TitleUpdated($this->eventId, $title);
451
    }
452
453
    /**
454
     * @param Language $language
455
     * @param StringLiteral $description
456
     * @return DescriptionTranslated
457
     */
458
    protected function createDescriptionTranslatedEvent(Language $language, StringLiteral $description)
459
    {
460
        return new DescriptionTranslated($this->eventId, $language, $description);
461
    }
462
463
    /**
464
     * @param string $description
465
     * @return DescriptionUpdated
466
     */
467
    protected function createDescriptionUpdatedEvent($description)
468
    {
469
        return new DescriptionUpdated($this->eventId, $description);
470
    }
471
472
    /**
473
     * @inheritdoc
474
     */
475
    protected function createCalendarUpdatedEvent(Calendar $calendar)
476
    {
477
        return new CalendarUpdated($this->eventId, $calendar);
478
    }
479
480
    /**
481
     * @param string $typicalAgeRange
482
     * @return TypicalAgeRangeUpdated
483
     */
484
    protected function createTypicalAgeRangeUpdatedEvent($typicalAgeRange)
485
    {
486
        return new TypicalAgeRangeUpdated($this->eventId, $typicalAgeRange);
0 ignored issues
show
Documentation introduced by
$typicalAgeRange is of type string, but the function expects a object<CultuurNet\UDB3\Offer\AgeRange>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
487
    }
488
489
    /**
490
     * @return TypicalAgeRangeDeleted
491
     */
492
    protected function createTypicalAgeRangeDeletedEvent()
493
    {
494
        return new TypicalAgeRangeDeleted($this->eventId);
495
    }
496
497
    /**
498
     * @param string $organizerId
499
     * @return OrganizerUpdated
500
     */
501
    protected function createOrganizerUpdatedEvent($organizerId)
502
    {
503
        return new OrganizerUpdated($this->eventId, $organizerId);
504
    }
505
506
    /**
507
     * @param string $organizerId
508
     * @return OrganizerDeleted
509
     */
510
    protected function createOrganizerDeletedEvent($organizerId)
511
    {
512
        return new OrganizerDeleted($this->eventId, $organizerId);
513
    }
514
515
    /**
516
     * @param ContactPoint $contactPoint
517
     * @return ContactPointUpdated
518
     */
519
    protected function createContactPointUpdatedEvent(ContactPoint $contactPoint)
520
    {
521
        return new ContactPointUpdated($this->eventId, $contactPoint);
522
    }
523
524
    /**
525
     * @inheritdoc
526
     */
527
    protected function createGeoCoordinatesUpdatedEvent(Coordinates $coordinates)
528
    {
529
        return new GeoCoordinatesUpdated($this->eventId, $coordinates);
530
    }
531
532
    /**
533
     * @param BookingInfo $bookingInfo
534
     * @return BookingInfoUpdated
535
     */
536
    protected function createBookingInfoUpdatedEvent(BookingInfo $bookingInfo)
537
    {
538
        return new BookingInfoUpdated($this->eventId, $bookingInfo);
539
    }
540
541
    /**
542
     * @param PriceInfo $priceInfo
543
     * @return PriceInfoUpdated
544
     */
545
    protected function createPriceInfoUpdatedEvent(PriceInfo $priceInfo)
546
    {
547
        return new PriceInfoUpdated($this->eventId, $priceInfo);
548
    }
549
550
    /**
551
     * @return EventDeleted
552
     */
553
    protected function createOfferDeletedEvent()
554
    {
555
        return new EventDeleted($this->eventId);
556
    }
557
558
    /**
559
     * @inheritdoc
560
     */
561
    protected function createPublishedEvent(\DateTimeInterface $publicationDate)
562
    {
563
        return new Published($this->eventId, $publicationDate);
564
    }
565
566
    /**
567
     * @inheritdoc
568
     */
569
    protected function createApprovedEvent()
570
    {
571
        return new Approved($this->eventId);
572
    }
573
574
    /**
575
     * @inheritdoc
576
     */
577
    protected function createRejectedEvent(StringLiteral $reason)
578
    {
579
        return new Rejected($this->eventId, $reason);
580
    }
581
582
    /**
583
     * @inheritDoc
584
     */
585
    protected function createFlaggedAsDuplicate()
586
    {
587
        return new FlaggedAsDuplicate($this->eventId);
588
    }
589
590
    /**
591
     * @inheritDoc
592
     */
593
    protected function createFlaggedAsInappropriate()
594
    {
595
        return new FlaggedAsInappropriate($this->eventId);
596
    }
597
598
    /**
599
     * @inheritdoc
600
     */
601
    protected function createTypeUpdatedEvent(EventType $type)
602
    {
603
        return new TypeUpdated($this->eventId, $type);
604
    }
605
606
    /**
607
     * @inheritdoc
608
     */
609
    protected function createThemeUpdatedEvent(Theme $theme)
610
    {
611
        return new ThemeUpdated($this->eventId, $theme);
612
    }
613
614
    /**
615
     * @inheritdoc
616
     */
617
    protected function createFacilitiesUpdatedEvent(array $facilities)
618
    {
619
        return new FacilitiesUpdated($this->eventId, $facilities);
620
    }
621
622
    /**
623
     * Use reflection to get check if the aggregate has uncommitted events.
624
     * @return bool
625
     */
626
    private function hasUncommittedEvents()
627
    {
628
        $reflector = new \ReflectionClass(EventSourcedAggregateRoot::class);
629
        $property = $reflector->getProperty('uncommittedEvents');
630
631
        $property->setAccessible(true);
632
        $uncommittedEvents = $property->getValue($this);
633
634
        return !empty($uncommittedEvents);
635
    }
636
637
    public function conclude()
638
    {
639
        if (!$this->concluded) {
640
            $this->apply(new Concluded($this->eventId));
641
        }
642
    }
643
644
    /**
645
     * @param Concluded $concluded
646
     */
647
    protected function applyConcluded(Concluded $concluded)
0 ignored issues
show
Unused Code introduced by
The parameter $concluded is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
648
    {
649
        $this->concluded = true;
650
    }
651
}
652