Completed
Push — master ( c476c7...501e4d )
by
unknown
05:07
created

Event::updateLocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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