Completed
Pull Request — master (#321)
by
unknown
04:34
created

Place::createTitleUpdatedEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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