Completed
Pull Request — master (#281)
by Luc
04:19
created

Event::createPublishedEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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