Completed
Push — master ( 5af20f...a965fa )
by Jonas
17s queued 10s
created

Event::createImageRemovedEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Description;
14
use CultuurNet\UDB3\Event\Events\AudienceUpdated;
15
use CultuurNet\UDB3\Event\Events\BookingInfoUpdated;
16
use CultuurNet\UDB3\Event\Events\CalendarUpdated;
17
use CultuurNet\UDB3\Event\Events\Concluded;
18
use CultuurNet\UDB3\Event\Events\ContactPointUpdated;
19
use CultuurNet\UDB3\Event\Events\DescriptionTranslated;
20
use CultuurNet\UDB3\Event\Events\DescriptionUpdated;
21
use CultuurNet\UDB3\Event\Events\EventCdbXMLInterface;
22
use CultuurNet\UDB3\Event\Events\EventCopied;
23
use CultuurNet\UDB3\Event\Events\EventCreated;
24
use CultuurNet\UDB3\Event\Events\EventDeleted;
25
use CultuurNet\UDB3\Event\Events\EventImportedFromUDB2;
26
use CultuurNet\UDB3\Event\Events\EventUpdatedFromUDB2;
27
use CultuurNet\UDB3\Event\Events\FacilitiesUpdated;
28
use CultuurNet\UDB3\Event\Events\GeoCoordinatesUpdated;
29
use CultuurNet\UDB3\Event\Events\ImageAdded;
30
use CultuurNet\UDB3\Event\Events\ImageRemoved;
31
use CultuurNet\UDB3\Event\Events\Image\ImagesImportedFromUDB2;
32
use CultuurNet\UDB3\Event\Events\Image\ImagesUpdatedFromUDB2;
33
use CultuurNet\UDB3\Event\Events\ImageUpdated;
34
use CultuurNet\UDB3\Event\Events\LabelAdded;
35
use CultuurNet\UDB3\Event\Events\LabelRemoved;
36
use CultuurNet\UDB3\Event\Events\LabelsImported;
37
use CultuurNet\UDB3\Event\Events\LocationUpdated;
38
use CultuurNet\UDB3\Event\Events\MainImageSelected;
39
use CultuurNet\UDB3\Event\Events\MajorInfoUpdated;
40
use CultuurNet\UDB3\Event\Events\Moderation\Approved;
41
use CultuurNet\UDB3\Event\Events\Moderation\FlaggedAsDuplicate;
42
use CultuurNet\UDB3\Event\Events\Moderation\FlaggedAsInappropriate;
43
use CultuurNet\UDB3\Event\Events\Moderation\Published;
44
use CultuurNet\UDB3\Event\Events\Moderation\Rejected;
45
use CultuurNet\UDB3\Event\Events\OrganizerDeleted;
46
use CultuurNet\UDB3\Event\Events\OrganizerUpdated;
47
use CultuurNet\UDB3\Event\Events\PriceInfoUpdated;
48
use CultuurNet\UDB3\Event\Events\ThemeUpdated;
49
use CultuurNet\UDB3\Event\Events\TitleTranslated;
50
use CultuurNet\UDB3\Event\Events\TitleUpdated;
51
use CultuurNet\UDB3\Event\Events\TypeUpdated;
52
use CultuurNet\UDB3\Event\Events\TypicalAgeRangeDeleted;
53
use CultuurNet\UDB3\Event\Events\TypicalAgeRangeUpdated;
54
use CultuurNet\UDB3\Event\ValueObjects\Audience;
55
use CultuurNet\UDB3\Event\ValueObjects\AudienceType;
56
use CultuurNet\UDB3\Label;
57
use CultuurNet\UDB3\LabelCollection;
58
use CultuurNet\UDB3\Language;
59
use CultuurNet\UDB3\Event\ValueObjects\LocationId;
60
use CultuurNet\UDB3\Media\ImageCollection;
61
use CultuurNet\UDB3\Media\Image;
62
use CultuurNet\UDB3\Model\ValueObject\Taxonomy\Label\Labels;
63
use CultuurNet\UDB3\Offer\AgeRange;
64
use CultuurNet\UDB3\Offer\Offer;
65
use CultuurNet\UDB3\Offer\WorkflowStatus;
66
use CultuurNet\UDB3\PriceInfo\PriceInfo;
67
use CultuurNet\UDB3\Theme;
68
use CultuurNet\UDB3\Title;
69
use ValueObjects\Identity\UUID;
70
use ValueObjects\Person\Age;
71
use ValueObjects\StringLiteral\StringLiteral;
72
73
class Event extends Offer implements UpdateableWithCdbXmlInterface
74
{
75
    /**
76
     * @var string
77
     */
78
    protected $eventId;
79
80
    /**
81
     * @var Audience
82
     */
83
    private $audience;
84
85
    /**
86
     * @var LocationId
87
     */
88
    private $locationId;
89
90
    /**
91
     * @var boolean
92
     */
93
    private $concluded = false;
94
95
    public function __construct()
96
    {
97
        parent::__construct();
98
    }
99
100
    /**
101
     * Factory method to create a new event.
102
     *
103
     * @param $eventId
104
     * @param Language $mainLanguage
105
     * @param Title $title
106
     * @param EventType $eventType
107
     * @param LocationId $location
108
     * @param CalendarInterface $calendar
109
     * @param Theme|null $theme
110
     * @param \DateTimeImmutable|null $publicationDate
111
     * @return Event
112
     */
113 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...
114
        $eventId,
115
        Language $mainLanguage,
116
        Title $title,
117
        EventType $eventType,
118
        LocationId $location,
119
        CalendarInterface $calendar,
120
        Theme $theme = null,
121
        \DateTimeImmutable $publicationDate = null
122
    ) {
123
        $event = new self();
124
125
        $event->apply(
126
            new EventCreated(
127
                $eventId,
128
                $mainLanguage,
129
                $title,
130
                $eventType,
131
                $location,
132
                $calendar,
133
                $theme,
134
                $publicationDate
135
            )
136
        );
137
138
        return $event;
139
    }
