Completed
Pull Request — master (#121)
by Kristof
06:30
created

Event::applyImageRemoved()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\Event;
4
5
use Broadway\EventSourcing\EventSourcedAggregateRoot;
6
use CultuurNet\UDB3\BookingInfo;
7
use CultuurNet\UDB3\CalendarInterface;
8
use CultuurNet\UDB3\Cdb\EventItemFactory;
9
use CultuurNet\UDB3\CollaborationDataCollection;
10
use CultuurNet\UDB3\ContactPoint;
11
use CultuurNet\UDB3\Event\Commands\UpdateImage;
12
use CultuurNet\UDB3\Event\Events\BookingInfoUpdated;
13
use CultuurNet\UDB3\Event\Events\ContactPointUpdated;
14
use CultuurNet\UDB3\Event\Events\DescriptionUpdated;
15
use CultuurNet\UDB3\Event\Events\EventCdbXMLInterface;
16
use CultuurNet\UDB3\Event\Events\EventCreated;
17
use CultuurNet\UDB3\Event\Events\EventCreatedFromCdbXml;
18
use CultuurNet\UDB3\Event\Events\EventDeleted;
19
use CultuurNet\UDB3\Event\Events\EventImportedFromUDB2;
20
use CultuurNet\UDB3\Event\Events\EventUpdatedFromCdbXml;
21
use CultuurNet\UDB3\Event\Events\EventUpdatedFromUDB2;
22
use CultuurNet\UDB3\Event\Events\EventWasLabelled;
23
use CultuurNet\UDB3\Event\Events\ImageAdded;
24
use CultuurNet\UDB3\Event\Events\ImageRemoved;
25
use CultuurNet\UDB3\Event\Events\ImageUpdated;
26
use CultuurNet\UDB3\Event\Events\LabelsMerged;
27
use CultuurNet\UDB3\Event\Events\CollaborationDataAdded;
28
use CultuurNet\UDB3\Event\Events\MajorInfoUpdated;
29
use CultuurNet\UDB3\Event\Events\OrganizerDeleted;
30
use CultuurNet\UDB3\Event\Events\OrganizerUpdated;
31
use CultuurNet\UDB3\Event\Events\TranslationApplied;
32
use CultuurNet\UDB3\Event\Events\TranslationDeleted;
33
use CultuurNet\UDB3\Event\Events\TypicalAgeRangeDeleted;
34
use CultuurNet\UDB3\Event\Events\TypicalAgeRangeUpdated;
35
use CultuurNet\UDB3\Event\Events\Unlabelled;
36
use CultuurNet\UDB3\EventXmlString;
37
use CultuurNet\UDB3\Label;
38
use CultuurNet\UDB3\LabelCollection;
39
use CultuurNet\UDB3\Language;
40
use CultuurNet\UDB3\CollaborationData;
41
use CultuurNet\UDB3\Location;
42
use CultuurNet\UDB3\Media\Image;
43
use CultuurNet\UDB3\Media\MediaObject;
44
use CultuurNet\UDB3\Media\Properties\MIMEType;
45
use CultuurNet\UDB3\Title;
46
use CultuurNet\UDB3\Translation;
47
use ValueObjects\Identity\UUID;
48
use ValueObjects\String\String;
49
use ValueObjects\Web\Url;
50
51
class Event extends EventSourcedAggregateRoot
52
{
53
    protected $eventId;
54
55
    /**
56
     * @var LabelCollection
57
     */
58
    protected $labels;
59
60
    /**
61
     * @var Translation[]
62
     */
63
    protected $translations = [];
64
65
    /**
66
     * @var CollaborationDataCollection[]
67
     *   Array of different collections, keyed by language.
68
     */
69
    protected $collaborationData;
70
71
    /**
72
     * @var UUID[]
73
     */
74
    protected $mediaObjects = [];
75
76
    const MAIN_LANGUAGE_CODE = 'nl';
77
78
    public function __construct()
79
    {
80
        $this->resetLabels();
81
    }
82
83
    /**
84
     * Factory method to create a new event.
85
     *
86
     * @param Title $title
87
     * @param EventType $eventType
88
     * @param Location $location
89
     * @param CalendarBase $calendar
90
     * @param Theme/null $theme
0 ignored issues
show
Documentation introduced by
The doc-type Theme/null could not be parsed: Unknown type name "Theme/null" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
91
     *
92
     * @return Event
93
     */
94
    public static function create($eventId, Title $title, EventType $eventType, Location $location, CalendarInterface $calendar, $theme = null)
95
    {
96
        if (!is_string($eventId)) {
97
            throw new \InvalidArgumentException(
98
                'Expected eventId to be a string, received ' . gettype($eventId)
99
            );
100
        }
101
        $event = new self();
102
        $event->apply(new EventCreated($eventId, $title, $eventType, $location, $calendar, $theme));
103
104
        return $event;
105
    }
106
107
    /**
108
     * @param string $eventId
109
     * @param string $cdbXml
110
     * @param string $cdbXmlNamespaceUri
111
     * @return Event
112
     */
113
    public static function importFromUDB2(
114
        $eventId,
115
        $cdbXml,
116
        $cdbXmlNamespaceUri
117
    ) {
118
        $event = new self();
119
        $event->apply(
120
            new EventImportedFromUDB2(
121
                $eventId,
122
                $cdbXml,
123
                $cdbXmlNamespaceUri
124
            )
125
        );
126
127
        return $event;
128
    }
129
130
    /**
131
     * @param EventXmlString $xmlString
132
     * @param String $eventId
133
     * @param String $cdbXmlNamespaceUri
134
     * @return Event
135
     */
136
    public static function createFromCdbXml(
137
        String $eventId,
138
        EventXmlString $xmlString,
139
        String $cdbXmlNamespaceUri
140
    ) {
141
        $event = new self();
142
        $event->apply(
143
            new EventCreatedFromCdbXml(
144
                $eventId,
145
                $xmlString,
146
                $cdbXmlNamespaceUri
147
            )
148
        );
149
150
        return $event;
151
    }
152
153
    /**
154
     * @param String $eventId
155
     * @param EventXmlString $xmlString
156
     * @param String $cdbXmlNamespaceUri
157
     * @return Event
158
     */
159
    public function updateFromCdbXml(
160
        String $eventId,
161
        EventXmlString $xmlString,
162
        String $cdbXmlNamespaceUri
163
    ) {
164
        $this->apply(
165
            new EventUpdatedFromCdbXml(
166
                $eventId,
167
                $xmlString,
168
                $cdbXmlNamespaceUri
169
            )
170
        );
171
    }
172
173
    /**
174
     * @param LabelCollection $labels
175
     */
176
    public function mergeLabels(LabelCollection $labels)
177
    {
178
        if (count($labels) === 0) {
179
            throw new \InvalidArgumentException(
180
                'Argument $labels should contain at least one label'
181
            );
182
        }
183
184
        $this->apply(
185
            new LabelsMerged(
186
                new String($this->eventId),
187
                $labels
188
            )
189
        );
190
    }
191
192
    /**
193
     * @param Language $language
194
     * @param String|null $title
195
     * @param String|null $shortDescription
196
     * @param String|null $longDescription
197
     */
198
    public function applyTranslation(
199
        Language $language,
200
        String $title = null,
201
        String $shortDescription = null,
202
        String $longDescription = null
203
    ) {
204
        $this->apply(
205
            new TranslationApplied(
206
                new String($this->eventId),
207
                $language,
208
                $title,
209
                $shortDescription,
210
                $longDescription
211
            )
212
        );
213
    }
214
215
    /**
216
     * @param Language $language
217
     */
218
    public function deleteTranslation(
219
        Language $language
220
    ) {
221
        if (!array_key_exists($language->getCode(), $this->translations)) {
222
            return;
223
        }
224
225
        $this->apply(
226
            new TranslationDeleted(
227
                new String($this->eventId),
228
                $language
229
            )
230
        );
231
    }
232
233
    /**
234
     * @param Language $language
235
     * @param CollaborationData $collaborationData
236
     * @return bool
237
     */
238
    protected function isSameCollaborationDataAlreadyPresent(
239
        Language $language,
240
        CollaborationData $collaborationData
241
    ) {
242
        if (!isset($this->collaborationData[$language->getCode()])) {
243
            return false;
244
        }
245
246
        $languageCollaborationData = $this->collaborationData[$language->getCode()];
247
248
        return $languageCollaborationData->contains($collaborationData);
249
    }
250
251
    /**
252
     * @param Language $language
253
     * @param \CultuurNet\UDB3\CollaborationData $collaborationData
254
     */
255
    public function addCollaborationData(
256
        Language $language,
257
        CollaborationData $collaborationData
258
    ) {
259
        if ($this->isSameCollaborationDataAlreadyPresent($language, $collaborationData)) {
260
            return;
261
        }
262
263
        $collaborationDataAdded = new CollaborationDataAdded(
264
            new String($this->eventId),
265
            $language,
266
            $collaborationData
267
        );
268
269
        $this->apply($collaborationDataAdded);
270
    }
271
272
    /**
273
     * {@inheritdoc}
274
     */
275
    public function getAggregateRootId()
276
    {
277
        return $this->eventId;
278
    }
279
280
    /**
281
     * @return Translation[]
282
     */
283
    public function getTranslations()
284
    {
285
        return $this->translations;
286
    }
287
288
    /**
289
     * @return LabelCollection
290
     */
291
    public function getLabels()
292
    {
293
        return $this->labels;
294
    }
295
296
    /**
297
     * @return UUID[]
298
     */
299
    public function getMediaObjects()
300
    {
301
        return $this->mediaObjects;
302
    }
303
304
    /**
305
     * @param Label $label
306
     */
307
    public function label(Label $label)
308
    {
309
        if (!$this->labels->contains($label)) {
310
            $this->apply(new EventWasLabelled($this->eventId, $label));
311
        }
312
    }
313
314
    /**
315
     * @param Label $label
316
     */
317
    public function unlabel(Label $label)
318
    {
319
        if ($this->labels->contains($label)) {
320
            $this->apply(new Unlabelled($this->eventId, $label));
321
        }
322
    }
323
324
    protected function applyEventCreated(EventCreated $eventCreated)
325
    {
326
        $this->eventId = $eventCreated->getEventId();
327
    }
328
329
    protected function applyEventWasLabelled(EventWasLabelled $eventLabelled)
330
    {
331
        $newLabel = $eventLabelled->getLabel();
332
333
        if (!$this->labels->contains($newLabel)) {
334
            $this->labels = $this->labels->with($newLabel);
335
        }
336
    }
337
338
    protected function applyUnlabelled(Unlabelled $unlabelled)
339
    {
340
        $removedLabel = $unlabelled->getLabel();
341
342
        $this->labels = $this->labels->without($removedLabel);
343
    }
344
345
    protected function applyEventImportedFromUDB2(
346
        EventImportedFromUDB2 $eventImported
347
    ) {
348
        $this->eventId = $eventImported->getEventId();
349
        $this->setUDB2Data($eventImported);
350
    }
351
352
    /**
353
     * @param EventUpdatedFromUDB2 $eventUpdated
354
     */
355
    protected function applyEventUpdatedFromUDB2(
356
        EventUpdatedFromUDB2 $eventUpdated
357
    ) {
358
        $this->setUDB2Data($eventUpdated);
359
    }
360
361
    /**
362
     * @param EventCdbXMLInterface $eventCdbXML
363
     */
364
    protected function setUDB2Data(
365
        EventCdbXMLInterface $eventCdbXML
366
    ) {
367
        $udb2Event = EventItemFactory::createEventFromCdbXml(
368
            $eventCdbXML->getCdbXmlNamespaceUri(),
369
            $eventCdbXML->getCdbXml()
370
        );
371
372
        $this->setLabelsFromUDB2Event($udb2Event);
373
    }
374
375
    /**
376
     * @param Language $language
377
     * @param string $title
378
     */
379
    public function translateTitle(Language $language, $title)
380
    {
381
        $this->apply(new TitleTranslated($this->eventId, $language, $title));
382
    }
383
384
    /**
385
     * @param Language $language
386
     * @param string $description
387
     */
388
    public function translateDescription(Language $language, $description)
389
    {
390
        $this->apply(
391
            new DescriptionTranslated($this->eventId, $language, $description)
392
        );
393
    }
394
395
    /**
396
     * @param string $description
397
     */
398
    public function updateDescription($description)
399
    {
400
        $this->apply(new DescriptionUpdated($this->eventId, $description));
401
    }
402
403
    /**
404
     * @param string $typicalAgeRange
405
     */
406
    public function updateTypicalAgeRange($typicalAgeRange)
407
    {
408
        $this->apply(new TypicalAgeRangeUpdated($this->eventId, $typicalAgeRange));
409
    }
410
411
    public function deleteTypicalAgeRange()
412
    {
413
        $this->apply(new TypicalAgeRangeDeleted($this->eventId));
414
    }
415
416
    /**
417
     * @param string $organizerId
418
     */
419
    public function updateOrganizer($organizerId)
420
    {
421
        $this->apply(new OrganizerUpdated($this->eventId, $organizerId));
422
    }
423
424
    /**
425
     * Delete the given organizer.
426
     *
427
     * @param string $organizerId
428
     */
429
    public function deleteOrganizer($organizerId)
430
    {
431
        $this->apply(new OrganizerDeleted($this->eventId, $organizerId));
432
    }
433
434
    /**
435
     * Updated the contact info.
436
     *
437
     * @param array $phones
0 ignored issues
show
Bug introduced by
There is no parameter named $phones. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
438
     * @param array $emails
0 ignored issues
show
Bug introduced by
There is no parameter named $emails. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
439
     * @param array $urls
0 ignored issues
show
Bug introduced by
There is no parameter named $urls. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
440
     */
441
    public function updateContactPoint(ContactPoint $contactPoint)
442
    {
443
        $this->apply(new ContactPointUpdated($this->eventId, $contactPoint));
444
    }
445
446
    /**
447
     * Updated the booking info.
448
     *
449
     * @param BookingInfo $bookingInfo
450
     */
451
    public function updateBookingInfo(BookingInfo $bookingInfo)
452
    {
453
        $this->apply(new BookingInfoUpdated($this->eventId, $bookingInfo));
454
    }
455
456
    /**
457
     * Add a new image.
458
     *
459
     * @param Image $image
460
     * @throws DuplicateMediaObjectException
461
     */
462
    public function addImage(Image $image)
463
    {
464
        $duplicateMediaObject = array_filter(
465
            $this->getMediaObjects(),
466
            function ($existingMediaObjectId) use ($image) {
467
                return $image
468
                    ->getMediaObjectId()
469
                    ->sameValueAs($existingMediaObjectId);
470
            }
471
        );
472
473
        if (empty($duplicateMediaObject)) {
474
            $this->apply(new ImageAdded($this->eventId, $image));
475
        }
476
    }
477
478
479
    /**
480
     * @param UpdateImage $updateImageCommand
481
     */
482
    public function updateImage(UpdateImage $updateImageCommand)
483
    {
484
        $this->apply(new ImageUpdated(
485
            $updateImageCommand->getItemId(),
486
            $updateImageCommand->getMediaObjectId(),
487
            $updateImageCommand->getDescription(),
488
            $updateImageCommand->getCopyrightHolder()
489
        ));
490
    }
491
492
    /**
493
     * Remove an image.
494
     *
495
     * @param Image $image
496
     */
497
    public function removeImage(Image $image)
498
    {
499
        $this->apply(new ImageRemoved($this->eventId, $image));
500
    }
501
502
    /**
503
     * Update the major info.
504
     *
505
     * @param Title $title
506
     * @param EventType $eventType
507
     * @param Location $location
508
     * @param CalendarInterface $calendar
509
     * @param type $theme
510
     */
511
    public function updateMajorInfo(Title $title, EventType $eventType, Location $location, CalendarInterface $calendar, $theme = null)
512
    {
513
        $this->apply(new MajorInfoUpdated($this->eventId, $title, $eventType, $location, $calendar, $theme));
514
    }
515
516
    /**
517
     * Delete this item.
518
     */
519
    public function deleteEvent()
520
    {
521
        $this->apply(new EventDeleted($this->eventId));
522
    }
523
524
    /**
525
     * @param \CultureFeed_Cdb_Item_Event $udb2Event
526
     */
527
    protected function setLabelsFromUDB2Event(\CultureFeed_Cdb_Item_Event $udb2Event)
528
    {
529
        $this->resetLabels();
530
531
        /** @var \CultureFeed_Cdb_Data_Keyword $udb2Keyword */
532
        foreach (array_values($udb2Event->getKeywords(true)) as $udb2Keyword) {
533
            $keyword = trim($udb2Keyword->getValue());
534
            if ($keyword) {
535
                $this->labels = $this->labels->with(
536
                    new Label($keyword, $udb2Keyword->isVisible())
537
                );
538
            }
539
        }
540
    }
541
542
    protected function resetLabels()
543
    {
544
        $this->labels = new LabelCollection();
545
    }
546
547
    protected function applyEventCreatedFromCdbXml(
548
        EventCreatedFromCdbXml $eventCreatedFromCdbXml
549
    ) {
550
        $this->eventId = $eventCreatedFromCdbXml->getEventId()->toNative();
551
552
        $udb2Event = EventItemFactory::createEventFromCdbXml(
553
            $eventCreatedFromCdbXml->getCdbXmlNamespaceUri(),
554
            $eventCreatedFromCdbXml->getEventXmlString()->toEventXmlString()
555
        );
556
557
        $this->setLabelsFromUDB2Event($udb2Event);
558
    }
559
560 View Code Duplication
    protected function applyEventUpdatedFromCdbXml(
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...
561
        EventUpdatedFromCdbXml $eventUpdatedFromCdbXml
562
    ) {
563
        $this->eventId = $eventUpdatedFromCdbXml->getEventId()->toNative();
564
565
        $udb2Event = EventItemFactory::createEventFromCdbXml(
566
            $eventUpdatedFromCdbXml->getCdbXmlNamespaceUri(),
567
            $eventUpdatedFromCdbXml->getEventXmlString()->toEventXmlString()
568
        );
569
570
        $this->setLabelsFromUDB2Event($udb2Event);
571
    }
572
573
    protected function applyLabelsMerged(
574
        LabelsMerged $labelsMerged
575
    ) {
576
        $this->labels = $this->labels->merge($labelsMerged->getLabels());
577
    }
578
579
    protected function applyTranslationApplied(
580
        TranslationApplied $translationApplied
581
    ) {
582
        $this->eventId = $translationApplied->getEventId()->toNative();
583
584
        $language = $translationApplied->getLanguage()->getCode();
585
        $translation = new Translation(
586
            $translationApplied->getLanguage(),
587
            $translationApplied->getTitle(),
588
            $translationApplied->getShortdescription(),
589
            $translationApplied->getLongdescription()
590
        );
591
592
        if (!array_key_exists($language, $this->translations)) {
593
            $this->translations[$language] = $translation;
594
        } else {
595
            $newTranslation = $this->translations[$language]->mergeTranslation($translation);
596
            $this->translations[$language] = $newTranslation;
597
        }
598
    }
599
600
    protected function applyTranslationDeleted(
601
        TranslationDeleted $translationDeleted
602
    ) {
603
        $language = $translationDeleted->getLanguage()->getCode();
604
605
        if (array_key_exists($language, $this->translations)) {
606
            unset($this->translations[$language]);
607
        }
608
    }
609
610
    protected function applyCollaborationDataAdded(
611
        CollaborationDataAdded $collaborationDataAdded
612
    ) {
613
        $language = $collaborationDataAdded->getLanguage()->getCode();
614
        $collaborationData = $collaborationDataAdded->getCollaborationData();
615
616
        if (!isset($this->collaborationData[$language])) {
617
            $this->collaborationData[$language] = new CollaborationDataCollection();
618
        }
619
620
        if ($this->collaborationData[$language]->contains($collaborationData)) {
621
            return;
622
        }
623
624
        $this->collaborationData[$language] = $this->collaborationData[$language]
625
            ->withKey(
626
                $collaborationData->getSubBrand()->toNative(),
627
                $collaborationData
628
            );
629
    }
630
631
    public function updateWithCdbXml($cdbXml, $cdbXmlNamespaceUri)
632
    {
633
        $this->apply(
634
            new EventUpdatedFromUDB2(
635
                $this->eventId,
636
                $cdbXml,
637
                $cdbXmlNamespaceUri
638
            )
639
        );
640
    }
641
642
    protected function applyImageAdded(ImageAdded $imageAdded)
643
    {
644
        $this->mediaObjects[] = $imageAdded->getImage()->getMediaObjectId();
645
    }
646
647
    protected function applyImageRemoved(ImageRemoved $imageRemoved)
648
    {
649
        $this->mediaObjects = array_diff(
650
            $this->mediaObjects,
651
            [$imageRemoved->getImage()->getMediaObjectId()]
652
        );
653
    }
654
}
655