Completed
Pull Request — master (#264)
by Luc
05:13
created

Event::copy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 2
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\Cdb\UpdateableWithCdbXmlInterface;
10
use CultuurNet\UDB3\ContactPoint;
11
use CultuurNet\UDB3\Event\Events\AudienceUpdated;
12
use CultuurNet\UDB3\Event\Events\BookingInfoUpdated;
13
use CultuurNet\UDB3\Event\Events\ContactPointUpdated;
14
use CultuurNet\UDB3\Event\Events\DescriptionTranslated;
15
use CultuurNet\UDB3\Event\Events\DescriptionUpdated;
16
use CultuurNet\UDB3\Event\Events\EventCdbXMLInterface;
17
use CultuurNet\UDB3\Event\Events\EventCopied;
18
use CultuurNet\UDB3\Event\Events\EventCreated;
19
use CultuurNet\UDB3\Event\Events\EventDeleted;
20
use CultuurNet\UDB3\Event\Events\EventImportedFromUDB2;
21
use CultuurNet\UDB3\Event\Events\EventUpdatedFromUDB2;
22
use CultuurNet\UDB3\Event\Events\ImageAdded;
23
use CultuurNet\UDB3\Event\Events\ImageRemoved;
24
use CultuurNet\UDB3\Event\Events\Image\ImagesImportedFromUDB2;
25
use CultuurNet\UDB3\Event\Events\Image\ImagesUpdatedFromUDB2;
26
use CultuurNet\UDB3\Event\Events\ImageUpdated;
27
use CultuurNet\UDB3\Event\Events\LabelAdded;
28
use CultuurNet\UDB3\Event\Events\LabelRemoved;
29
use CultuurNet\UDB3\Event\Events\MainImageSelected;
30
use CultuurNet\UDB3\Event\Events\MajorInfoUpdated;
31
use CultuurNet\UDB3\Event\Events\Moderation\Approved;
32
use CultuurNet\UDB3\Event\Events\Moderation\FlaggedAsDuplicate;
33
use CultuurNet\UDB3\Event\Events\Moderation\FlaggedAsInappropriate;
34
use CultuurNet\UDB3\Event\Events\Moderation\Published;
35
use CultuurNet\UDB3\Event\Events\Moderation\Rejected;
36
use CultuurNet\UDB3\Event\Events\OrganizerDeleted;
37
use CultuurNet\UDB3\Event\Events\OrganizerUpdated;
38
use CultuurNet\UDB3\Event\Events\PriceInfoUpdated;
39
use CultuurNet\UDB3\Event\Events\TitleTranslated;
40
use CultuurNet\UDB3\Event\Events\TypicalAgeRangeDeleted;
41
use CultuurNet\UDB3\Event\Events\TypicalAgeRangeUpdated;
42
use CultuurNet\UDB3\Event\ValueObjects\Audience;
43
use CultuurNet\UDB3\Label;
44
use CultuurNet\UDB3\LabelCollection;
45
use CultuurNet\UDB3\Language;
46
use CultuurNet\UDB3\Location\Location;
47
use CultuurNet\UDB3\Media\ImageCollection;
48
use CultuurNet\UDB3\Media\Image;
49
use CultuurNet\UDB3\Offer\Commands\Image\AbstractUpdateImage;
50
use CultuurNet\UDB3\Offer\Offer;
51
use CultuurNet\UDB3\Offer\WorkflowStatus;
52
use CultuurNet\UDB3\PriceInfo\PriceInfo;
53
use CultuurNet\UDB3\Theme;
54
use CultuurNet\UDB3\Title;
55
use ValueObjects\Identity\UUID;
56
use ValueObjects\String\String as StringLiteral;
57
58
class Event extends Offer implements UpdateableWithCdbXmlInterface
59
{
60
    protected $eventId;
61
62
    /**
63
     * @var Audience
64
     */
65
    private $audience;
66
67
    const MAIN_LANGUAGE_CODE = 'nl';
68
69
    public function __construct()
70
    {
71
        parent::__construct();
72
    }
73
74
    /**
75
     * Factory method to create a new event.
76
     *
77
     * @param $eventId
78
     * @param Title $title
79
     * @param EventType $eventType
80
     * @param Location $location
81
     * @param CalendarInterface $calendar
82
     * @param Theme|null $theme
83
     * @param \DateTimeImmutable|null $publicationDate
84
     * @return Event
85
     */
86 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...
87
        $eventId,
88
        Title $title,
89
        EventType $eventType,
90
        Location $location,
91
        CalendarInterface $calendar,
92
        Theme $theme = null,
93
        \DateTimeImmutable $publicationDate = null
94
    ) {
95
        $event = new self();
96
97
        $event->apply(
98
            new EventCreated(
99
                $eventId,
100
                $title,
101
                $eventType,
102
                $location,
103
                $calendar,
104
                $theme,
105
                $publicationDate
106
            )
107
        );
108
109
        return $event;
110
    }
111
112
    /**
113
     * @param string $newEventId
114
     * @param CalendarInterface $calendar
115
     *
116
     * @return Event
117
     */
118
    public function copy($newEventId, CalendarInterface $calendar)
119
    {
120
        if ($this->hasUncommittedEvents()) {
121
            throw new \RuntimeException('I refuse to copy, there are uncommitted events present.');
122
        }
123
124
        // The copied event will have a playhead of the original event + 1
125
        $copy = clone $this;
126
127
        $copy->apply(
128
            new EventCopied(
129
                $newEventId,
130
                $this->eventId,
131
                $calendar
132
            )
133
        );
134
135
        return $copy;
136
    }
137
138
    /**
139
     * @param string $eventId
140
     * @param string $cdbXml
141
     * @param string $cdbXmlNamespaceUri
142
     * @return Event
143
     */
144
    public static function importFromUDB2(
145
        $eventId,
146
        $cdbXml,
147
        $cdbXmlNamespaceUri
148
    ) {
149
        $event = new self();
150
        $event->apply(
151
            new EventImportedFromUDB2(
152
                $eventId,
153
                $cdbXml,
154
                $cdbXmlNamespaceUri
155
            )
156
        );
157
158
        return $event;
159
    }
160
161
    /**
162
     * @param ImageCollection $images
163
     */
164
    public function updateImagesFromUDB2(ImageCollection $images)
165
    {
166
        $this->apply(new ImagesUpdatedFromUDB2($this->eventId, $images));
167
    }
168
169
    /**
170
     * @param ImageCollection $images
171
     */
172
    public function importImagesFromUDB2(ImageCollection $images)
173
    {
174
        $this->apply(new ImagesImportedFromUDB2($this->eventId, $images));
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180
    public function getAggregateRootId()
181
    {
182
        return $this->eventId;
183
    }
184
185
    /**
186
     * @return UUID[]
187
     */
188
    public function getMediaObjects()
189
    {
190
        return $this->mediaObjects;
191
    }
192
193
    protected function applyEventCreated(EventCreated $eventCreated)
194
    {
195
        $this->eventId = $eventCreated->getEventId();
196
        $this->workflowStatus = WorkflowStatus::DRAFT();
197
    }
198
199
    /**
200
     * @param EventCopied $eventCopied
201
     */
202
    protected function applyEventCopied(EventCopied $eventCopied)
203
    {
204
        $this->eventId = $eventCopied->getItemId();
205
        $this->workflowStatus = WorkflowStatus::DRAFT();
206
        $this->labels = new LabelCollection();
207
    }
208
209
    protected function applyEventImportedFromUDB2(
210
        EventImportedFromUDB2 $eventImported
211
    ) {
212
        $this->eventId = $eventImported->getEventId();
213
        $this->setUDB2Data($eventImported);
214
    }
215
216
    /**
217
     * @param EventUpdatedFromUDB2 $eventUpdated
218
     */
219
    protected function applyEventUpdatedFromUDB2(
220
        EventUpdatedFromUDB2 $eventUpdated
221
    ) {
222
        $this->setUDB2Data($eventUpdated);
223
    }
224
225
    /**
226
     * @param EventCdbXMLInterface $eventCdbXML
227
     */
228
    protected function setUDB2Data(
229
        EventCdbXMLInterface $eventCdbXML
230
    ) {
231
        $udb2Event = EventItemFactory::createEventFromCdbXml(
232
            $eventCdbXML->getCdbXmlNamespaceUri(),
233
            $eventCdbXML->getCdbXml()
234
        );
235
236
        $this->importWorkflowStatus($udb2Event);
237
        $this->labels = LabelCollection::fromKeywords($udb2Event->getKeywords(true));
238
    }
239
240
    /**
241
     * Update the major info.
242
     *
243
     * @param Title $title
244
     * @param EventType $eventType
245
     * @param Location $location
246
     * @param CalendarInterface $calendar
247
     * @param Theme|null $theme
248
     */
249
    public function updateMajorInfo(
250
        Title $title,
251
        EventType $eventType,
252
        Location $location,
253
        CalendarInterface $calendar,
254
        Theme $theme = null
255
    ) {
256
        $this->apply(new MajorInfoUpdated($this->eventId, $title, $eventType, $location, $calendar, $theme));
257
    }
258
259
    /**
260
     * @param Audience $audience
261
     */
262
    public function updateAudience(
263
        Audience $audience
264
    ) {
265
        if (is_null($this->audience) || !$this->audience->equals($audience)) {
266
            $this->apply(new AudienceUpdated(
267
                $this->eventId,
268
                $audience
269
            ));
270
        }
271
    }
272
273
    /**
274
     * @param AudienceUpdated $audienceUpdated
275
     */
276
    public function applyAudienceUpdated(AudienceUpdated $audienceUpdated)
277
    {
278
        $this->audience= $audienceUpdated->getAudience();
279
    }
280
281
    /**
282
     * @inheritDoc
283
     * @return ImagesImportedFromUDB2
284
     */
285
    protected function createImagesImportedFromUDB2(ImageCollection $images)
286
    {
287
        return new ImagesImportedFromUDB2($this->eventId, $images);
288
    }
289
290
    /**
291
     * @inheritDoc
292
     * @return ImagesUpdatedFromUDB2
293
     */
294
    protected function createImagesUpdatedFromUDB2(ImageCollection $images)
295
    {
296
        return new ImagesUpdatedFromUDB2($this->eventId, $images);
297
    }
298
299
    /**
300
     * @inheritdoc
301
     */
302
    public function updateWithCdbXml($cdbXml, $cdbXmlNamespaceUri)
303
    {
304
        $this->apply(
305
            new EventUpdatedFromUDB2(
306
                $this->eventId,
307
                $cdbXml,
308
                $cdbXmlNamespaceUri
309
            )
310
        );
311
    }
312
313
    /**
314
     * @param Label $label
315
     * @return LabelAdded
316
     */
317
    protected function createLabelAddedEvent(Label $label)
318
    {
319
        return new LabelAdded($this->eventId, $label);
320
    }
321
322
    /**
323
     * @param Label $label
324
     * @return LabelRemoved
325
     */
326
    protected function createLabelRemovedEvent(Label $label)
327
    {
328
        return new LabelRemoved($this->eventId, $label);
329
    }
330
331
    /**
332
     * @param Image $image
333
     * @return ImageAdded
334
     */
335
    protected function createImageAddedEvent(Image $image)
336
    {
337
        return new ImageAdded($this->eventId, $image);
338
    }
339
340
    /**
341
     * @param Image $image
342
     * @return ImageRemoved
343
     */
344
    protected function createImageRemovedEvent(Image $image)
345
    {
346
        return new ImageRemoved($this->eventId, $image);
347
    }
348
349
    /**
350
     * @param AbstractUpdateImage $updateImageCommand
351
     * @return ImageUpdated
352
     */
353
    protected function createImageUpdatedEvent(
354
        AbstractUpdateImage $updateImageCommand
355
    ) {
356
        return new ImageUpdated(
357
            $this->eventId,
358
            $updateImageCommand->getMediaObjectId(),
359
            $updateImageCommand->getDescription(),
360
            $updateImageCommand->getCopyrightHolder()
361
        );
362
    }
363
364
    /**
365
     * @param Image $image
366
     * @return MainImageSelected
367
     */
368
    protected function createMainImageSelectedEvent(Image $image)
369
    {
370
        return new MainImageSelected($this->eventId, $image);
371
    }
372
373
    /**
374
     * @param Language $language
375
     * @param StringLiteral $title
376
     * @return TitleTranslated
377
     */
378
    protected function createTitleTranslatedEvent(Language $language, StringLiteral $title)
379
    {
380
        return new TitleTranslated($this->eventId, $language, $title);
381
    }
382
383
    /**
384
     * @param Language $language
385
     * @param StringLiteral $description
386
     * @return DescriptionTranslated
387
     */
388
    protected function createDescriptionTranslatedEvent(Language $language, StringLiteral $description)
389
    {
390
        return new DescriptionTranslated($this->eventId, $language, $description);
391
    }
392
393
    /**
394
     * @param string $description
395
     * @return DescriptionUpdated
396
     */
397
    protected function createDescriptionUpdatedEvent($description)
398
    {
399
        return new DescriptionUpdated($this->eventId, $description);
400
    }
401
402
    /**
403
     * @param string $typicalAgeRange
404
     * @return TypicalAgeRangeUpdated
405
     */
406
    protected function createTypicalAgeRangeUpdatedEvent($typicalAgeRange)
407
    {
408
        return new TypicalAgeRangeUpdated($this->eventId, $typicalAgeRange);
409
    }
410
411
    /**
412
     * @return TypicalAgeRangeDeleted
413
     */
414
    protected function createTypicalAgeRangeDeletedEvent()
415
    {
416
        return new TypicalAgeRangeDeleted($this->eventId);
417
    }
418
419
    /**
420
     * @param string $organizerId
421
     * @return OrganizerUpdated
422
     */
423
    protected function createOrganizerUpdatedEvent($organizerId)
424
    {
425
        return new OrganizerUpdated($this->eventId, $organizerId);
426
    }
427
428
    /**
429
     * @param string $organizerId
430
     * @return OrganizerDeleted
431
     */
432
    protected function createOrganizerDeletedEvent($organizerId)
433
    {
434
        return new OrganizerDeleted($this->eventId, $organizerId);
435
    }
436
437
    /**
438
     * @param ContactPoint $contactPoint
439
     * @return ContactPointUpdated
440
     */
441
    protected function createContactPointUpdatedEvent(ContactPoint $contactPoint)
442
    {
443
        return new ContactPointUpdated($this->eventId, $contactPoint);
444
    }
445
446
    /**
447
     * @param BookingInfo $bookingInfo
448
     * @return BookingInfoUpdated
449
     */
450
    protected function createBookingInfoUpdatedEvent(BookingInfo $bookingInfo)
451
    {
452
        return new BookingInfoUpdated($this->eventId, $bookingInfo);
453
    }
454
455
    /**
456
     * @param PriceInfo $priceInfo
457
     * @return PriceInfoUpdated
458
     */
459
    protected function createPriceInfoUpdatedEvent(PriceInfo $priceInfo)
460
    {
461
        return new PriceInfoUpdated($this->eventId, $priceInfo);
462
    }
463
464
    /**
465
     * @return EventDeleted
466
     */
467
    protected function createOfferDeletedEvent()
468
    {
469
        return new EventDeleted($this->eventId);
470
    }
471
472
    /**
473
     * @inheritdoc
474
     */
475
    protected function createPublishedEvent(\DateTimeInterface $publicationDate)
476
    {
477
        return new Published($this->eventId, $publicationDate);
478
    }
479
480
    /**
481
     * @inheritdoc
482
     */
483
    protected function createApprovedEvent()
484
    {
485
        return new Approved($this->eventId);
486
    }
487
488
    /**
489
     * @inheritdoc
490
     */
491
    protected function createRejectedEvent(StringLiteral $reason)
492
    {
493
        return new Rejected($this->eventId, $reason);
494
    }
495
496
    /**
497
     * @inheritDoc
498
     */
499
    protected function createFlaggedAsDuplicate()
500
    {
501
        return new FlaggedAsDuplicate($this->eventId);
502
    }
503
504
    /**
505
     * @inheritDoc
506
     */
507
    protected function createFlaggedAsInappropriate()
508
    {
509
        return new FlaggedAsInappropriate($this->eventId);
510
    }
511
512
    /**
513
     * Use reflection to get check if the aggregate has uncommitted events.
514
     * @return bool
515
     */
516
    private function hasUncommittedEvents()
517
    {
518
        $reflector = new \ReflectionClass(EventSourcedAggregateRoot::class);
519
        $property = $reflector->getProperty('uncommittedEvents');
520
521
        $property->setAccessible(true);
522
        $uncommittedEvents = $property->getValue($this);
523
524
        return !empty($uncommittedEvents);
525
    }
526
}
527