Completed
Pull Request — master (#103)
by Kristof
08:54
created

Event::applyCollaborationDataAdded()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 17
rs 9.4286
cc 3
eloc 10
nc 4
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\Events\BookingInfoUpdated;
12
use CultuurNet\UDB3\Event\Events\ContactPointUpdated;
13
use CultuurNet\UDB3\Event\Events\DescriptionUpdated;
14
use CultuurNet\UDB3\Event\Events\EventCdbXMLInterface;
15
use CultuurNet\UDB3\Event\Events\EventCreated;
16
use CultuurNet\UDB3\Event\Events\EventCreatedFromCdbXml;
17
use CultuurNet\UDB3\Event\Events\EventDeleted;
18
use CultuurNet\UDB3\Event\Events\EventImportedFromUDB2;
19
use CultuurNet\UDB3\Event\Events\EventUpdatedFromCdbXml;
20
use CultuurNet\UDB3\Event\Events\EventUpdatedFromUDB2;
21
use CultuurNet\UDB3\Event\Events\EventWasLabelled;
22
use CultuurNet\UDB3\Event\Events\ImageAdded;
23
use CultuurNet\UDB3\Event\Events\ImageDeleted;
24
use CultuurNet\UDB3\Event\Events\ImageUpdated;
25
use CultuurNet\UDB3\Event\Events\LabelsMerged;
26
use CultuurNet\UDB3\Event\Events\CollaborationDataAdded;
27
use CultuurNet\UDB3\Event\Events\MajorInfoUpdated;
28
use CultuurNet\UDB3\Event\Events\OrganizerDeleted;
29
use CultuurNet\UDB3\Event\Events\OrganizerUpdated;
30
use CultuurNet\UDB3\Event\Events\TranslationApplied;
31
use CultuurNet\UDB3\Event\Events\TranslationDeleted;
32
use CultuurNet\UDB3\Event\Events\TypicalAgeRangeDeleted;
33
use CultuurNet\UDB3\Event\Events\TypicalAgeRangeUpdated;
34
use CultuurNet\UDB3\Event\Events\Unlabelled;
35
use CultuurNet\UDB3\EventXmlString;
36
use CultuurNet\UDB3\Label;
37
use CultuurNet\UDB3\LabelCollection;
38
use CultuurNet\UDB3\Language;
39
use CultuurNet\UDB3\CollaborationData;
40
use CultuurNet\UDB3\Location;
41
use CultuurNet\UDB3\MediaObject;
42
use CultuurNet\UDB3\Title;
43
use CultuurNet\UDB3\Translation;
44
use ValueObjects\String\String;
45
46
class Event extends EventSourcedAggregateRoot
47
{
48
    protected $eventId;
49
50
    /**
51
     * @var LabelCollection
52
     */
53
    protected $labels;
54
55
    /**
56
     * @var Translation[]
57
     */
58
    protected $translations = [];
59
60
    /**
61
     * @var CollaborationDataCollection[]
62
     *   Array of different collections, keyed by language.
63
     */
64
    protected $collaborationData;
65
66
    const MAIN_LANGUAGE_CODE = 'nl';
67
68
    public function __construct()
69
    {
70
        $this->resetLabels();
71
    }
72
73
    /**
74
     * Factory method to create a new event.
75
     *
76
     * @param Title $title
77
     * @param EventType $eventType
78
     * @param Location $location
79
     * @param CalendarBase $calendar
80
     * @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...
81
     *
82
     * @return Event
83
     */
84
    public static function create($eventId, Title $title, EventType $eventType, Location $location, CalendarInterface $calendar, $theme = null)
85
    {
86
        if (!is_string($eventId)) {
87
            throw new \InvalidArgumentException(
88
                'Expected eventId to be a string, received ' . gettype($eventId)
89
            );
90
        }
91
        $event = new self();
92
        $event->apply(new EventCreated($eventId, $title, $eventType, $location, $calendar, $theme));
93
94
        return $event;
95
    }
96
97
    /**
98
     * @param string $eventId
99
     * @param string $cdbXml
100
     * @param string $cdbXmlNamespaceUri
101
     * @return Event
102
     */
103
    public static function importFromUDB2(
104
        $eventId,
105
        $cdbXml,
106
        $cdbXmlNamespaceUri
107
    ) {
108
        $event = new self();
109
        $event->apply(
110
            new EventImportedFromUDB2(
111
                $eventId,
112
                $cdbXml,
113
                $cdbXmlNamespaceUri
114
            )
115
        );
116
117
        return $event;
118
    }
119
120
    /**
121
     * @param EventXmlString $xmlString
122
     * @param String $eventId
123
     * @param String $cdbXmlNamespaceUri
124
     * @return Event
125
     */
126
    public static function createFromCdbXml(
127
        String $eventId,
128
        EventXmlString $xmlString,
129
        String $cdbXmlNamespaceUri
130
    ) {
131
        $event = new self();
132
        $event->apply(
133
            new EventCreatedFromCdbXml(
134
                $eventId,
135
                $xmlString,
136
                $cdbXmlNamespaceUri
137
            )
138
        );
139
140
        return $event;
141
    }
142
143
    /**
144
     * @param String $eventId
145
     * @param EventXmlString $xmlString
146
     * @param String $cdbXmlNamespaceUri
147
     * @return Event
148
     */
149
    public function updateFromCdbXml(
150
        String $eventId,
151
        EventXmlString $xmlString,
152
        String $cdbXmlNamespaceUri
153
    ) {
154
        $this->apply(
155
            new EventUpdatedFromCdbXml(
156
                $eventId,
157
                $xmlString,
158
                $cdbXmlNamespaceUri
159
            )
160
        );
161
    }
162
163
    /**
164
     * @param LabelCollection $labels
165
     */
166
    public function mergeLabels(LabelCollection $labels)
167
    {
168
        if (count($labels) === 0) {
169
            throw new \InvalidArgumentException(
170
                'Argument $labels should contain at least one label'
171
            );
172
        }
173
174
        $this->apply(
175
            new LabelsMerged(
176
                new String($this->eventId),
177
                $labels
178
            )
179
        );
180
    }
181
182
    /**
183
     * @param Language $language
184
     * @param String|null $title
185
     * @param String|null $shortDescription
186
     * @param String|null $longDescription
187
     */
188
    public function applyTranslation(
189
        Language $language,
190
        String $title = null,
191
        String $shortDescription = null,
192
        String $longDescription = null
193
    ) {
194
        $this->apply(
195
            new TranslationApplied(
196
                new String($this->eventId),
197
                $language,
198
                $title,
199
                $shortDescription,
200
                $longDescription
201
            )
202
        );
203
    }
204
205
    /**
206
     * @param Language $language
207
     */
208
    public function deleteTranslation(
209
        Language $language
210
    ) {
211
        if (!array_key_exists($language->getCode(), $this->translations)) {
212
            return;
213
        }
214
215
        $this->apply(
216
            new TranslationDeleted(
217
                new String($this->eventId),
218
                $language
219
            )
220
        );
221
    }
222
223
    /**
224
     * @param Language $language
225
     * @param CollaborationData $collaborationData
226
     * @return bool
227
     */
228
    protected function isSameCollaborationDataAlreadyPresent(
229
        Language $language,
230
        CollaborationData $collaborationData
231
    ) {
232
        if (!isset($this->collaborationData[$language->getCode()])) {
233
            return false;
234
        }
235
236
        $languageCollaborationData = $this->collaborationData[$language->getCode()];
237
238
        return $languageCollaborationData->contains($collaborationData);
239
    }
240
241
    /**
242
     * @param Language $language
243
     * @param \CultuurNet\UDB3\CollaborationData $collaborationData
244
     */
245
    public function addCollaborationData(
246
        Language $language,
247
        CollaborationData $collaborationData
248
    ) {
249
        if ($this->isSameCollaborationDataAlreadyPresent($language, $collaborationData)) {
250
            return;
251
        }
252
253
        $collaborationDataAdded = new CollaborationDataAdded(
254
            new String($this->eventId),
255
            $language,
256
            $collaborationData
257
        );
258
259
        $this->apply($collaborationDataAdded);
260
    }
261
262
    /**
263
     * {@inheritdoc}
264
     */
265
    public function getAggregateRootId()
266
    {
267
        return $this->eventId;
268
    }
269
270
    /**
271
     * @return Translation[]
272
     */
273
    public function getTranslations()
274
    {
275
        return $this->translations;
276
    }
277
278
    /**
279
     * @return LabelCollection
280
     */
281
    public function getLabels()
282
    {
283
        return $this->labels;
284
    }
285
286
    /**
287
     * @param Label $label
288
     */
289
    public function label(Label $label)
290
    {
291
        if (!$this->labels->contains($label)) {
292
            $this->apply(new EventWasLabelled($this->eventId, $label));
293
        }
294
    }
295
296
    /**
297
     * @param Label $label
298
     */
299
    public function unlabel(Label $label)
300
    {
301
        if ($this->labels->contains($label)) {
302
            $this->apply(new Unlabelled($this->eventId, $label));
303
        }
304
    }
305
306
    protected function applyEventCreated(EventCreated $eventCreated)
307
    {
308
        $this->eventId = $eventCreated->getEventId();
309
    }
310
311
    protected function applyEventWasLabelled(EventWasLabelled $eventLabelled)
312
    {
313
        $newLabel = $eventLabelled->getLabel();
314
315
        if (!$this->labels->contains($newLabel)) {
316
            $this->labels = $this->labels->with($newLabel);
317
        }
318
    }
319
320
    protected function applyUnlabelled(Unlabelled $unlabelled)
321
    {
322
        $removedLabel = $unlabelled->getLabel();
323
324
        $this->labels = $this->labels->without($removedLabel);
325
    }
326
327
    protected function applyEventImportedFromUDB2(
328
        EventImportedFromUDB2 $eventImported
329
    ) {
330
        $this->eventId = $eventImported->getEventId();
331
        $this->setUDB2Data($eventImported);
332
    }
333
334
    /**
335
     * @param EventUpdatedFromUDB2 $eventUpdated
336
     */
337
    protected function applyEventUpdatedFromUDB2(
338
        EventUpdatedFromUDB2 $eventUpdated
339
    ) {
340
        $this->setUDB2Data($eventUpdated);
341
    }
342
343
    /**
344
     * @param EventCdbXMLInterface $eventCdbXML
345
     */
346
    protected function setUDB2Data(
347
        EventCdbXMLInterface $eventCdbXML
348
    ) {
349
        $udb2Event = EventItemFactory::createEventFromCdbXml(
350
            $eventCdbXML->getCdbXmlNamespaceUri(),
351
            $eventCdbXML->getCdbXml()
352
        );
353
354
        $this->setLabelsFromUDB2Event($udb2Event);
355
    }
356
357
    /**
358
     * @param Language $language
359
     * @param string $title
360
     */
361
    public function translateTitle(Language $language, $title)
362
    {
363
        $this->apply(new TitleTranslated($this->eventId, $language, $title));
364
    }
365
366
    /**
367
     * @param Language $language
368
     * @param string $description
369
     */
370
    public function translateDescription(Language $language, $description)
371
    {
372
        $this->apply(
373
            new DescriptionTranslated($this->eventId, $language, $description)
374
        );
375
    }
376
377
    /**
378
     * @param string $description
379
     */
380
    public function updateDescription($description)
381
    {
382
        $this->apply(new DescriptionUpdated($this->eventId, $description));
383
    }
384
385
    /**
386
     * @param string $typicalAgeRange
387
     */
388
    public function updateTypicalAgeRange($typicalAgeRange)
389
    {
390
        $this->apply(new TypicalAgeRangeUpdated($this->eventId, $typicalAgeRange));
391
    }
392
393
    public function deleteTypicalAgeRange()
394
    {
395
        $this->apply(new TypicalAgeRangeDeleted($this->eventId));
396
    }
397
398
    /**
399
     * @param string $organizerId
400
     */
401
    public function updateOrganizer($organizerId)
402
    {
403
        $this->apply(new OrganizerUpdated($this->eventId, $organizerId));
404
    }
405
406
    /**
407
     * Delete the given organizer.
408
     *
409
     * @param string $organizerId
410
     */
411
    public function deleteOrganizer($organizerId)
412
    {
413
        $this->apply(new OrganizerDeleted($this->eventId, $organizerId));
414
    }
415
416
    /**
417
     * Updated the contact info.
418
     *
419
     * @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...
420
     * @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...
421
     * @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...
422
     */
423
    public function updateContactPoint(ContactPoint $contactPoint)
424
    {
425
        $this->apply(new ContactPointUpdated($this->eventId, $contactPoint));
426
    }
427
428
    /**
429
     * Updated the booking info.
430
     *
431
     * @param BookingInfo $bookingInfo
432
     */
433
    public function updateBookingInfo(BookingInfo $bookingInfo)
434
    {
435
        $this->apply(new BookingInfoUpdated($this->eventId, $bookingInfo));
436
    }
437
438
    /**
439
     * Add a new image.
440
     *
441
     * @param MediaObject $mediaObject
442
     */
443
    public function addImage(MediaObject $mediaObject)
444
    {
445
        $this->apply(new ImageAdded($this->eventId, $mediaObject));
446
    }
447
448
    /**
449
     * Update an image.
450
     *
451
     * @param int $indexToUpdate
452
     * @param MediaObject $mediaObject
453
     */
454
    public function updateImage($indexToUpdate, MediaObject $mediaObject)
455
    {
456
        $this->apply(new ImageUpdated($this->eventId, $indexToUpdate, $mediaObject));
457
    }
458
459
    /**
460
     * Delete an image.
461
     *
462
     * @param int $indexToDelete
463
     * @param mixed int|string $internalId
464
     */
465
    public function deleteImage($indexToDelete, $internalId)
466
    {
467
        $this->apply(new ImageDeleted($this->eventId, $indexToDelete, $internalId));
468
    }
469
470
    /**
471
     * Update the major info.
472
     *
473
     * @param Title $title
474
     * @param EventType $eventType
475
     * @param Location $location
476
     * @param CalendarInterface $calendar
477
     * @param type $theme
478
     */
479
    public function updateMajorInfo(Title $title, EventType $eventType, Location $location, CalendarInterface $calendar, $theme = null)
480
    {
481
        $this->apply(new MajorInfoUpdated($this->eventId, $title, $eventType, $location, $calendar, $theme));
482
    }
483
484
    /**
485
     * Delete this item.
486
     */
487
    public function deleteEvent()
488
    {
489
        $this->apply(new EventDeleted($this->eventId));
490
    }
491
492
    /**
493
     * @param \CultureFeed_Cdb_Item_Event $udb2Event
494
     */
495
    protected function setLabelsFromUDB2Event(\CultureFeed_Cdb_Item_Event $udb2Event)
496
    {
497
        $this->resetLabels();
498
499
        /** @var \CultureFeed_Cdb_Data_Keyword $udb2Keyword */
500
        foreach (array_values($udb2Event->getKeywords(true)) as $udb2Keyword) {
501
            $keyword = trim($udb2Keyword->getValue());
502
            if ($keyword) {
503
                $this->labels = $this->labels->with(
504
                    new Label($keyword, $udb2Keyword->isVisible())
505
                );
506
            }
507
        }
508
    }
509
510
    protected function resetLabels()
511
    {
512
        $this->labels = new LabelCollection();
513
    }
514
515
    protected function applyEventCreatedFromCdbXml(
516
        EventCreatedFromCdbXml $eventCreatedFromCdbXml
517
    ) {
518
        $this->eventId = $eventCreatedFromCdbXml->getEventId()->toNative();
519
520
        $udb2Event = EventItemFactory::createEventFromCdbXml(
521
            $eventCreatedFromCdbXml->getCdbXmlNamespaceUri(),
522
            $eventCreatedFromCdbXml->getEventXmlString()->toEventXmlString()
523
        );
524
525
        $this->setLabelsFromUDB2Event($udb2Event);
526
    }
527
528 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...
529
        EventUpdatedFromCdbXml $eventUpdatedFromCdbXml
530
    ) {
531
        $this->eventId = $eventUpdatedFromCdbXml->getEventId()->toNative();
532
533
        $udb2Event = EventItemFactory::createEventFromCdbXml(
534
            $eventUpdatedFromCdbXml->getCdbXmlNamespaceUri(),
535
            $eventUpdatedFromCdbXml->getEventXmlString()->toEventXmlString()
536
        );
537
538
        $this->setLabelsFromUDB2Event($udb2Event);
539
    }
540
541
    protected function applyLabelsMerged(
542
        LabelsMerged $labelsMerged
543
    ) {
544
        $this->labels = $this->labels->merge($labelsMerged->getLabels());
545
    }
546
547
    protected function applyTranslationApplied(
548
        TranslationApplied $translationApplied
549
    ) {
550
        $this->eventId = $translationApplied->getEventId()->toNative();
551
552
        $language = $translationApplied->getLanguage()->getCode();
553
        $translation = new Translation(
554
            $translationApplied->getLanguage(),
555
            $translationApplied->getTitle(),
556
            $translationApplied->getShortdescription(),
557
            $translationApplied->getLongdescription()
558
        );
559
560
        if (!array_key_exists($language, $this->translations)) {
561
            $this->translations[$language] = $translation;
562
        } else {
563
            $newTranslation = $this->translations[$language]->mergeTranslation($translation);
564
            $this->translations[$language] = $newTranslation;
565
        }
566
    }
567
568
    protected function applyTranslationDeleted(
569
        TranslationDeleted $translationDeleted
570
    ) {
571
        $language = $translationDeleted->getLanguage()->getCode();
572
573
        if (array_key_exists($language, $this->translations)) {
574
            unset($this->translations[$language]);
575
        }
576
    }
577
578
    protected function applyCollaborationDataAdded(
579
        CollaborationDataAdded $collaborationDataAdded
580
    ) {
581
        $language = $collaborationDataAdded->getLanguage()->getCode();
582
        $collaborationData = $collaborationDataAdded->getCollaborationData();
583
584
        if (!isset($this->collaborationData[$language])) {
585
            $this->collaborationData[$language] = new CollaborationDataCollection();
586
        }
587
588
        if ($this->collaborationData[$language]->contains($collaborationData)) {
589
            return;
590
        }
591
592
        $this->collaborationData[$language] = $this->collaborationData[$language]
593
            ->with($collaborationData);
594
    }
595
596
    public function updateWithCdbXml($cdbXml, $cdbXmlNamespaceUri)
597
    {
598
        $this->apply(
599
            new EventUpdatedFromUDB2(
600
                $this->eventId,
601
                $cdbXml,
602
                $cdbXmlNamespaceUri
603
            )
604
        );
605
    }
606
}
607