Completed
Pull Request — master (#342)
by Luc
05:03
created

Event::createThemeUpdatedEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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