Completed
Pull Request — master (#336)
by Luc
04:29
created

Place::createOrganizerUpdatedEvent()   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
    public function __construct()
72
    {
73
        parent::__construct();
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function getAggregateRootId()
80
    {
81
        return $this->placeId;
82
    }
83
84
    /**
85
     * Factory method to create a new Place.
86
     *
87
     * @todo Refactor this method so it can be called create. Currently the
88
     * normal behavior for create is taken by the legacy udb2 logic.
89
     * The PlaceImportedFromUDB2 could be a superclass of Place.
90
     *
91
     * @param string $id
92
     * @param Title $title
93
     * @param EventType $eventType
94
     * @param Address $address
95
     * @param CalendarInterface $calendar
96
     * @param Theme|null $theme
97
     * @param DateTimeImmutable|null $publicationDate
98
     *
99
     * @return self
100
     */
101 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...
102
        $id,
103
        Title $title,
104
        EventType $eventType,
105
        Address $address,
106
        CalendarInterface $calendar,
107
        Theme $theme = null,
108
        DateTimeImmutable $publicationDate = null
109
    ) {
110
        $place = new self();
111
        $place->apply(new PlaceCreated(
112
            $id,
113
            $title,
114
            $eventType,
115
            $address,
116
            $calendar,
117
            $theme,
118
            $publicationDate
119
        ));
120
121
        return $place;
122
    }
123
124
    /**
125
     * Apply the place created event.
126
     * @param PlaceCreated $placeCreated
127
     */
128
    protected function applyPlaceCreated(PlaceCreated $placeCreated)
129
    {
130
        $this->placeId = $placeCreated->getPlaceId();
131
        $this->workflowStatus = WorkflowStatus::DRAFT();
132
    }
133
134
    /**
135
     * Update the major info.
136
     *
137
     * @param Title $title
138
     * @param EventType $eventType
139
     * @param Address $address
140
     * @param CalendarInterface $calendar
141
     * @param Theme $theme
142
     */
143
    public function updateMajorInfo(
144
        Title $title,
145
        EventType $eventType,
146
        Address $address,
147
        CalendarInterface $calendar,
148
        Theme $theme = null
149
    ) {
150
        $this->apply(
151
            new MajorInfoUpdated(
152
                $this->placeId,
153
                $title,
154
                $eventType,
155
                $address,
156
                $calendar,
157
                $theme
158
            )
159
        );
160
    }
161
162
    /**
163
     * @param Address $address
164
     * @param Language $language
165
     */
166
    public function updateAddress(Address $address, Language $language)
167
    {
168
        if ($language->getCode() === $this->mainLanguage->getCode()) {
169
            $event = new AddressUpdated($this->placeId, $address);
170
        } else {
171
            $event = new AddressTranslated($this->placeId, $address, $language);
172
        }
173
174
        $this->apply($event);
175
    }
176
177
    /**
178
     * @param Coordinates $coordinates
179
     */
180
    public function updateGeoCoordinates(
181
        Coordinates $coordinates
182
    ) {
183
        // Note: DON'T compare to previous coordinates and apply only on
184
        // changes. Various projectors expect GeoCoordinatesUpdated after
185
        // MajorInfoUpdated and PlaceUpdatedFromUDB2, even if the address
186
        // and thus the coordinates haven't actually changed.
187
        $this->apply(new GeoCoordinatesUpdated($this->placeId, $coordinates));
188
    }
189
190
    /**
191
     * Import from UDB2.
192
     *
193
     * @param string $actorId
194
     *   The actor id.
195
     * @param string $cdbXml
196
     *   The cdb xml.
197
     * @param string $cdbXmlNamespaceUri
198
     *   The cdb xml namespace uri.
199
     *
200
     * @return Place
201
     */
202
    public static function importFromUDB2Actor(
203
        $actorId,
204
        $cdbXml,
205
        $cdbXmlNamespaceUri
206
    ) {
207
        $place = new static();
208
        $place->apply(
209
            new PlaceImportedFromUDB2(
210
                $actorId,
211
                $cdbXml,
212
                $cdbXmlNamespaceUri
213
            )
214
        );
215
216
        return $place;
217
    }
218
219
    /**
220
     * @param PlaceImportedFromUDB2 $placeImported
221
     */
222 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...
223
        PlaceImportedFromUDB2 $placeImported
224
    ) {
225
        $this->placeId = $placeImported->getActorId();
226
227
        $udb2Actor = ActorItemFactory::createActorFromCdbXml(
228
            $placeImported->getCdbXmlNamespaceUri(),
229
            $placeImported->getCdbXml()
230
        );
231
232
        $this->importWorkflowStatus($udb2Actor);
233
        $this->labels = LabelCollection::fromKeywords($udb2Actor->getKeywords(true));
234
    }
