Completed
Pull Request — master (#121)
by Kristof
05:12
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
     * @param Label $label
298
     */
299
    public function label(Label $label)
300
    {
301
        if (!$this->labels->contains($label)) {
302
            $this->apply(new EventWasLabelled($this->eventId, $label));
303
        }
304
    }
305
306
    /**
307
     * @param Label $label
308
     */
309
    public function unlabel(Label $label)
310
    {
311
        if ($this->labels->contains($label)) {
312
            $this->apply(new Unlabelled($this->eventId, $label));
313
        }
314
    }
315
316
    protected function applyEventCreated(EventCreated $eventCreated)
317
    {
318
        $this->eventId = $eventCreated->getEventId();
319
    }
320
321
    protected function applyEventWasLabelled(EventWasLabelled $eventLabelled)
322
    {
323
        $newLabel = $eventLabelled->getLabel();
324
325
        if (!$this->labels->contains($newLabel)) {
326
            $this->labels = $this->labels->with($newLabel);
327
        }
328
    }
329
330
    protected function applyUnlabelled(Unlabelled $unlabelled)
331
    {
332
        $removedLabel = $unlabelled->getLabel();
333
334
        $this->labels = $this->labels->without($removedLabel);
335
    }
336
337
    protected function applyEventImportedFromUDB2(
338
        EventImportedFromUDB2 $eventImported
339
    ) {
340
        $this->eventId = $eventImported->getEventId();
341
        $this->setUDB2Data($eventImported);
342
    }
343
344
    /**
345
     * @param EventUpdatedFromUDB2 $eventUpdated
346
     */
347
    protected function applyEventUpdatedFromUDB2(
348
        EventUpdatedFromUDB2 $eventUpdated
349
    ) {
350
        $this->setUDB2Data($eventUpdated);
351
    }
352
353
    /**
354
     * @param EventCdbXMLInterface $eventCdbXML
355
     */
356
    protected function setUDB2Data(
357
        EventCdbXMLInterface $eventCdbXML
358
    ) {
359
        $udb2Event = EventItemFactory::createEventFromCdbXml(
360
            $eventCdbXML->getCdbXmlNamespaceUri(),
361
            $eventCdbXML->getCdbXml()
362
        );
363
364
        $this->setLabelsFromUDB2Event($udb2Event);
365
    }
366
367
    /**
368
     * @param Language $language
369
     * @param string $title
370
     */
371
    public function translateTitle(Language $language, $title)
372
    {
373
        $this->apply(new TitleTranslated($this->eventId, $language, $title));
374
    }
375
376
    /**
377
     * @param Language $language
378
     * @param string $description
379
     */
380
    public function translateDescription(Language $language, $description)
381
    {
382
        $this->apply(
383
            new DescriptionTranslated($this->eventId, $language, $description)
384
        );
385
    }
386
387
    /**
388
     * @param string $description
389
     */
390
    public function updateDescription($description)
391
    {
392
        $this->apply(new DescriptionUpdated($this->eventId, $description));
393
    }
394
395
    /**
396
     * @param string $typicalAgeRange
397
     */
398
    public function updateTypicalAgeRange($typicalAgeRange)
399
    {
400
        $this->apply(new TypicalAgeRangeUpdated($this->eventId, $typicalAgeRange));
401
    }
402
403
    public function deleteTypicalAgeRange()
404
    {
405
        $this->apply(new TypicalAgeRangeDeleted($this->eventId));
406
    }
407
408
    /**
409
     * @param string $organizerId
410
     */
411
    public function updateOrganizer($organizerId)
412
    {
413
        $this->apply(new OrganizerUpdated($this->eventId, $organizerId));
414
    }
415
416
    /**
417
     * Delete the given organizer.
418
     *
419
     * @param string $organizerId
420
     */
421
    public function deleteOrganizer($organizerId)
422
    {
423
        $this->apply(new OrganizerDeleted($this->eventId, $organizerId));
424
    }
425
426
    /**
427
     * Updated the contact info.
428
     *
429
     * @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...
430
     * @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...
431
     * @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...
432
     */
433
    public function updateContactPoint(ContactPoint $contactPoint)
434
    {
435
        $this->apply(new ContactPointUpdated($this->eventId, $contactPoint));
436
    }
437
438
    /**
439
     * Updated the booking info.
440
     *
441
     * @param BookingInfo $bookingInfo
442
     */
443
    public function updateBookingInfo(BookingInfo $bookingInfo)
444
    {
445
        $this->apply(new BookingInfoUpdated($this->eventId, $bookingInfo));
446
    }
447
448
    /**
449
     * @return boolean
450
     */
451
    private function containsImage(Image $image)
452
    {
453
        $equalImages = array_filter(
454
            $this->mediaObjects,
455
            function ($existingMediaObjectId) use ($image) {
456
                return $image
457
                    ->getMediaObjectId()
458
                    ->sameValueAs($existingMediaObjectId);
459
            }
460
        );
461
462
        return !empty($equalImages);
463
    }
464
465
    /**
466
     * Add a new image.
467
     *
468
     * @param Image $image
469
     */
470
    public function addImage(Image $image)
471
    {
472
        if (!$this->containsImage($image)) {
473
            $this->apply(new ImageAdded($this->eventId, $image));
474
        }
475
    }
476
477
    /**
478
     * @param UpdateImage $updateImageCommand
479
     */
480
    public function updateImage(UpdateImage $updateImageCommand)
481
    {
482
        $this->apply(new ImageUpdated(
483
            $updateImageCommand->getItemId(),
484
            $updateImageCommand->getMediaObjectId(),
485
            $updateImageCommand->getDescription(),
486
            $updateImageCommand->getCopyrightHolder()
487
        ));
488
    }
489
490
    /**
491
     * Remove an image.
492
     *
493
     * @param Image $image
494
     */
495
    public function removeImage(Image $image)
496
    {
497
        if ($this->containsImage($image)) {
498
            $this->apply(new ImageRemoved($this->eventId, $image));
499
        }
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