Completed
Pull Request — master (#309)
by Luc
05:53
created

Place::createTypicalAgeRangeDeletedEvent()   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 0
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\CalendarInterface;
9
use CultuurNet\UDB3\Cdb\ActorItemFactory;
10
use CultuurNet\UDB3\Cdb\UpdateableWithCdbXmlInterface;
11
use CultuurNet\UDB3\ContactPoint;
12
use CultuurNet\UDB3\Event\EventType;
13
use CultuurNet\UDB3\Label;
14
use CultuurNet\UDB3\LabelCollection;
15
use CultuurNet\UDB3\Media\ImageCollection;
16
use CultuurNet\UDB3\Offer\Commands\Image\AbstractUpdateImage;
17
use CultuurNet\UDB3\Language;
18
use CultuurNet\UDB3\Offer\Offer;
19
use CultuurNet\UDB3\Media\Image;
20
use CultuurNet\UDB3\Offer\WorkflowStatus;
21
use CultuurNet\UDB3\Place\Events\AddressTranslated;
22
use CultuurNet\UDB3\Place\Events\AddressUpdated;
23
use CultuurNet\UDB3\Place\Events\BookingInfoUpdated;
24
use CultuurNet\UDB3\Place\Events\ContactPointUpdated;
25
use CultuurNet\UDB3\Place\Events\DescriptionTranslated;
26
use CultuurNet\UDB3\Place\Events\DescriptionUpdated;
27
use CultuurNet\UDB3\Place\Events\FacilitiesUpdated;
28
use CultuurNet\UDB3\Place\Events\GeoCoordinatesUpdated;
29
use CultuurNet\UDB3\Place\Events\Image\ImagesImportedFromUDB2;
30
use CultuurNet\UDB3\Place\Events\Image\ImagesUpdatedFromUDB2;
31
use CultuurNet\UDB3\Place\Events\ImageAdded;
32
use CultuurNet\UDB3\Place\Events\ImageRemoved;
33
use CultuurNet\UDB3\Place\Events\ImageUpdated;
34
use CultuurNet\UDB3\Place\Events\MainImageSelected;
35
use CultuurNet\UDB3\Place\Events\LabelAdded;
36
use CultuurNet\UDB3\Place\Events\LabelRemoved;
37
use CultuurNet\UDB3\Place\Events\MajorInfoUpdated;
38
use CultuurNet\UDB3\Place\Events\Moderation\Approved;
39
use CultuurNet\UDB3\Place\Events\Moderation\FlaggedAsDuplicate;
40
use CultuurNet\UDB3\Place\Events\Moderation\FlaggedAsInappropriate;
41
use CultuurNet\UDB3\Place\Events\Moderation\Published;
42
use CultuurNet\UDB3\Place\Events\Moderation\Rejected;
43
use CultuurNet\UDB3\Place\Events\OrganizerDeleted;
44
use CultuurNet\UDB3\Place\Events\OrganizerUpdated;
45
use CultuurNet\UDB3\Place\Events\PlaceCreated;
46
use CultuurNet\UDB3\Place\Events\PlaceDeleted;
47
use CultuurNet\UDB3\Place\Events\PlaceImportedFromUDB2;
48
use CultuurNet\UDB3\Place\Events\PlaceUpdatedFromUDB2;
49
use CultuurNet\UDB3\Place\Events\PriceInfoUpdated;
50
use CultuurNet\UDB3\Place\Events\TitleTranslated;
51
use CultuurNet\UDB3\Place\Events\TypicalAgeRangeDeleted;
52
use CultuurNet\UDB3\Place\Events\TypicalAgeRangeUpdated;
53
use CultuurNet\UDB3\PriceInfo\PriceInfo;
54
use CultuurNet\UDB3\Theme;
55
use CultuurNet\UDB3\Title;
56
use DateTimeImmutable;
57
use ValueObjects\StringLiteral\StringLiteral;
58
59
class Place extends Offer implements UpdateableWithCdbXmlInterface
60
{
61
    /**
62
     * @var string
63
     */
64
    private $placeId;
65
66
    /**
67
     * @var Language
68
     */
69
    private $mainLanguage;
70
71
    public function __construct()
72
    {
73
        parent::__construct();
74
75
        // For now the main language is hardcoded as nl.
76
        // In the future it should be set dynamically on create.
77
        $this->mainLanguage = new Language('nl');
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function getAggregateRootId()
84
    {
85
        return $this->placeId;
86
    }
87
88
    /**
89
     * Factory method to create a new Place.
90
     *
91
     * @todo Refactor this method so it can be called create. Currently the
92
     * normal behavior for create is taken by the legacy udb2 logic.
93
     * The PlaceImportedFromUDB2 could be a superclass of Place.
94
     *
95
     * @param string $id
96
     * @param Title $title
97
     * @param EventType $eventType
98
     * @param Address $address
99
     * @param CalendarInterface $calendar
100
     * @param Theme|null $theme
101
     * @param DateTimeImmutable|null $publicationDate
102
     *
103
     * @return self
104
     */
105 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...
106
        $id,
107
        Title $title,
108
        EventType $eventType,
109
        Address $address,
110
        CalendarInterface $calendar,
111
        Theme $theme = null,
112
        DateTimeImmutable $publicationDate = null
113
    ) {
114
        $place = new self();
115
        $place->apply(new PlaceCreated(
116
            $id,
117
            $title,
118
            $eventType,
119
            $address,
120
            $calendar,
121
            $theme,
122
            $publicationDate
123
        ));
124
125
        return $place;
126
    }
127
128
    /**
129
     * Apply the place created event.
130
     * @param PlaceCreated $placeCreated
131
     */
132
    protected function applyPlaceCreated(PlaceCreated $placeCreated)
133
    {
134
        $this->placeId = $placeCreated->getPlaceId();
135
        $this->workflowStatus = WorkflowStatus::DRAFT();
136
    }
137
138
    /**
139
     * Update the facilities.
140
     *
141
     * @param array $facilities
142
     */
143
    public function updateFacilities(array $facilities)
144
    {
145
        $this->apply(new FacilitiesUpdated($this->placeId, $facilities));
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 Address $address
178
     * @param Language $language
179
     */
180
    public function updateAddress(Address $address, Language $language)
181
    {
182
        if ($language->getCode() === $this->mainLanguage->getCode()) {
183
            $event = new AddressUpdated($this->placeId, $address);
184
        } else {
185
            $event = new AddressTranslated($this->placeId, $address, $language);
186
        }
187
188
        $this->apply($event);
189
    }
190
191
    /**
192
     * @param Coordinates $coordinates
193
     */
194
    public function updateGeoCoordinates(
195
        Coordinates $coordinates
196
    ) {
197
        // Note: DON'T compare to previous coordinates and apply only on
198
        // changes. Various projectors expect GeoCoordinatesUpdated after
199
        // MajorInfoUpdated and PlaceUpdatedFromUDB2, even if the address
200
        // and thus the coordinates haven't actually changed.
201
        $this->apply(new GeoCoordinatesUpdated($this->placeId, $coordinates));
202
    }
203
204
    /**
205
     * Import from UDB2.
206
     *
207
     * @param string $actorId
208
     *   The actor id.
209
     * @param string $cdbXml
210
     *   The cdb xml.
211
     * @param string $cdbXmlNamespaceUri
212
     *   The cdb xml namespace uri.
213
     *
214
     * @return Place
215
     */
216
    public static function importFromUDB2Actor(
217
        $actorId,
218
        $cdbXml,
219
        $cdbXmlNamespaceUri
220
    ) {
221
        $place = new static();
222
        $place->apply(
223
            new PlaceImportedFromUDB2(
224
                $actorId,
225
                $cdbXml,
226
                $cdbXmlNamespaceUri
227
            )
228
        );
229
230
        return $place;
231
    }
232
233
    /**
234
     * @param PlaceImportedFromUDB2 $placeImported
235
     */
236 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...
237
        PlaceImportedFromUDB2 $placeImported
238
    ) {
239
        $this->placeId = $placeImported->getActorId();
240
241
        $udb2Actor = ActorItemFactory::createActorFromCdbXml(
242
            $placeImported->getCdbXmlNamespaceUri(),
243
            $placeImported->getCdbXml()
244
        );
245
246
        $this->importWorkflowStatus($udb2Actor);
247
        $this->labels = LabelCollection::fromKeywords($udb2Actor->getKeywords(true));
248
    }
249
250
    /**
251
     * @param PlaceUpdatedFromUDB2 $placeUpdatedFromUDB2
252
     */
253 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...
254
        PlaceUpdatedFromUDB2 $placeUpdatedFromUDB2
255
    ) {
256
        $udb2Actor = ActorItemFactory::createActorFromCdbXml(
257
            $placeUpdatedFromUDB2->getCdbXmlNamespaceUri(),
258
            $placeUpdatedFromUDB2->getCdbXml()
259
        );
260
261
        $this->importWorkflowStatus($udb2Actor);
262
        $this->labels = LabelCollection::fromKeywords($udb2Actor->getKeywords(true));
263
    }
264
265
    /**
266
     * @inheritdoc
267
     */
268
    public function updateWithCdbXml($cdbXml, $cdbXmlNamespaceUri)
269
    {
270
        ActorItemFactory::createActorFromCdbXml($cdbXmlNamespaceUri, $cdbXml);
271
272
        $this->apply(
273
            new PlaceUpdatedFromUDB2(
274
                $this->placeId,
275
                $cdbXml,
276
                $cdbXmlNamespaceUri
277
            )
278
        );
279
    }
280
281
    /**
282
     * @param Label $label
283
     * @return LabelAdded
284
     */
285
    protected function createLabelAddedEvent(Label $label)
286
    {
287
        return new LabelAdded($this->placeId, $label);
288
    }
289
290
    /**
291
     * @param Label $label
292
     * @return LabelRemoved
293
     */
294
    protected function createLabelRemovedEvent(Label $label)
295
    {
296
        return new LabelRemoved($this->placeId, $label);
297
    }
298
299
    protected function createImageAddedEvent(Image $image)
300
    {
301
        return new ImageAdded($this->placeId, $image);
302
    }
303
304
    protected function createImageRemovedEvent(Image $image)
305
    {
306
        return new ImageRemoved($this->placeId, $image);
307
    }
308
309
    protected function createImageUpdatedEvent(
310
        AbstractUpdateImage $updateImageCommand
311
    ) {
312
        return new ImageUpdated(
313
            $this->placeId,
314
            $updateImageCommand->getMediaObjectId(),
315
            $updateImageCommand->getDescription(),
316
            $updateImageCommand->getCopyrightHolder()
317
        );
318
    }
319
320
    protected function createMainImageSelectedEvent(Image $image)
321
    {
322
        return new MainImageSelected($this->placeId, $image);
323
    }
324
325
    /**
326
     * @param Language $language
327
     * @param StringLiteral $title
328
     * @return TitleTranslated
329
     */
330
    protected function createTitleTranslatedEvent(Language $language, StringLiteral $title)
331
    {
332
        return new TitleTranslated($this->placeId, $language, $title);
333
    }
334
335
    /**
336
     * @param Language $language
337
     * @param StringLiteral $description
338
     * @return DescriptionTranslated
339
     */
340
    protected function createDescriptionTranslatedEvent(Language $language, StringLiteral $description)
341
    {
342
        return new DescriptionTranslated($this->placeId, $language, $description);
343
    }
344
345
    /**
346
     * @param string $description
347
     * @return DescriptionUpdated
348
     */
349
    protected function createDescriptionUpdatedEvent($description)
350
    {
351
        return new DescriptionUpdated($this->placeId, $description);
352
    }
353
354
    /**
355
     * @param string $typicalAgeRange
356
     * @return TypicalAgeRangeUpdated
357
     */
358
    protected function createTypicalAgeRangeUpdatedEvent($typicalAgeRange)
359
    {
360
        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...
361
    }
362
363
    /**
364
     * @return TypicalAgeRangeDeleted
365
     */
366
    protected function createTypicalAgeRangeDeletedEvent()
367
    {
368
        return new TypicalAgeRangeDeleted($this->placeId);
369
    }
370
371
    /**
372
     * @param string $organizerId
373
     * @return OrganizerUpdated
374
     */
375
    protected function createOrganizerUpdatedEvent($organizerId)
376
    {
377
        return new OrganizerUpdated($this->placeId, $organizerId);
378
    }
379
380
    /**
381
     * @param string $organizerId
382
     * @return OrganizerDeleted
383
     */
384
    protected function createOrganizerDeletedEvent($organizerId)
385
    {
386
        return new OrganizerDeleted($this->placeId, $organizerId);
387
    }
388
389
    /**
390
     * @param ContactPoint $contactPoint
391
     * @return ContactPointUpdated
392
     */
393
    protected function createContactPointUpdatedEvent(ContactPoint $contactPoint)
394
    {
395
        return new ContactPointUpdated($this->placeId, $contactPoint);
396
    }
397
398
    /**
399
     * @param BookingInfo $bookingInfo
400
     * @return BookingInfoUpdated
401
     */
402
    protected function createBookingInfoUpdatedEvent(BookingInfo $bookingInfo)
403
    {
404
        return new BookingInfoUpdated($this->placeId, $bookingInfo);
405
    }
406
407
    /**
408
     * @param PriceInfo $priceInfo
409
     * @return PriceInfoUpdated
410
     */
411
    protected function createPriceInfoUpdatedEvent(PriceInfo $priceInfo)
412
    {
413
        return new PriceInfoUpdated($this->placeId, $priceInfo);
414
    }
415
416
    /**
417
     * @return PlaceDeleted
418
     */
419
    protected function createOfferDeletedEvent()
420
    {
421
        return new PlaceDeleted($this->placeId);
422
    }
423
424
    /**
425
     * @inheritDoc
426
     */
427
    protected function createPublishedEvent(\DateTimeInterface $publicationDate)
428
    {
429
        return new Published($this->placeId, $publicationDate);
430
    }
431
432
    /**
433
     * @inheritDoc
434
     */
435
    protected function createApprovedEvent()
436
    {
437
        return new Approved($this->placeId);
438
    }
439
440
    /**
441
     * @inheritDoc
442
     */
443
    protected function createRejectedEvent(StringLiteral $reason)
444
    {
445
        return new Rejected($this->placeId, $reason);
446
    }
447
448
    /**
449
     * @inheritDoc
450
     */
451
    protected function createFlaggedAsDuplicate()
452
    {
453
        return new FlaggedAsDuplicate($this->placeId);
454
    }
455
456
    /**
457
     * @inheritDoc
458
     */
459
    protected function createFlaggedAsInappropriate()
460
    {
461
        return new FlaggedAsInappropriate($this->placeId);
462
    }
463
464
    /**
465
     * @inheritDoc
466
     * @return ImagesImportedFromUDB2
467
     */
468
    protected function createImagesImportedFromUDB2(ImageCollection $images)
469
    {
470
        return new ImagesImportedFromUDB2($this->placeId, $images);
471
    }
472
473
    /**
474
     * @inheritDoc
475
     * @return ImagesUpdatedFromUDB2
476
     */
477
    protected function createImagesUpdatedFromUDB2(ImageCollection $images)
478
    {
479
        return new ImagesUpdatedFromUDB2($this->placeId, $images);
480
    }
481
}
482