235
236
    /**
237
     * @param PlaceUpdatedFromUDB2 $placeUpdatedFromUDB2
238
     */
239 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...
240
        PlaceUpdatedFromUDB2 $placeUpdatedFromUDB2
241
    ) {
242
        $udb2Actor = ActorItemFactory::createActorFromCdbXml(
243
            $placeUpdatedFromUDB2->getCdbXmlNamespaceUri(),
244
            $placeUpdatedFromUDB2->getCdbXml()
245
        );
246
247
        $this->importWorkflowStatus($udb2Actor);
248
        $this->labels = LabelCollection::fromKeywords($udb2Actor->getKeywords(true));
249
    }
250
251
    /**
252
     * @inheritdoc
253
     */
254
    public function updateWithCdbXml($cdbXml, $cdbXmlNamespaceUri)
255
    {
256
        ActorItemFactory::createActorFromCdbXml($cdbXmlNamespaceUri, $cdbXml);
257
258
        $this->apply(
259
            new PlaceUpdatedFromUDB2(
260
                $this->placeId,
261
                $cdbXml,
262
                $cdbXmlNamespaceUri
263
            )
264
        );
265
    }
266
267
    /**
268
     * @param Label $label
269
     * @return LabelAdded
270
     */
271
    protected function createLabelAddedEvent(Label $label)
272
    {
273
        return new LabelAdded($this->placeId, $label);
274
    }
275
276
    /**
277
     * @param Label $label
278
     * @return LabelRemoved
279
     */
280
    protected function createLabelRemovedEvent(Label $label)
281
    {
282
        return new LabelRemoved($this->placeId, $label);
283
    }
284
285
    protected function createImageAddedEvent(Image $image)
286
    {
287
        return new ImageAdded($this->placeId, $image);
288
    }
289
290
    protected function createImageRemovedEvent(Image $image)
291
    {
292
        return new ImageRemoved($this->placeId, $image);
293
    }
294
295
    protected function createImageUpdatedEvent(
296
        AbstractUpdateImage $updateImageCommand
297
    ) {
298
        return new ImageUpdated(
299
            $this->placeId,
300
            $updateImageCommand->getMediaObjectId(),
301
            $updateImageCommand->getDescription(),
302
            $updateImageCommand->getCopyrightHolder()
303
        );
304
    }
305
306
    protected function createMainImageSelectedEvent(Image $image)
307
    {
308
        return new MainImageSelected($this->placeId, $image);
309
    }
310
311
    /**
312
     * @param Language $language
313
     * @param StringLiteral $title
314
     * @return TitleTranslated
315
     */
316
    protected function createTitleTranslatedEvent(Language $language, StringLiteral $title)
317
    {
318
        return new TitleTranslated($this->placeId, $language, $title);
319
    }
320
321
    /**
322
     * @param Title $title
323
     * @return TitleUpdated
324
     */
325
    protected function createTitleUpdatedEvent(Title $title)
326
    {
327
        return new TitleUpdated($this->placeId, $title);
328
    }
329
330
    /**
331
     * @param Language $language
332
     * @param StringLiteral $description
333
     * @return DescriptionTranslated
334
     */
335
    protected function createDescriptionTranslatedEvent(Language $language, StringLiteral $description)
336
    {
337
        return new DescriptionTranslated($this->placeId, $language, $description);
338
    }
339
340
    /**
341
     * @param string $description
342
     * @return DescriptionUpdated
343
     */
344
    protected function createDescriptionUpdatedEvent($description)
345
    {
346
        return new DescriptionUpdated($this->placeId, $description);
347
    }
348
349
    /**
350
     * @inheritdoc
351
     */
352
    protected function createCalendarUpdatedEvent(Calendar $calendar)
353
    {
354
        return new CalendarUpdated($this->placeId, $calendar);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \CultuurNet\U...s->placeId, $calendar); (CultuurNet\UDB3\Place\Events\CalendarUpdated) is incompatible with the return type declared by the abstract method CultuurNet\UDB3\Offer\Of...ateCalendarUpdatedEvent of type CultuurNet\UDB3\Offer\AbstractCalendarUpdated.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
