Completed
Pull Request — master (#349)
by Luc
08:34 queued 03:17
created

Event::createLabelsImportedEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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\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\Commands\Image\AbstractUpdateImage;
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 Location $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
        Location $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
    /**
199
     * @return UUID[]
200
     */
201
    public function getMediaObjects()
202
    {
203
        return $this->mediaObjects;
0 ignored issues
show
Bug introduced by
The property mediaObjects does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
204
    }
205
206
    protected function applyEventCreated(EventCreated $eventCreated)
207
    {
208
        $this->eventId = $eventCreated->getEventId();
209
        $this->locationId = new LocationId($eventCreated->getLocation()->getCdbid());
210
        $this->mainLanguage = $eventCreated->getMainLanguage();
211
        $this->workflowStatus = WorkflowStatus::DRAFT();
212
    }
213
214
    /**
215
     * @param EventCopied $eventCopied
216
     */
217
    protected function applyEventCopied(EventCopied $eventCopied)
218
    {
219
        $this->eventId = $eventCopied->getItemId();
220
        $this->workflowStatus = WorkflowStatus::DRAFT();
221
        $this->labels = new LabelCollection();
222
    }
223
224
    protected function applyEventImportedFromUDB2(
225
        EventImportedFromUDB2 $eventImported
226
    ) {
227
        $this->eventId = $eventImported->getEventId();
228
        // When importing from UDB2 the default main language is always 'nl'.
229
        $this->mainLanguage = new Language('nl');
230
        $this->setUDB2Data($eventImported);
231
    }
232
233
    /**
234
     * @param EventUpdatedFromUDB2 $eventUpdated
235
     */
236
    protected function applyEventUpdatedFromUDB2(
237
        EventUpdatedFromUDB2 $eventUpdated
238
    ) {
239
        // Note: when updating from UDB2 never change the main language.
240
        $this->setUDB2Data($eventUpdated);
241
    }
242
243
    /**
244
     * @param EventCdbXMLInterface $eventCdbXML
245
     */
246
    protected function setUDB2Data(
247
        EventCdbXMLInterface $eventCdbXML
248
    ) {
249
        $udb2Event = EventItemFactory::createEventFromCdbXml(
250
            $eventCdbXML->getCdbXmlNamespaceUri(),
251
            $eventCdbXML->getCdbXml()
252
        );
253
254
        // Just clear the facilities.
255
        $this->facilities = [];
256
257
        // Just clear the location id after an import or update.
258
        $this->locationId = null;
259
260
        // Just clear the contact point.
261
        $this->contactPoint = null;
262
263
        // Just clear the calendar.
264
        $this->calendar = null;
265
266
        // Correctly set the age range to avoid issues with deleting age range.
267
        // after an update from UDB2.
268
        $this->typicalAgeRange = new AgeRange(
269
            $udb2Event->getAgeFrom() ? new Age($udb2Event->getAgeFrom()) : null,
270
            $udb2Event->getAgeTo() ? new Age($udb2Event->getAgeTo()) : null
271
        );
272
273
        // Just clear the booking info.
274
        $this->bookingInfo = null;
275
276
        $this->importWorkflowStatus($udb2Event);
277
        $this->labels = LabelCollection::fromKeywords($udb2Event->getKeywords(true));
278
    }
279
280
    /**
281
     * Update the major info.
282
     *
283
     * @param Title $title
284
     * @param EventType $eventType
285
     * @param Location $location
286
     * @param CalendarInterface $calendar
287
     * @param Theme|null $theme
288
     */
289
    public function updateMajorInfo(
290
        Title $title,
291
        EventType $eventType,
292
        Location $location,
293
        CalendarInterface $calendar,
294
        Theme $theme = null
295
    ) {
296
        $this->apply(new MajorInfoUpdated($this->eventId, $title, $eventType, $location, $calendar, $theme));
297
    }
298
299
    /**
300
     * @param LocationId $locationId
301
     */
302
    public function updateLocation(LocationId $locationId)
303
    {
304
        if (is_null($this->locationId) || !$this->locationId->sameValueAs($locationId)) {
305
            $this->apply(new LocationUpdated($this->eventId, $locationId));
306
        }
307
    }
308
309
    /**
310
     * @param LocationUpdated $locationUpdated
311
     */
312
    public function applyLocationUpdated(LocationUpdated $locationUpdated)
313
    {
314
        $this->locationId = $locationUpdated->getLocationId();
315
    }
316
317
    /**
318
     * @param Audience $audience
319
     */
320
    public function updateAudience(
321
        Audience $audience
322
    ) {
323
        if (is_null($this->audience) || !$this->audience->equals($audience)) {
324
            $this->apply(new AudienceUpdated(
325
                $this->eventId,
326
                $audience
327
            ));
328
        }
329
    }
330
331
    /**
332
     * @param AudienceUpdated $audienceUpdated
333
     */
334
    public function applyAudienceUpdated(AudienceUpdated $audienceUpdated)
335
    {
336
        $this->audience= $audienceUpdated->getAudience();
337
    }
338
339
    /**
340
     * @inheritDoc
341
     * @return ImagesImportedFromUDB2
342
     */
343
    protected function createImagesImportedFromUDB2(ImageCollection $images)
344
    {
345
        return new ImagesImportedFromUDB2($this->eventId, $images);
346
    }
347
348
    /**
349
     * @inheritDoc
350
     * @return ImagesUpdatedFromUDB2
351
     */
352
    protected function createImagesUpdatedFromUDB2(ImageCollection $images)
353
    {
354
        return new ImagesUpdatedFromUDB2($this->eventId, $images);
355
    }
356
357
    /**
358
     * @inheritdoc
359
     */
360
    public function updateWithCdbXml($cdbXml, $cdbXmlNamespaceUri)
361
    {
362
        $this->apply(
363
            new EventUpdatedFromUDB2(
364
                $this->eventId,
365
                $cdbXml,
366
                $cdbXmlNamespaceUri
367
            )
368
        );
369
    }
370
371
    /**
372
     * @param Label $label
373
     * @return LabelAdded
374
     */
375
    protected function createLabelAddedEvent(Label $label)
376
    {
377
        return new LabelAdded($this->eventId, $label);
378
    }
379
380
    /**
381
     * @param Label $label
382
     * @return LabelRemoved
383
     */
384
    protected function createLabelRemovedEvent(Label $label)
385
    {
386
        return new LabelRemoved($this->eventId, $label);
387
    }
388
389
    /**
390
     * @inheritdoc
391
     */
392
    protected function createLabelsImportedEvent(Labels $labels)
393
    {
394
        return new LabelsImported($this->eventId, $labels);
395
    }
396
397
    /**
398
     * @param Image $image
399
     * @return ImageAdded
400
     */
401
    protected function createImageAddedEvent(Image $image)
402
    {
403
        return new ImageAdded($this->eventId, $image);
404
    }
405
406
    /**
407
     * @param Image $image
408
     * @return ImageRemoved
409
     */
410
    protected function createImageRemovedEvent(Image $image)
411
    {
412
        return new ImageRemoved($this->eventId, $image);
413
    }
414
415
    /**
416
     * @param AbstractUpdateImage $updateImageCommand
417
     * @return ImageUpdated
418
     */
419
    protected function createImageUpdatedEvent(
420
        AbstractUpdateImage $updateImageCommand
421
    ) {
422
        return new ImageUpdated(
423
            $this->eventId,
424
            $updateImageCommand->getMediaObjectId(),
425
            $updateImageCommand->getDescription(),
426
            $updateImageCommand->getCopyrightHolder()
427
        );
428
    }
429
430
    /**
431
     * @param Image $image
432
     * @return MainImageSelected
433
     */
434
    protected function createMainImageSelectedEvent(Image $image)
435
    {
436
        return new MainImageSelected($this->eventId, $image);
437
    }
438
439
    /**
440
     * @param Language $language
441
     * @param StringLiteral $title
442
     * @return TitleTranslated
443
     */
444
    protected function createTitleTranslatedEvent(Language $language, StringLiteral $title)
445
    {
446
        return new TitleTranslated($this->eventId, $language, $title);
447
    }
448
449
    /**
450
     * @param Title $title
451
     * @return TitleUpdated
452
     */
453
    protected function createTitleUpdatedEvent(Title $title)
454
    {
455
        return new TitleUpdated($this->eventId, $title);
456
    }
457
458
    /**
459
     * @param Language $language
460
     * @param StringLiteral $description
461
     * @return DescriptionTranslated
462
     */
463
    protected function createDescriptionTranslatedEvent(Language $language, StringLiteral $description)
464
    {
465
        return new DescriptionTranslated($this->eventId, $language, $description);
466
    }
467
468
    /**
469
     * @param string $description
470
     * @return DescriptionUpdated
471
     */
472
    protected function createDescriptionUpdatedEvent($description)
473
    {
474
        return new DescriptionUpdated($this->eventId, $description);
475
    }
476
477
    /**
478
     * @inheritdoc
479
     */
480
    protected function createCalendarUpdatedEvent(Calendar $calendar)
481
    {
482
        return new CalendarUpdated($this->eventId, $calendar);
483
    }
484
485
    /**
486
     * @param string $typicalAgeRange
487
     * @return TypicalAgeRangeUpdated
488
     */
489
    protected function createTypicalAgeRangeUpdatedEvent($typicalAgeRange)
490
    {
491
        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...
492
    }
493
494
    /**
495
     * @return TypicalAgeRangeDeleted
496
     */
497
    protected function createTypicalAgeRangeDeletedEvent()
498
    {
499
        return new TypicalAgeRangeDeleted($this->eventId);
500
    }
501
502
    /**
503
     * @param string $organizerId
504
     * @return OrganizerUpdated
505
     */
506
    protected function createOrganizerUpdatedEvent($organizerId)
507
    {
508
        return new OrganizerUpdated($this->eventId, $organizerId);
509
    }
510
511
    /**
512
     * @param string $organizerId
513
     * @return OrganizerDeleted
514
     */
515
    protected function createOrganizerDeletedEvent($organizerId)
516
    {
517
        return new OrganizerDeleted($this->eventId, $organizerId);
518
    }
519
520
    /**
521
     * @param ContactPoint $contactPoint
522
     * @return ContactPointUpdated
523
     */
524
    protected function createContactPointUpdatedEvent(ContactPoint $contactPoint)
525
    {
526
        return new ContactPointUpdated($this->eventId, $contactPoint);
527
    }
528
529
    /**
530
     * @inheritdoc
531
     */
532
    protected function createGeoCoordinatesUpdatedEvent(Coordinates $coordinates)
533
    {
534
        return new GeoCoordinatesUpdated($this->eventId, $coordinates);
535
    }
536
537
    /**
538
     * @param BookingInfo $bookingInfo
539
     * @return BookingInfoUpdated
540
     */
541
    protected function createBookingInfoUpdatedEvent(BookingInfo $bookingInfo)
542
    {
543
        return new BookingInfoUpdated($this->eventId, $bookingInfo);
544
    }
545
546
    /**
547
     * @param PriceInfo $priceInfo
548
     * @return PriceInfoUpdated
549
     */
550
    protected function createPriceInfoUpdatedEvent(PriceInfo $priceInfo)
551
    {
552
        return new PriceInfoUpdated($this->eventId, $priceInfo);
553
    }
554
555
    /**
556
     * @return EventDeleted
557
     */
558
    protected function createOfferDeletedEvent()
559
    {
560
        return new EventDeleted($this->eventId);
561
    }
562
563
    /**
564
     * @inheritdoc
565
     */
566
    protected function createPublishedEvent(\DateTimeInterface $publicationDate)
567
    {
568
        return new Published($this->eventId, $publicationDate);
569
    }
570
571
    /**
572
     * @inheritdoc
573
     */
574
    protected function createApprovedEvent()
575
    {
576
        return new Approved($this->eventId);
577
    }
578
579
    /**
580
     * @inheritdoc
581
     */
582
    protected function createRejectedEvent(StringLiteral $reason)
583
    {
584
        return new Rejected($this->eventId, $reason);
585
    }
586
587
    /**
588
     * @inheritDoc
589
     */
590
    protected function createFlaggedAsDuplicate()
591
    {
592
        return new FlaggedAsDuplicate($this->eventId);
593
    }
594
595
    /**
596
     * @inheritDoc
597
     */
598
    protected function createFlaggedAsInappropriate()
599
    {
600
        return new FlaggedAsInappropriate($this->eventId);
601
    }
602
603
    /**
604
     * @inheritdoc
605
     */
606
    protected function createTypeUpdatedEvent(EventType $type)
607
    {
608
        return new TypeUpdated($this->eventId, $type);
609
    }
610
611
    /**
612
     * @inheritdoc
613
     */
614
    protected function createThemeUpdatedEvent(Theme $theme)
615
    {
616
        return new ThemeUpdated($this->eventId, $theme);
617
    }
618
619
    /**
620
     * @inheritdoc
621
     */
622
    protected function createFacilitiesUpdatedEvent(array $facilities)
623
    {
624
        return new FacilitiesUpdated($this->eventId, $facilities);
625
    }
626
627
    /**
628
     * Use reflection to get check if the aggregate has uncommitted events.
629
     * @return bool
630
     */
631
    private function hasUncommittedEvents()
632
    {
633
        $reflector = new \ReflectionClass(EventSourcedAggregateRoot::class);
634
        $property = $reflector->getProperty('uncommittedEvents');
635
636
        $property->setAccessible(true);
637
        $uncommittedEvents = $property->getValue($this);
638
639
        return !empty($uncommittedEvents);
640
    }
641
642
    public function conclude()
643
    {
644
        if (!$this->concluded) {
645
            $this->apply(new Concluded($this->eventId));
646
        }
647
    }
648
649
    /**
650
     * @param Concluded $concluded
651
     */
652
    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...
653
    {
654
        $this->concluded = true;
655
    }
656
}
657