Completed
Pull Request — master (#342)
by Luc
05:03
created

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