355
    }
356
    
357
    /**
358
     * @param string $typicalAgeRange
359
     * @return TypicalAgeRangeUpdated
360
     */
361
    protected function createTypicalAgeRangeUpdatedEvent($typicalAgeRange)
362
    {
363
        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...
364
    }
365
366
    /**
367
     * @return TypicalAgeRangeDeleted
368
     */
369
    protected function createTypicalAgeRangeDeletedEvent()
370
    {
371
        return new TypicalAgeRangeDeleted($this->placeId);
372
    }
373
374
    /**
375
     * @param string $organizerId
376
     * @return OrganizerUpdated
377
     */
378
    protected function createOrganizerUpdatedEvent($organizerId)
379
    {
380
        return new OrganizerUpdated($this->placeId, $organizerId);
381
    }
382
383
    /**
384
     * @param string $organizerId
385
     * @return OrganizerDeleted
386
     */
387
    protected function createOrganizerDeletedEvent($organizerId)
388
    {
389
        return new OrganizerDeleted($this->placeId, $organizerId);
390
    }
391
392
    /**
393
     * @param ContactPoint $contactPoint
394
     * @return ContactPointUpdated
395
     */
396
    protected function createContactPointUpdatedEvent(ContactPoint $contactPoint)
397
    {
398
        return new ContactPointUpdated($this->placeId, $contactPoint);
399
    }
400
401
    /**
402
     * @param BookingInfo $bookingInfo
403
     * @return BookingInfoUpdated
404
     */
405
    protected function createBookingInfoUpdatedEvent(BookingInfo $bookingInfo)
406
    {
407
        return new BookingInfoUpdated($this->placeId, $bookingInfo);
408
    }
409
410
    /**
411
     * @param PriceInfo $priceInfo
412
     * @return PriceInfoUpdated
413
     */
414
    protected function createPriceInfoUpdatedEvent(PriceInfo $priceInfo)
415
    {
416
        return new PriceInfoUpdated($this->placeId, $priceInfo);
417
    }
418
419
    /**
420
     * @return PlaceDeleted
421
     */
422
    protected function createOfferDeletedEvent()
423
    {
424
        return new PlaceDeleted($this->placeId);
425
    }
426
427
    /**
428
     * @inheritDoc
429
     */
430
    protected function createPublishedEvent(\DateTimeInterface $publicationDate)
431
    {
432
        return new Published($this->placeId, $publicationDate);
433
    }
434
435
    /**
436
     * @inheritDoc
437
     */
438
    protected function createApprovedEvent()
439
    {
440
        return new Approved($this->placeId);
441
    }
442
443
    /**
444
     * @inheritDoc
445
     */
446
    protected function createRejectedEvent(StringLiteral $reason)
447
    {
448
        return new Rejected($this->placeId, $reason);
449
    }
450
451
    /**
452
     * @inheritDoc
453
     */
454
    protected function createFlaggedAsDuplicate()
455
    {
456
        return new FlaggedAsDuplicate($this->placeId);
457
    }
458
459
    /**
460
     * @inheritDoc
461
     */
462
    protected function createFlaggedAsInappropriate()
463
    {
464
        return new FlaggedAsInappropriate($this->placeId);
465
    }
466
467
    /**
468
     * @inheritDoc
469
     * @return ImagesImportedFromUDB2
470
     */
471
    protected function createImagesImportedFromUDB2(ImageCollection $images)
472
    {
473
        return new ImagesImportedFromUDB2($this->placeId, $images);
474
    }
475
476
    /**
477
     * @inheritDoc
478
     * @return ImagesUpdatedFromUDB2
479
     */
480
    protected function createImagesUpdatedFromUDB2(ImageCollection $images)
481
    {
482
        return new ImagesUpdatedFromUDB2($this->placeId, $images);
483
    }
484
485
    protected function createTypeUpdatedEvent(EventType $type)
486
    {
487
        return new TypeUpdated($this->placeId, $type);
488
    }
489
490
    protected function createThemeUpdatedEvent(Theme $theme)
491
    {
492
        return new ThemeUpdated($this->placeId, $theme);
493
    }
494
495
    /**
496
     * @inheritdoc
497
     */
498
    protected function createFacilitiesUpdatedEvent(array $facilities)
499
    {
500
        return new FacilitiesUpdated($this->placeId, $facilities);
501
    }
502
}
503