Completed
Pull Request — master (#321)
by
unknown
04:34
created

Event::createTitleUpdatedEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\LocationUpdated;
31
use CultuurNet\UDB3\Event\Events\MainImageSelected;
32
use CultuurNet\UDB3\Event\Events\MajorInfoUpdated;
33
use CultuurNet\UDB3\Event\Events\Moderation\Approved;
34
use CultuurNet\UDB3\Event\Events\Moderation\FlaggedAsDuplicate;
35
use CultuurNet\UDB3\Event\Events\Moderation\FlaggedAsInappropriate;
36
use CultuurNet\UDB3\Event\Events\Moderation\Published;
37
use CultuurNet\UDB3\Event\Events\Moderation\Rejected;
38
use CultuurNet\UDB3\Event\Events\OrganizerDeleted;
39
use CultuurNet\UDB3\Event\Events\OrganizerUpdated;
40
use CultuurNet\UDB3\Event\Events\PriceInfoUpdated;
41
use CultuurNet\UDB3\Event\Events\TitleTranslated;
42
use CultuurNet\UDB3\Event\Events\TitleUpdated;
43
use CultuurNet\UDB3\Event\Events\TypicalAgeRangeDeleted;
44
use CultuurNet\UDB3\Event\Events\TypicalAgeRangeUpdated;
45
use CultuurNet\UDB3\Event\ValueObjects\Audience;
46
use CultuurNet\UDB3\Label;
47
use CultuurNet\UDB3\LabelCollection;
48
use CultuurNet\UDB3\Language;
49
use CultuurNet\UDB3\Location\Location;
50
use CultuurNet\UDB3\Location\LocationId;
51
use CultuurNet\UDB3\Media\ImageCollection;
52
use CultuurNet\UDB3\Media\Image;
53
use CultuurNet\UDB3\Offer\Commands\Image\AbstractUpdateImage;
54
use CultuurNet\UDB3\Offer\Offer;
55
use CultuurNet\UDB3\Offer\WorkflowStatus;
56
use CultuurNet\UDB3\PriceInfo\PriceInfo;
57
use CultuurNet\UDB3\Theme;
58
use CultuurNet\UDB3\Title;
59
use ValueObjects\Identity\UUID;
60
use ValueObjects\StringLiteral\StringLiteral;
61
62
class Event extends Offer implements UpdateableWithCdbXmlInterface
63
{
64
    /**
65
     * @var string
66
     */
67
    protected $eventId;
68
69
    /**
70
     * @var Audience
71
     */
72
    private $audience;
73
74
    /**
75
     * @var boolean
76
     */
77
    private $concluded = false;
78
79
    const MAIN_LANGUAGE_CODE = 'nl';
80
81
    public function __construct()
82
    {
83
        parent::__construct();
84
    }
85
86
    /**
87
     * Factory method to create a new event.
88
     *
89
     * @param $eventId
90
     * @param Title $title
91
     * @param EventType $eventType
92
     * @param Location $location
93
     * @param CalendarInterface $calendar
94
     * @param Theme|null $theme
95
     * @param \DateTimeImmutable|null $publicationDate
96
     * @return Event
97
     */
98 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...
99
        $eventId,
100
        Title $title,
101
        EventType $eventType,
102
        Location $location,
103
        CalendarInterface $calendar,
104
        Theme $theme = null,
105
        \DateTimeImmutable $publicationDate = null
106
    ) {
107
        $event = new self();
108
109
        $event->apply(
110
            new EventCreated(
111
                $eventId,
112
                $title,
113
                $eventType,
114
                $location,
115
                $calendar,
116
                $theme,
117
                $publicationDate
118
            )
119
        );
120
121
        return $event;
122
    }
123
124
    /**
125
     * @param string $newEventId
126
     * @param CalendarInterface $calendar
127
     *
128
     * @return Event
129
     */
130
    public function copy($newEventId, CalendarInterface $calendar)
131
    {
132
        if ($this->hasUncommittedEvents()) {
133
            throw new \RuntimeException('I refuse to copy, there are uncommitted events present.');
134
        }
135
136
        // The copied event will have a playhead of the original event + 1
137
        $copy = clone $this;
138
139
        $copy->apply(
140
            new EventCopied(
141
                $newEventId,
142
                $this->eventId,
143
                $calendar
144
            )
145
        );
146
147
        return $copy;
148
    }
149
150
    /**
151
     * @param string $eventId
152
     * @param string $cdbXml
153
     * @param string $cdbXmlNamespaceUri
154
     * @return Event
155
     */
156
    public static function importFromUDB2(
157
        $eventId,
158
        $cdbXml,
159
        $cdbXmlNamespaceUri
160
    ) {
161
        $event = new self();
162
        $event->apply(
163
            new EventImportedFromUDB2(
164
                $eventId,
165
                $cdbXml,
166
                $cdbXmlNamespaceUri
167
            )
168
        );
169
170
        return $event;
171
    }
172
173
    /**
174
     * @param ImageCollection $images
175
     */
176
    public function updateImagesFromUDB2(ImageCollection $images)
177
    {
178
        $this->apply(new ImagesUpdatedFromUDB2($this->eventId, $images));
179
    }
180
181
    /**
182
     * @param ImageCollection $images
183
     */
184
    public function importImagesFromUDB2(ImageCollection $images)
185
    {
186
        $this->apply(new ImagesImportedFromUDB2($this->eventId, $images));
187
    }
188
189
    /**
190
     * {@inheritdoc}
191
     */
192
    public function getAggregateRootId()
193
    {
194
        return $this->eventId;
195
    }
196
197
    /**
198
     * @return UUID[]
199
     */
200
    public function getMediaObjects()
201
    {
202
        return $this->mediaObjects;
0 ignored issues
show
Bug introduced by
The property mediaObjects does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
203
    }
204
205
    protected function applyEventCreated(EventCreated $eventCreated)
206
    {
207
        $this->eventId = $eventCreated->getEventId();
208
        $this->workflowStatus = WorkflowStatus::DRAFT();
209
    }
210
211
    /**
212
     * @param EventCopied $eventCopied
213
     */
214
    protected function applyEventCopied(EventCopied $eventCopied)
215
    {
216
        $this->eventId = $eventCopied->getItemId();
217
        $this->workflowStatus = WorkflowStatus::DRAFT();
218
        $this->labels = new LabelCollection();
219
    }
220
221
    protected function applyEventImportedFromUDB2(
222
        EventImportedFromUDB2 $eventImported
223
    ) {
224
        $this->eventId = $eventImported->getEventId();
225
        $this->setUDB2Data($eventImported);
226
    }
227
228
    /**
229
     * @param EventUpdatedFromUDB2 $eventUpdated
230
     */
231
    protected function applyEventUpdatedFromUDB2(
232
        EventUpdatedFromUDB2 $eventUpdated
233
    ) {
234
        $this->setUDB2Data($eventUpdated);
235
    }
236
237
    /**
238
     * @param EventCdbXMLInterface $eventCdbXML
239
     */
240
    protected function setUDB2Data(
241
        EventCdbXMLInterface $eventCdbXML
242
    ) {
243
        $udb2Event = EventItemFactory::createEventFromCdbXml(
244
            $eventCdbXML->getCdbXmlNamespaceUri(),
245
            $eventCdbXML->getCdbXml()
246
        );
247
248
        $this->importWorkflowStatus($udb2Event);
249
        $this->labels = LabelCollection::fromKeywords($udb2Event->getKeywords(true));
250
    }
251
252
    /**
253
     * Update the major info.
254
     *
255
     * @param Title $title
256
     * @param EventType $eventType
257
     * @param Location $location
258
     * @param CalendarInterface $calendar
259
     * @param Theme|null $theme
260
     */
261
    public function updateMajorInfo(
262
        Title $title,
263
        EventType $eventType,
264
        Location $location,
265
        CalendarInterface $calendar,
266
        Theme $theme = null
267
    ) {
268
        $this->apply(new MajorInfoUpdated($this->eventId, $title, $eventType, $location, $calendar, $theme));
269
    }
270
271
    /**
272
     * @param LocationId $locationId
273
     */
274
    public function updateLocation(LocationId $locationId)
275
    {
276
        // For now no special business rules for updating the location of an event.
277
        $this->apply(new LocationUpdated($this->eventId, $locationId));
278
    }
279
280
    /**
281
     * @param Audience $audience
282
     */
283
    public function updateAudience(
284
        Audience $audience
285
    ) {
286
        if (is_null($this->audience) || !$this->audience->equals($audience)) {
287
            $this->apply(new AudienceUpdated(
288
                $this->eventId,
289
                $audience
290
            ));
291
        }
292
    }
293
294
    /**
295
     * @param AudienceUpdated $audienceUpdated
296
     */
297
    public function applyAudienceUpdated(AudienceUpdated $audienceUpdated)
298
    {
299
        $this->audience= $audienceUpdated->getAudience();
300
    }
301
302
    /**
303
     * @inheritDoc
304
     * @return ImagesImportedFromUDB2
305
     */
306
    protected function createImagesImportedFromUDB2(ImageCollection $images)
307
    {
308
        return new ImagesImportedFromUDB2($this->eventId, $images);
309
    }
310
311
    /**
312
     * @inheritDoc
313
     * @return ImagesUpdatedFromUDB2
314
     */
315
    protected function createImagesUpdatedFromUDB2(ImageCollection $images)
316
    {
317
        return new ImagesUpdatedFromUDB2($this->eventId, $images);
318
    }
319
320
    /**
321
     * @inheritdoc
322
     */
323
    public function updateWithCdbXml($cdbXml, $cdbXmlNamespaceUri)
324
    {
325
        $this->apply(
326
            new EventUpdatedFromUDB2(
327
                $this->eventId,
328
                $cdbXml,
329
                $cdbXmlNamespaceUri
330
            )
331
        );
332
    }
333
334
    /**
335
     * @param Label $label
336
     * @return LabelAdded
337
     */
338
    protected function createLabelAddedEvent(Label $label)
339
    {
340
        return new LabelAdded($this->eventId, $label);
341
    }
342
343
    /**
344
     * @param Label $label
345
     * @return LabelRemoved
346
     */
347
    protected function createLabelRemovedEvent(Label $label)
348
    {
349
        return new LabelRemoved($this->eventId, $label);
350
    }
351
352
    /**
353
     * @param Image $image
354
     * @return ImageAdded
355
     */
356
    protected function createImageAddedEvent(Image $image)
357
    {
358
        return new ImageAdded($this->eventId, $image);
359
    }
360
361
    /**
362
     * @param Image $image
363
     * @return ImageRemoved
364
     */
365
    protected function createImageRemovedEvent(Image $image)
366
    {
367
        return new ImageRemoved($this->eventId, $image);
368
    }
369
370
    /**
371
     * @param AbstractUpdateImage $updateImageCommand
372
     * @return ImageUpdated
373
     */
374
    protected function createImageUpdatedEvent(
375
        AbstractUpdateImage $updateImageCommand
376
    ) {
377
        return new ImageUpdated(
378
            $this->eventId,
379
            $updateImageCommand->getMediaObjectId(),
380
            $updateImageCommand->getDescription(),
381
            $updateImageCommand->getCopyrightHolder()
382
        );
383
    }
384
385
    /**
386
     * @param Image $image
387
     * @return MainImageSelected
388
     */
389
    protected function createMainImageSelectedEvent(Image $image)
390
    {
391
        return new MainImageSelected($this->eventId, $image);
392
    }
393
394
    /**
395
     * @param Language $language
396
     * @param StringLiteral $title
397
     * @return TitleTranslated
398
     */
399
    protected function createTitleTranslatedEvent(Language $language, StringLiteral $title)
400
    {
401
        return new TitleTranslated($this->eventId, $language, $title);
402
    }
403
404
    /**
405
     * @param Title $title
406
     * @return TitleUpdated
407
     */
408
    protected function createTitleUpdatedEvent(Title $title)
409
    {
410
        return new TitleUpdated($this->eventId, $title);
411
    }
412
413
    /**
414
     * @param Language $language
415
     * @param StringLiteral $description
416
     * @return DescriptionTranslated
417
     */
418
    protected function createDescriptionTranslatedEvent(Language $language, StringLiteral $description)
419
    {
420
        return new DescriptionTranslated($this->eventId, $language, $description);
421
    }
422
423
    /**
424
     * @param string $description
425
     * @return DescriptionUpdated
426
     */
427
    protected function createDescriptionUpdatedEvent($description)
428
    {
429
        return new DescriptionUpdated($this->eventId, $description);
430
    }
431
432
    /**
433
     * @param string $typicalAgeRange
434
     * @return TypicalAgeRangeUpdated
435
     */
436
    protected function createTypicalAgeRangeUpdatedEvent($typicalAgeRange)
437
    {
438
        return new TypicalAgeRangeUpdated($this->eventId, $typicalAgeRange);
0 ignored issues
show
Documentation introduced by
$typicalAgeRange is of type string, but the function expects a object<CultuurNet\UDB3\Offer\AgeRange>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
439
    }
440
441
    /**
442
     * @return TypicalAgeRangeDeleted
443
     */
444
    protected function createTypicalAgeRangeDeletedEvent()
445
    {
446
        return new TypicalAgeRangeDeleted($this->eventId);
447
    }
448
449
    /**
450
     * @param string $organizerId
451
     * @return OrganizerUpdated
452
     */
453
    protected function createOrganizerUpdatedEvent($organizerId)
454
    {
455
        return new OrganizerUpdated($this->eventId, $organizerId);
456
    }
457
458
    /**
459
     * @param string $organizerId
460
     * @return OrganizerDeleted
461
     */
462
    protected function createOrganizerDeletedEvent($organizerId)
463
    {
464
        return new OrganizerDeleted($this->eventId, $organizerId);
465
    }
466
467
    /**
468
     * @param ContactPoint $contactPoint
469
     * @return ContactPointUpdated
470
     */
471
    protected function createContactPointUpdatedEvent(ContactPoint $contactPoint)
472
    {
473
        return new ContactPointUpdated($this->eventId, $contactPoint);
474
    }
475
476
    /**
477
     * @param BookingInfo $bookingInfo
478
     * @return BookingInfoUpdated
479
     */
480
    protected function createBookingInfoUpdatedEvent(BookingInfo $bookingInfo)
481
    {
482
        return new BookingInfoUpdated($this->eventId, $bookingInfo);
483
    }
484
485
    /**
486
     * @param PriceInfo $priceInfo
487
     * @return PriceInfoUpdated
488
     */
489
    protected function createPriceInfoUpdatedEvent(PriceInfo $priceInfo)
490
    {
491
        return new PriceInfoUpdated($this->eventId, $priceInfo);
492
    }
493
494
    /**
495
     * @return EventDeleted
496
     */
497
    protected function createOfferDeletedEvent()
498
    {
499
        return new EventDeleted($this->eventId);
500
    }
501
502
    /**
503
     * @inheritdoc
504
     */
505
    protected function createPublishedEvent(\DateTimeInterface $publicationDate)
506
    {
507
        return new Published($this->eventId, $publicationDate);
508
    }
509
510
    /**
511
     * @inheritdoc
512
     */
513
    protected function createApprovedEvent()
514
    {
515
        return new Approved($this->eventId);
516
    }
517
518
    /**
519
     * @inheritdoc
520
     */
521
    protected function createRejectedEvent(StringLiteral $reason)
522
    {
523
        return new Rejected($this->eventId, $reason);
524
    }
525
526
    /**
527
     * @inheritDoc
528
     */
529
    protected function createFlaggedAsDuplicate()
530
    {
531
        return new FlaggedAsDuplicate($this->eventId);
532
    }
533
534
    /**
535
     * @inheritDoc
536
     */
537
    protected function createFlaggedAsInappropriate()
538
    {
539
        return new FlaggedAsInappropriate($this->eventId);
540
    }
541
542
    /**
543
     * Use reflection to get check if the aggregate has uncommitted events.
544
     * @return bool
545
     */
546
    private function hasUncommittedEvents()
547
    {
548
        $reflector = new \ReflectionClass(EventSourcedAggregateRoot::class);
549
        $property = $reflector->getProperty('uncommittedEvents');
550
551
        $property->setAccessible(true);
552
        $uncommittedEvents = $property->getValue($this);
553
554
        return !empty($uncommittedEvents);
555
    }
556
557
    public function conclude()
558
    {
559
        if (!$this->concluded) {
560
            $this->apply(new Concluded($this->eventId));
561
        }
562
    }
563
564
    /**
565
     * @param Concluded $concluded
566
     */
567
    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...
568
    {
569
        $this->concluded = true;
570
    }
571
}
572