Completed
Push — master ( e5a769...e9d24b )
by
unknown
15s queued 10s
created

Place::createMainImageSelectedEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\Place;
4
5
use CultuurNet\Geocoding\Coordinate\Coordinates;
6
use CultuurNet\UDB3\Address\Address;
7
use CultuurNet\UDB3\BookingInfo;
8
use CultuurNet\UDB3\Calendar;
9
use CultuurNet\UDB3\CalendarInterface;
10
use CultuurNet\UDB3\Cdb\ActorItemFactory;
11
use CultuurNet\UDB3\Cdb\UpdateableWithCdbXmlInterface;
12
use CultuurNet\UDB3\ContactPoint;
13
use CultuurNet\UDB3\Description;
14
use CultuurNet\UDB3\Event\EventType;
15
use CultuurNet\UDB3\Label;
16
use CultuurNet\UDB3\LabelCollection;
17
use CultuurNet\UDB3\Media\ImageCollection;
18
use CultuurNet\UDB3\Model\ValueObject\Taxonomy\Label\Labels;
19
use CultuurNet\UDB3\Language;
20
use CultuurNet\UDB3\Offer\AgeRange;
21
use CultuurNet\UDB3\Offer\Offer;
22
use CultuurNet\UDB3\Media\Image;
23
use CultuurNet\UDB3\Offer\WorkflowStatus;
24
use CultuurNet\UDB3\Place\Events\AddressTranslated;
25
use CultuurNet\UDB3\Place\Events\AddressUpdated;
26
use CultuurNet\UDB3\Place\Events\BookingInfoUpdated;
27
use CultuurNet\UDB3\Place\Events\CalendarUpdated;
28
use CultuurNet\UDB3\Place\Events\ContactPointUpdated;
29
use CultuurNet\UDB3\Place\Events\DescriptionTranslated;
30
use CultuurNet\UDB3\Place\Events\DescriptionUpdated;
31
use CultuurNet\UDB3\Place\Events\FacilitiesUpdated;
32
use CultuurNet\UDB3\Place\Events\GeoCoordinatesUpdated;
33
use CultuurNet\UDB3\Place\Events\Image\ImagesImportedFromUDB2;
34
use CultuurNet\UDB3\Place\Events\Image\ImagesUpdatedFromUDB2;
35
use CultuurNet\UDB3\Place\Events\ImageAdded;
36
use CultuurNet\UDB3\Place\Events\ImageRemoved;
37
use CultuurNet\UDB3\Place\Events\ImageUpdated;
38
use CultuurNet\UDB3\Place\Events\LabelsImported;
39
use CultuurNet\UDB3\Place\Events\MainImageSelected;
40
use CultuurNet\UDB3\Place\Events\LabelAdded;
41
use CultuurNet\UDB3\Place\Events\LabelRemoved;
42
use CultuurNet\UDB3\Place\Events\MajorInfoUpdated;
43
use CultuurNet\UDB3\Place\Events\MarkedAsDuplicate;
44
use CultuurNet\UDB3\Place\Events\MarkedAsCanonical;
45
use CultuurNet\UDB3\Place\Events\Moderation\Approved;
46
use CultuurNet\UDB3\Place\Events\Moderation\FlaggedAsDuplicate;
47
use CultuurNet\UDB3\Place\Events\Moderation\FlaggedAsInappropriate;
48
use CultuurNet\UDB3\Place\Events\Moderation\Published;
49
use CultuurNet\UDB3\Place\Events\Moderation\Rejected;
50
use CultuurNet\UDB3\Place\Events\OrganizerDeleted;
51
use CultuurNet\UDB3\Place\Events\OrganizerUpdated;
52
use CultuurNet\UDB3\Place\Events\PlaceCreated;
53
use CultuurNet\UDB3\Place\Events\PlaceDeleted;
54
use CultuurNet\UDB3\Place\Events\PlaceImportedFromUDB2;
55
use CultuurNet\UDB3\Place\Events\PlaceUpdatedFromUDB2;
56
use CultuurNet\UDB3\Place\Events\PriceInfoUpdated;
57
use CultuurNet\UDB3\Place\Events\ThemeUpdated;
58
use CultuurNet\UDB3\Place\Events\TitleTranslated;
59
use CultuurNet\UDB3\Place\Events\TitleUpdated;
60
use CultuurNet\UDB3\Place\Events\TypeUpdated;
61
use CultuurNet\UDB3\Place\Events\TypicalAgeRangeDeleted;
62
use CultuurNet\UDB3\Place\Events\TypicalAgeRangeUpdated;
63
use CultuurNet\UDB3\PriceInfo\PriceInfo;
64
use CultuurNet\UDB3\Theme;
65
use CultuurNet\UDB3\Title;
66
use DateTimeImmutable;
67
use ValueObjects\Identity\UUID;
68
use ValueObjects\StringLiteral\StringLiteral;
69
70
class Place extends Offer implements UpdateableWithCdbXmlInterface
71
{
72
    /**
73
     * @var string
74
     */
75
    private $placeId;
76
77
    /**
78
     * @var Address[]
79
     */
80
    private $addresses;
81
82
    /**
83
     * @var boolean
84
     */
85
    private $isDuplicate = false;
86
87
    public function __construct()
88
    {
89
        parent::__construct();
90
91
        $this->addresses = [];
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function getAggregateRootId()
98
    {
99
        return $this->placeId;
100
    }
101
102
    /**
103
     * @todo Rename this method to create() after moving this part of the codebase to udb3-silex
104
     */
105
    public static function createPlace(
106
        string $id,
107
        Language $mainLanguage,
108
        Title $title,
109
        EventType $eventType,
110
        Address $address,
111
        CalendarInterface $calendar,
112
        Theme $theme = null,
113
        DateTimeImmutable $publicationDate = null
114
    ): self {
115
        $place = new self();
116
        $place->apply(new PlaceCreated(
117
            $id,
118
            $mainLanguage,
119
            $title,
120
            $eventType,
121
            $address,
122
            $calendar,
123
            $theme,
124
            $publicationDate
125
        ));
126
127
        return $place;
128
    }
129
130 View Code Duplication
    protected function applyPlaceCreated(PlaceCreated $placeCreated): void
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...
131
    {
132
        $this->mainLanguage = $placeCreated->getMainLanguage();
133
        $this->titles[$this->mainLanguage->getCode()] = $placeCreated->getTitle();
134
        $this->calendar = $placeCreated->getCalendar();
135
        $this->contactPoint = new ContactPoint();
136
        $this->bookingInfo = new BookingInfo();
137
        $this->typeId = $placeCreated->getEventType()->getId();
138
        $this->themeId = $placeCreated->getTheme() ? $placeCreated->getTheme()->getId() : null;
139
        $this->addresses[$this->mainLanguage->getCode()] = $placeCreated->getAddress();
140
        $this->placeId = $placeCreated->getPlaceId();
141
        $this->workflowStatus = WorkflowStatus::DRAFT();
142
    }
143
144
    public function updateMajorInfo(
145
        Title $title,
146
        EventType $eventType,
147
        Address $address,
148
        CalendarInterface $calendar,
149
        Theme $theme = null
150
    ): void {
151
        $this->apply(
152
            new MajorInfoUpdated(
153
                $this->placeId,
154
                $title,
155
                $eventType,
156
                $address,
157
                $calendar,
158
                $theme
159
            )
160
        );
161
    }
162
163
    protected function applyMajorInfoUpdated(MajorInfoUpdated $majorInfoUpdated): void
164
    {
165
        $this->addresses[$this->mainLanguage->getCode()] = $majorInfoUpdated->getAddress();
166
    }
167
168 View Code Duplication
    public function updateAddress(Address $address, Language $language): void
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...
169
    {
170
        if ($language->getCode() === $this->mainLanguage->getCode()) {
171
            $event = new AddressUpdated($this->placeId, $address);
172
        } else {
173
            $event = new AddressTranslated($this->placeId, $address, $language);
174
        }
175
176
        if ($this->allowAddressUpdate($address, $language)) {
177
            $this->apply($event);
178
        }
179
    }
180
181
    protected function applyAddressUpdated(AddressUpdated $addressUpdated): void
182
    {
183
        $this->addresses[$this->mainLanguage->getCode()] = $addressUpdated->getAddress();
184
    }
185
186
    protected function applyAddressTranslated(AddressTranslated $addressTranslated): void
187
    {
188
        $this->addresses[$addressTranslated->getLanguage()->getCode()] = $addressTranslated->getAddress();
189
    }
190
191 View Code Duplication
    public function markAsDuplicateOf(string $placeIdOfCanonical): void
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...
192
    {
193
        if ($this->isDeleted) {
194
            throw CannotMarkPlaceAsDuplicate::becauseItIsDeleted($this->placeId);
195
        }
196
197
        if ($this->isDuplicate) {
198
            throw CannotMarkPlaceAsDuplicate::becauseItIsAlreadyADuplicate($this->placeId);
199
        }
200
201
        $this->apply(new MarkedAsDuplicate($this->placeId, $placeIdOfCanonical));
202
    }
203
204 View Code Duplication
    public function markAsCanonicalFor(string $placeIdOfDuplicate): void
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...
205
    {
206
        if ($this->isDeleted) {
207
            throw CannotMarkPlaceAsCanonical::becauseItIsDeleted($this->placeId);
208
        }
209
210
        if ($this->isDuplicate) {
211
            throw CannotMarkPlaceAsCanonical::becauseItIsAlreadyADuplicate($this->placeId);
212
        }
213
214
        $this->apply(new MarkedAsCanonical($this->placeId, $placeIdOfDuplicate));
215
    }
216
217
    private function allowAddressUpdate(Address $address, Language $language): bool
218
    {
219
        // No current address in the provided language so update with new address is allowed.
220
        if (!isset($this->addresses[$language->getCode()])) {
221
            return true;
222
        }
223
224
        // The current address in de the provided language is different then the new address, so update allowed.
225
        if (!$this->addresses[$language->getCode()]->sameAs($address)) {
226
            return true;
227
        }
228
229
        return false;
230
    }
231
232
    public static function importFromUDB2Actor(
233
        string $actorId,
234
        string $cdbXml,
235
        string $cdbXmlNamespaceUri
236
    ): self {
237
        $place = new static();
238
        $place->apply(
239
            new PlaceImportedFromUDB2(
240
                $actorId,
241
                $cdbXml,
242
                $cdbXmlNamespaceUri
243
            )
244
        );
245
246
        return $place;
247
    }
248
249
    protected function applyPlaceImportedFromUDB2(PlaceImportedFromUDB2 $placeImported): void
250
    {
251
        $this->placeId = $placeImported->getActorId();
252
253
        // When importing from UDB2 the default main language is always 'nl'.
254
        $this->mainLanguage = new Language('nl');
255
256
        $udb2Actor = ActorItemFactory::createActorFromCdbXml(
257
            $placeImported->getCdbXmlNamespaceUri(),
258
            $placeImported->getCdbXml()
259
        );
260
261
        // Just clear the facilities.
262
        $this->facilities = [];
263
264
        // Just clear the contact point.
265
        $this->contactPoint = null;
266
267
        // Just clear the calendar.
268
        $this->calendar = null;
269
270
        // Note: an actor has no typical age range so after it can't be changed
271
        // by an UDB2 update. Nothing has to be done.
272
273
        // Just clear the booking info.
274
        $this->bookingInfo = null;
275
276
        // Just clear the price info.
277
        $this->priceInfo = null;
278
279
        $this->importWorkflowStatus($udb2Actor);
280
        $this->labels = LabelCollection::fromKeywords($udb2Actor->getKeywords(true));
281
    }
282
283
    protected function applyPlaceUpdatedFromUDB2(PlaceUpdatedFromUDB2 $placeUpdatedFromUDB2): void
284
    {
285
        // Note: when updating from UDB2 never change the main language.
286
287
        $udb2Actor = ActorItemFactory::createActorFromCdbXml(
288
            $placeUpdatedFromUDB2->getCdbXmlNamespaceUri(),
289
            $placeUpdatedFromUDB2->getCdbXml()
290
        );
291
292
        // Just clear the facilities.
293
        $this->facilities = [];
294
295
        // Just clear the contact point.
296
        $this->contactPoint = null;
297
298
        // Just clear the calendar.
299
        $this->calendar = null;
300
301
        // Note: an actor has no typical age range so after it can't be changed
302
        // by an UDB2 update. Nothing has to be done.
303
304
        // Just clear the booking info.
305
        $this->bookingInfo = null;
306
307
        // Just clear the price info.
308
        $this->priceInfo = null;
309
310
        $this->importWorkflowStatus($udb2Actor);
311
        $this->labels = LabelCollection::fromKeywords($udb2Actor->getKeywords(true));
312
313
        unset($this->addresses[$this->mainLanguage->getCode()]);
314
    }
315
316
    protected function applyPlaceDeleted(PlaceDeleted $event): void
0 ignored issues
show
Unused Code introduced by
The parameter $event 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...
317
    {
318
        $this->isDeleted = true;
319
    }
320
321
    protected function applyMarkedAsDuplicate(MarkedAsDuplicate $event): void
0 ignored issues
show
Unused Code introduced by
The parameter $event 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...
322
    {
323
        $this->isDuplicate = true;
324
    }
325
326
    /**
327
     * @inheritdoc
328
     */
329
    public function updateWithCdbXml($cdbXml, $cdbXmlNamespaceUri)
330
    {
331
        ActorItemFactory::createActorFromCdbXml($cdbXmlNamespaceUri, $cdbXml);
332
333
        $this->apply(
334
            new PlaceUpdatedFromUDB2(
335
                $this->placeId,
336
                $cdbXml,
337
                $cdbXmlNamespaceUri
338
            )
339
        );
340
    }
341
342
    /**
343
     * @param Label $label
344
     * @return LabelAdded
345
     */
346
    protected function createLabelAddedEvent(Label $label)
347
    {
348
        return new LabelAdded($this->placeId, $label);
349
    }
350
351
    /**
352
     * @param Label $label
353
     * @return LabelRemoved
354
     */
355
    protected function createLabelRemovedEvent(Label $label)
356
    {
357
        return new LabelRemoved($this->placeId, $label);
358
    }
359
360
    /**
361
     * @inheritdoc
362
     */
363
    protected function createLabelsImportedEvent(Labels $labels)
364
    {
365
        return new LabelsImported($this->placeId, $labels);
366
    }
367
368
    protected function createImageAddedEvent(Image $image)
369
    {
370
        return new ImageAdded($this->placeId, $image);
371
    }
372
373
    protected function createImageRemovedEvent(Image $image)
374
    {
375
        return new ImageRemoved($this->placeId, $image);
376
    }
377
378
    protected function createImageUpdatedEvent(
379
        UUID $mediaObjectId,
380
        StringLiteral $description,
381
        StringLiteral $copyrightHolder
382
    ) {
383
        return new ImageUpdated(
384
            $this->placeId,
385
            $mediaObjectId,
386
            $description,
387
            $copyrightHolder
388
        );
389
    }
390
391
    protected function createMainImageSelectedEvent(Image $image)
392
    {
393
        return new MainImageSelected($this->placeId, $image);
394
    }
395
396
    /**
397
     * @inheritdoc
398
     */
399
    protected function createTitleTranslatedEvent(Language $language, Title $title)
400
    {
401
        return new TitleTranslated($this->placeId, $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->placeId, $title);
411
    }
412
413
    /**
414
     * @inheritdoc
415
     */
416
    protected function createDescriptionTranslatedEvent(Language $language, Description $description)
417
    {
418
        return new DescriptionTranslated($this->placeId, $language, $description);
419
    }
420
421
    /**
422
     * @inheritdoc
423
     */
424
    protected function createDescriptionUpdatedEvent(Description $description)
425
    {
426
        return new DescriptionUpdated($this->placeId, $description);
427
    }
428
429
    /**
430
     * @inheritdoc
431
     */
432
    protected function createCalendarUpdatedEvent(Calendar $calendar)
433
    {
434
        return new CalendarUpdated($this->placeId, $calendar);
435
    }
436
437
    /**
438
     * @param AgeRange $typicalAgeRange
439
     * @return TypicalAgeRangeUpdated
440
     */
441
    protected function createTypicalAgeRangeUpdatedEvent($typicalAgeRange)
442
    {
443
        return new TypicalAgeRangeUpdated($this->placeId, $typicalAgeRange);
444
    }
445
446
    /**
447
     * @return TypicalAgeRangeDeleted
448
     */
449
    protected function createTypicalAgeRangeDeletedEvent()
450
    {
451
        return new TypicalAgeRangeDeleted($this->placeId);
452
    }
453
454
    /**
455
     * @param string $organizerId
456
     * @return OrganizerUpdated
457
     */
458
    protected function createOrganizerUpdatedEvent($organizerId)
459
    {
460
        return new OrganizerUpdated($this->placeId, $organizerId);
461
    }
462
463
    /**
464
     * @param string $organizerId
465
     * @return OrganizerDeleted
466
     */
467
    protected function createOrganizerDeletedEvent($organizerId)
468
    {
469
        return new OrganizerDeleted($this->placeId, $organizerId);
470
    }
471
472
    /**
473
     * @param ContactPoint $contactPoint
474
     * @return ContactPointUpdated
475
     */
476
    protected function createContactPointUpdatedEvent(ContactPoint $contactPoint)
477
    {
478
        return new ContactPointUpdated($this->placeId, $contactPoint);
479
    }
480
481
    /**
482
     * @inheritdoc
483
     */
484
    protected function createGeoCoordinatesUpdatedEvent(Coordinates $coordinates)
485
    {
486
        return new GeoCoordinatesUpdated($this->placeId, $coordinates);
487
    }
488
489
    /**
490
     * @param BookingInfo $bookingInfo
491
     * @return BookingInfoUpdated
492
     */
493
    protected function createBookingInfoUpdatedEvent(BookingInfo $bookingInfo)
494
    {
495
        return new BookingInfoUpdated($this->placeId, $bookingInfo);
496
    }
497
498
    /**
499
     * @param PriceInfo $priceInfo
500
     * @return PriceInfoUpdated
501
     */
502
    protected function createPriceInfoUpdatedEvent(PriceInfo $priceInfo)
503
    {
504
        return new PriceInfoUpdated($this->placeId, $priceInfo);
505
    }
506
507
    /**
508
     * @return PlaceDeleted
509
     */
510
    protected function createOfferDeletedEvent()
511
    {
512
        return new PlaceDeleted($this->placeId);
513
    }
514
515
    /**
516
     * @inheritDoc
517
     */
518
    protected function createPublishedEvent(\DateTimeInterface $publicationDate)
519
    {
520
        return new Published($this->placeId, $publicationDate);
521
    }
522
523
    /**
524
     * @inheritDoc
525
     */
526
    protected function createApprovedEvent()
527
    {
528
        return new Approved($this->placeId);
529
    }
530
531
    /**
532
     * @inheritDoc
533
     */
534
    protected function createRejectedEvent(StringLiteral $reason)
535
    {
536
        return new Rejected($this->placeId, $reason);
537
    }
538
539
    /**
540
     * @inheritDoc
541
     */
542
    protected function createFlaggedAsDuplicate()
543
    {
544
        return new FlaggedAsDuplicate($this->placeId);
545
    }
546
547
    /**
548
     * @inheritDoc
549
     */
550
    protected function createFlaggedAsInappropriate()
551
    {
552
        return new FlaggedAsInappropriate($this->placeId);
553
    }
554
555
    /**
556
     * @inheritDoc
557
     * @return ImagesImportedFromUDB2
558
     */
559
    protected function createImagesImportedFromUDB2(ImageCollection $images)
560
    {
561
        return new ImagesImportedFromUDB2($this->placeId, $images);
562
    }
563
564
    /**
565
     * @inheritDoc
566
     * @return ImagesUpdatedFromUDB2
567
     */
568
    protected function createImagesUpdatedFromUDB2(ImageCollection $images)
569
    {
570
        return new ImagesUpdatedFromUDB2($this->placeId, $images);
571
    }
572
573
    protected function createTypeUpdatedEvent(EventType $type)
574
    {
575
        return new TypeUpdated($this->placeId, $type);
576
    }
577
578
    protected function createThemeUpdatedEvent(Theme $theme)
579
    {
580
        return new ThemeUpdated($this->placeId, $theme);
581
    }
582
583
    /**
584
     * @inheritdoc
585
     */
586
    protected function createFacilitiesUpdatedEvent(array $facilities)
587
    {
588
        return new FacilitiesUpdated($this->placeId, $facilities);
589
    }
590
}
591