Completed
Pull Request — master (#349)
by Luc
08:34 queued 03:17
created

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