Completed
Pull Request — master (#281)
by Kristof
04:31
created

Event::conclude()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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