Completed
Pull Request — master (#342)
by Luc
11:53 queued 06:44
created

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