140
141
    /**
142
     * @param string $newEventId
143
     * @param CalendarInterface $calendar
144
     *
145
     * @return Event
146
     */
147
    public function copy($newEventId, CalendarInterface $calendar)
148
    {
149
        if ($this->hasUncommittedEvents()) {
150
            throw new \RuntimeException('I refuse to copy, there are uncommitted events present.');
151
        }
152
153
        // The copied event will have a playhead of the original event + 1
154
        $copy = clone $this;
155
156
        $copy->apply(
157
            new EventCopied(
158
                $newEventId,
159
                $this->eventId,
160
                $calendar
161
            )
162
        );
163
164
        return $copy;
165
    }
166
167
    /**
168
     * @param string $eventId
169
     * @param string $cdbXml
170
     * @param string $cdbXmlNamespaceUri
171
     * @return Event
172
     */
173
    public static function importFromUDB2(
174
        $eventId,
175
        $cdbXml,
176
        $cdbXmlNamespaceUri
177
    ) {
178
        $event = new self();
179
        $event->apply(
180
            new EventImportedFromUDB2(
181
                $eventId,
182
                $cdbXml,
183
                $cdbXmlNamespaceUri
184
            )
185
        );
186
187
        return $event;
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193
    public function getAggregateRootId()
194
    {
195
        return $this->eventId;
196
    }
197
198 View Code Duplication
    protected function applyEventCreated(EventCreated $eventCreated)
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...
199
    {
200
        $this->eventId = $eventCreated->getEventId();
201
        $this->titles[$eventCreated->getMainLanguage()->getCode()] = $eventCreated->getTitle();
202
        $this->calendar = $eventCreated->getCalendar();
203
        $this->audience = new Audience(AudienceType::EVERYONE());
204
        $this->contactPoint = new ContactPoint();
205
        $this->bookingInfo = new BookingInfo();
206
        $this->typeId = $eventCreated->getEventType()->getId();
207
        $this->themeId = $eventCreated->getTheme() ? $eventCreated->getTheme()->getId() : null;
208
        $this->locationId = $eventCreated->getLocation();
209
        $this->mainLanguage = $eventCreated->getMainLanguage();
210
        $this->workflowStatus = WorkflowStatus::DRAFT();
211
    }
212
213
    /**
214
     * @param EventCopied $eventCopied
215
     */
216
    protected function applyEventCopied(EventCopied $eventCopied)
217
    {
218
        $this->eventId = $eventCopied->getItemId();
219
        $this->workflowStatus = WorkflowStatus::DRAFT();
220
        $this->labels = new LabelCollection();
221
    }
222
223
    protected function applyEventImportedFromUDB2(
224
        EventImportedFromUDB2 $eventImported
225
    ) {
226
        $this->eventId = $eventImported->getEventId();
227
        // When importing from UDB2 the default main language is always 'nl'.
228
        $this->mainLanguage = new Language('nl');
229
        $this->setUDB2Data($eventImported);
230
    }
231
232
    /**
233
     * @param EventUpdatedFromUDB2 $eventUpdated
234
     */
235
    protected function applyEventUpdatedFromUDB2(
236
        EventUpdatedFromUDB2 $eventUpdated
237
    ) {
238
        // Note: when updating from UDB2 never change the main language.
239
        $this->setUDB2Data($eventUpdated);
240
    }
241
242
    /**
243
     * @param EventCdbXMLInterface $eventCdbXML
244
     */
245
    protected function setUDB2Data(
246
        EventCdbXMLInterface $eventCdbXML
247
    ) {
248
        $udb2Event = EventItemFactory::createEventFromCdbXml(
249
            $eventCdbXML->getCdbXmlNamespaceUri(),
250
            $eventCdbXML->getCdbXml()
251
        );
252
253
        // Just clear the facilities.
254
        $this->facilities = [];
255
256
        // Just clear the location id after an import or update.
257
        $this->locationId = null;
258
259
        // Just clear the contact point.
260
        $this->contactPoint = null;
261
262
        // Just clear the calendar.
263
        $this->calendar = null;
264
265
        // Correctly set the age range to avoid issues with deleting age range.
266
        // after an update from UDB2.
267
        $this->typicalAgeRange = new AgeRange(
268
            $udb2Event->getAgeFrom() ? new Age($udb2Event->getAgeFrom()) : null,
269
            $udb2Event->getAgeTo() ? new Age($udb2Event->getAgeTo()) : null
270
        );
271
272
        // Just clear the booking info.
273
        $this->bookingInfo = null;
274
275
        // Just clear the price info.
276
        $this->priceInfo = null;
277
278
        $this->importWorkflowStatus($udb2Event);
279
        $this->labels = LabelCollection::fromKeywords($udb2Event->getKeywords(true));
280
    }
281
282
    /**
283
     * Update the major info.
284
     *
285
     * @param Title $title
286
     * @param EventType $eventType
287
     * @param LocationId $location
288
     * @param CalendarInterface $calendar
289
     * @param Theme|null $theme
290
     */
291
    public function updateMajorInfo(
292
        Title $title,
293
        EventType $eventType,
294
        LocationId $location,
295
        CalendarInterface $calendar,
296
        Theme $theme = null
297
    ) {
298
        $this->apply(new MajorInfoUpdated($this->eventId, $title, $eventType, $location, $calendar, $theme));
299
    }
300
301
    /**
302
     * @param LocationId $locationId
303
     */
304
    public function updateLocation(LocationId $locationId)
305
    {
306
        if (is_null($this->locationId) || !$this->locationId->sameValueAs($locationId)) {
307
            $this->apply(new LocationUpdated($this->eventId, $locationId));
308
        }
309
    }
310
311
    /**
312
     * @param LocationUpdated $locationUpdated
313
     */
314
    public function applyLocationUpdated(LocationUpdated $locationUpdated)
315
    {
316
        $this->locationId = $locationUpdated->getLocationId();
317
    }
318
319
    /**
320
     * @param Audience $audience
321
     */
322
    public function updateAudience(
323
        Audience $audience
324
    ) {
325
        $audienceType = $audience->getAudienceType();
326
        if ($this->locationId->isDummyPlaceForEducation() && !$audienceType->sameValueAs(AudienceType::EDUCATION())) {
327
            throw IncompatibleAudienceType::forEvent($this->eventId, $audienceType);
328
        }
329
330
        if (is_null($this->audience) || !$this->audience->equals($audience)) {
331
            $this->apply(new AudienceUpdated(
332
                $this->eventId,
333
                $audience
334
            ));
335
        }
336
    }
337
338
    /**
339
     * @param AudienceUpdated $audienceUpdated
340
     */
341
    public function applyAudienceUpdated(AudienceUpdated $audienceUpdated)
342
    {
343
        $this->audience= $audienceUpdated->getAudience();
344
    }
345
346
    /**
347
     * @inheritDoc
348
     * @return ImagesImportedFromUDB2
349
     */
350
    protected function createImagesImportedFromUDB2(ImageCollection $images)
351
    {
352
        return new ImagesImportedFromUDB2($this->eventId, $images);
353
    }
354
355
    /**
356
     * @inheritDoc
357
     * @return ImagesUpdatedFromUDB2
358
     */
359
    protected function createImagesUpdatedFromUDB2(ImageCollection $images)
360
    {
361
        return new ImagesUpdatedFromUDB2($this->eventId, $images);
362
    }
363
364
    /**
365
     * @inheritdoc
366
     */
367
    public function updateWithCdbXml($cdbXml, $cdbXmlNamespaceUri)
368
    {
369
        $this->apply(
370
            new EventUpdatedFromUDB2(
371
                $this->eventId,
372
                $cdbXml,
373
                $cdbXmlNamespaceUri
374
            )
375
        );
376
    }
377
378
    /**
379
     * @param Label $label
380
     * @return LabelAdded
381
     */
382
    protected function createLabelAddedEvent(Label $label)
383
    {
384
        return new LabelAdded($this->eventId, $label);
385
    }
386
387
    /**
388
     * @param Label $label
389
     * @return LabelRemoved
390
     */
391
    protected function createLabelRemovedEvent(Label $label)
392
    {
393
        return new LabelRemoved($this->eventId, $label);
394
    }
395
396
    /**
397
     * @inheritdoc
398
     */
399
    protected function createLabelsImportedEvent(Labels $labels)
400
    {
401
        return new LabelsImported($this->eventId, $labels);
402
    }
403
404
    /**
405
     * @param Image $image
406
     * @return ImageAdded
407
     */
408
    protected function createImageAddedEvent(Image $image)
409
    {
410
        return new ImageAdded($this->eventId, $image);
411
    }
412
413
    /**
414
     * @param Image $image
415
     * @return ImageRemoved
416
     */
417
    protected function createImageRemovedEvent(Image $image)
418
    {
419
        return new ImageRemoved($this->eventId, $image);
420
    }
421
422
    /**
423
     * @param UUID $mediaObjectId
424
     * @param StringLiteral $description
425
     * @param StringLiteral $copyrightHolder
426
     * @return ImageUpdated
427
     */
428
    protected function createImageUpdatedEvent(
429
        UUID $mediaObjectId,
430
        StringLiteral $description,
431
        StringLiteral $copyrightHolder
432
    ) {
433
        return new ImageUpdated(
434
            $this->eventId,
435
            $mediaObjectId,
436
            $description,
437
            $copyrightHolder
438
        );
439
    }
440
441
    /**
442
     * @param Image $image
443
     * @return MainImageSelected
444
     */
445
    protected function createMainImageSelectedEvent(Image $image)
446
    {
447
        return new MainImageSelected($this->eventId, $image);
448
    }
449
450
    /**
451
     * @inheritdoc
452
     */
453
    protected function createTitleTranslatedEvent(Language $language, Title $title)
454
    {
455
        return new TitleTranslated($this->eventId, $language, $title);
456
    }
457
458
    /**
459
     * @param Title $title
460
     * @return TitleUpdated
461
     */
462
    protected function createTitleUpdatedEvent(Title $title)
463
    {
464
        return new TitleUpdated($this->eventId, $title);
465
    }
466
467
    /**
468
     * @inheritdoc
469
     */
470
    protected function createDescriptionTranslatedEvent(Language $language, Description $description)
471
    {
472
        return new DescriptionTranslated($this->eventId, $language, $description);
473
    }
474
475
    /**
476
     * @inheritdoc
477
     */
478
    protected function createDescriptionUpdatedEvent(Description $description)
479
    {
480
        return new DescriptionUpdated($this->eventId, $description);
481
    }
482
483
    /**
484
     * @inheritdoc
485
     */
486
    protected function createCalendarUpdatedEvent(Calendar $calendar)
487
    {
488
        return new CalendarUpdated($this->eventId, $calendar);
489
    }
490
491
    /**
492
     * @param AgeRange $typicalAgeRange
493
     * @return TypicalAgeRangeUpdated
494
     */
495
    protected function createTypicalAgeRangeUpdatedEvent($typicalAgeRange)
496
    {
497
        return new TypicalAgeRangeUpdated($this->eventId, $typicalAgeRange);
498
    }
499
500
    /**
501
     * @return TypicalAgeRangeDeleted
502
     */
503
    protected function createTypicalAgeRangeDeletedEvent()
504
    {
505
        return new TypicalAgeRangeDeleted($this->eventId);
506
    }
507
508
    /**
509
     * @param string $organizerId
510
     * @return OrganizerUpdated
511
     */
512
    protected function createOrganizerUpdatedEvent($organizerId)
513
    {
514
        return new OrganizerUpdated($this->eventId, $organizerId);
515
    }
516
517
    /**
518
     * @param string $organizerId
519
     * @return OrganizerDeleted
520
     */
521
    protected function createOrganizerDeletedEvent($organizerId)
522
    {
523
        return new OrganizerDeleted($this->eventId, $organizerId);
524
    }
525
526
    /**
527
     * @param ContactPoint $contactPoint
528
     * @return ContactPointUpdated
529
     */
530
    protected function createContactPointUpdatedEvent(ContactPoint $contactPoint)
531
    {
532
        return new ContactPointUpdated($this->eventId, $contactPoint);
533
    }
534
535
    /**
536
     * @inheritdoc
537
     */
538
    protected function createGeoCoordinatesUpdatedEvent(Coordinates $coordinates)
539
    {
540
        return new GeoCoordinatesUpdated($this->eventId, $coordinates);
541
    }
542
543
    /**
544
     * @param BookingInfo $bookingInfo
545
     * @return BookingInfoUpdated
546
     */
547
    protected function createBookingInfoUpdatedEvent(BookingInfo $bookingInfo)
548
    {
549
        return new BookingInfoUpdated($this->eventId, $bookingInfo);
550
    }
551
552
    /**
553
     * @param PriceInfo $priceInfo
554
     * @return PriceInfoUpdated
555
     */
556
    protected function createPriceInfoUpdatedEvent(PriceInfo $priceInfo)
557
    {
558
        return new PriceInfoUpdated($this->eventId, $priceInfo);
559
    }
560
561
    /**
562
     * @return EventDeleted
563
     */
564
    protected function createOfferDeletedEvent()
565
    {
566
        return new EventDeleted($this->eventId);
567
    }
568
569
    /**
570
     * @inheritdoc
571
     */
572
    protected function createPublishedEvent(\DateTimeInterface $publicationDate)
573
    {
574
        return new Published($this->eventId, $publicationDate);
575
    }
576
577
    /**
578
     * @inheritdoc
579
     */
580
    protected function createApprovedEvent()
581
    {
582
        return new Approved($this->eventId);
583
    }
584
585
    /**
586
     * @inheritdoc
587
     */
588
    protected function createRejectedEvent(StringLiteral $reason)
589
    {
590
        return new Rejected($this->eventId, $reason);
591
    }
592
593
    /**
594
     * @inheritDoc
595
     */
596
    protected function createFlaggedAsDuplicate()
597
    {
598
        return new FlaggedAsDuplicate($this->eventId);
599
    }
600
601
    /**
602
     * @inheritDoc
603
     */
604
    protected function createFlaggedAsInappropriate()
605
    {
606
        return new FlaggedAsInappropriate($this->eventId);
607
    }
608
609
    /**
610
     * @inheritdoc
611
     */
612
    protected function createTypeUpdatedEvent(EventType $type)
613
    {
614
        return new TypeUpdated($this->eventId, $type);
615
    }
616
617
    /**
618
     * @inheritdoc
619
     */
620
    protected function createThemeUpdatedEvent(Theme $theme)
621
    {
622
        return new ThemeUpdated($this->eventId, $theme);
623
    }
624
625
    /**
626
     * @inheritdoc
627
     */
628
    protected function createFacilitiesUpdatedEvent(array $facilities)
629
    {
630
        return new FacilitiesUpdated($this->eventId, $facilities);
631
    }
632
633
    /**
634
     * Use reflection to get check if the aggregate has uncommitted events.
635
     * @return bool
636
     */
637
    private function hasUncommittedEvents()
638
    {
639
        $reflector = new \ReflectionClass(EventSourcedAggregateRoot::class);
640
        $property = $reflector->getProperty('uncommittedEvents');
641
642
        $property->setAccessible(true);
643
        $uncommittedEvents = $property->getValue($this);
644
645
        return !empty($uncommittedEvents);
646
    }
647
648
    public function conclude()
649
    {
650
        if (!$this->concluded) {
651
            $this->apply(new Concluded($this->eventId));
652
        }
653
    }
654
655
    /**
656
     * @param Concluded $concluded
657
     */
658
    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...
659
    {
660
        $this->concluded = true;
661
    }
662
}
663