Completed
Pull Request — master (#340)
by Luc
05:12
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
     * Import from UDB2.
179
     *
180
     * @param string $actorId
181
     *   The actor id.
182
     * @param string $cdbXml
183
     *   The cdb xml.
184
     * @param string $cdbXmlNamespaceUri
185
     *   The cdb xml namespace uri.
186
     *
187
     * @return Place
188
     */
189
    public static function importFromUDB2Actor(
190
        $actorId,
191
        $cdbXml,
192
        $cdbXmlNamespaceUri
193
    ) {
194
        $place = new static();
195
        $place->apply(
196
            new PlaceImportedFromUDB2(
197
                $actorId,
198
                $cdbXml,
199
                $cdbXmlNamespaceUri
200
            )
201
        );
202
203
        return $place;
204
    }
205
206
    /**
207
     * @param PlaceImportedFromUDB2 $placeImported
208
     */
209 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...
210
        PlaceImportedFromUDB2 $placeImported
211
    ) {
212
        $this->placeId = $placeImported->getActorId();
213
214
        $udb2Actor = ActorItemFactory::createActorFromCdbXml(
215
            $placeImported->getCdbXmlNamespaceUri(),
216
            $placeImported->getCdbXml()
217
        );
218
219
        $this->importWorkflowStatus($udb2Actor);
220
        $this->labels = LabelCollection::fromKeywords($udb2Actor->getKeywords(true));
221
    }
222
223
    /**
224
     * @param PlaceUpdatedFromUDB2 $placeUpdatedFromUDB2
225
     */
226 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...
227
        PlaceUpdatedFromUDB2 $placeUpdatedFromUDB2
228
    ) {
229
        $udb2Actor = ActorItemFactory::createActorFromCdbXml(
230
            $placeUpdatedFromUDB2->getCdbXmlNamespaceUri(),
231
            $placeUpdatedFromUDB2->getCdbXml()
232
        );
233
234
        $this->importWorkflowStatus($udb2Actor);
235
        $this->labels = LabelCollection::fromKeywords($udb2Actor->getKeywords(true));
236
    }
237
238
    /**
239
     * @inheritdoc
240
     */
241
    public function updateWithCdbXml($cdbXml, $cdbXmlNamespaceUri)
242
    {
243
        ActorItemFactory::createActorFromCdbXml($cdbXmlNamespaceUri, $cdbXml);
244
245
        $this->apply(
246
            new PlaceUpdatedFromUDB2(
247
                $this->placeId,
248
                $cdbXml,
249
                $cdbXmlNamespaceUri
250
            )
251
        );
252
    }
253
254
    /**
255
     * @param Label $label
256
     * @return LabelAdded
257
     */
258
    protected function createLabelAddedEvent(Label $label)
259
    {
260
        return new LabelAdded($this->placeId, $label);
261
    }
262
263
    /**
264
     * @param Label $label
265
     * @return LabelRemoved
266
     */
267
    protected function createLabelRemovedEvent(Label $label)
268
    {
269
        return new LabelRemoved($this->placeId, $label);
270
    }
271
272
    protected function createImageAddedEvent(Image $image)
273
    {
274
        return new ImageAdded($this->placeId, $image);
275
    }
276
277
    protected function createImageRemovedEvent(Image $image)
278
    {
279
        return new ImageRemoved($this->placeId, $image);
280
    }
281
282
    protected function createImageUpdatedEvent(
283
        AbstractUpdateImage $updateImageCommand
284
    ) {
285
        return new ImageUpdated(
286
            $this->placeId,
287
            $updateImageCommand->getMediaObjectId(),
288
            $updateImageCommand->getDescription(),
289
            $updateImageCommand->getCopyrightHolder()
290
        );
291
    }
292
293
    protected function createMainImageSelectedEvent(Image $image)
294
    {
295
        return new MainImageSelected($this->placeId, $image);
296
    }
297
298
    /**
299
     * @param Language $language
300
     * @param StringLiteral $title
301
     * @return TitleTranslated
302
     */
303
    protected function createTitleTranslatedEvent(Language $language, StringLiteral $title)
304
    {
305
        return new TitleTranslated($this->placeId, $language, $title);
306
    }
307
308
    /**
309
     * @param Title $title
310
     * @return TitleUpdated
311
     */
312
    protected function createTitleUpdatedEvent(Title $title)
313
    {
314
        return new TitleUpdated($this->placeId, $title);
315
    }
316
317
    /**
318
     * @param Language $language
319
     * @param StringLiteral $description
320
     * @return DescriptionTranslated
321
     */
322
    protected function createDescriptionTranslatedEvent(Language $language, StringLiteral $description)
323
    {
324
        return new DescriptionTranslated($this->placeId, $language, $description);
325
    }
326
327
    /**
328
     * @param string $description
329
     * @return DescriptionUpdated
330
     */
331
    protected function createDescriptionUpdatedEvent($description)
332
    {
333
        return new DescriptionUpdated($this->placeId, $description);
334
    }
335
336
    /**
337
     * @inheritdoc
338
     */
339
    protected function createCalendarUpdatedEvent(Calendar $calendar)
340
    {
341
        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...
342
    }
343
    
344
    /**
345
     * @param string $typicalAgeRange
346
     * @return TypicalAgeRangeUpdated
347
     */
348
    protected function createTypicalAgeRangeUpdatedEvent($typicalAgeRange)
349
    {
350
        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...
351
    }
352
353
    /**
354
     * @return TypicalAgeRangeDeleted
355
     */
356
    protected function createTypicalAgeRangeDeletedEvent()
357
    {
358
        return new TypicalAgeRangeDeleted($this->placeId);
359
    }
360
361
    /**
362
     * @param string $organizerId
363
     * @return OrganizerUpdated
364
     */
365
    protected function createOrganizerUpdatedEvent($organizerId)
366
    {
367
        return new OrganizerUpdated($this->placeId, $organizerId);
368
    }
369
370
    /**
371
     * @param string $organizerId
372
     * @return OrganizerDeleted
373
     */
374
    protected function createOrganizerDeletedEvent($organizerId)
375
    {
376
        return new OrganizerDeleted($this->placeId, $organizerId);
377
    }
378
379
    /**
380
     * @param ContactPoint $contactPoint
381
     * @return ContactPointUpdated
382
     */
383
    protected function createContactPointUpdatedEvent(ContactPoint $contactPoint)
384
    {
385
        return new ContactPointUpdated($this->placeId, $contactPoint);
386
    }
387
388
    /**
389
     * @inheritdoc
390
     */
391
    protected function createGeoCoordinatesUpdatedEvent(Coordinates $coordinates)
392
    {
393
        return new GeoCoordinatesUpdated($this->placeId, $coordinates);
394
    }
395
396
    /**
397
     * @param BookingInfo $bookingInfo
398
     * @return BookingInfoUpdated
399
     */
400
    protected function createBookingInfoUpdatedEvent(BookingInfo $bookingInfo)
401
    {
402
        return new BookingInfoUpdated($this->placeId, $bookingInfo);
403
    }
404
405
    /**
406
     * @param PriceInfo $priceInfo
407
     * @return PriceInfoUpdated
408
     */
409
    protected function createPriceInfoUpdatedEvent(PriceInfo $priceInfo)
410
    {
411
        return new PriceInfoUpdated($this->placeId, $priceInfo);
412
    }
413
414
    /**
415
     * @return PlaceDeleted
416
     */
417
    protected function createOfferDeletedEvent()
418
    {
419
        return new PlaceDeleted($this->placeId);
420
    }
421
422
    /**
423
     * @inheritDoc
424
     */
425
    protected function createPublishedEvent(\DateTimeInterface $publicationDate)
426
    {
427
        return new Published($this->placeId, $publicationDate);
428
    }
429
430
    /**
431
     * @inheritDoc
432
     */
433
    protected function createApprovedEvent()
434
    {
435
        return new Approved($this->placeId);
436
    }
437
438
    /**
439
     * @inheritDoc
440
     */
441
    protected function createRejectedEvent(StringLiteral $reason)
442
    {
443
        return new Rejected($this->placeId, $reason);
444
    }
445
446
    /**
447
     * @inheritDoc
448
     */
449
    protected function createFlaggedAsDuplicate()
450
    {
451
        return new FlaggedAsDuplicate($this->placeId);
452
    }
453
454
    /**
455
     * @inheritDoc
456
     */
457
    protected function createFlaggedAsInappropriate()
458
    {
459
        return new FlaggedAsInappropriate($this->placeId);
460
    }
461
462
    /**
463
     * @inheritDoc
464
     * @return ImagesImportedFromUDB2
465
     */
466
    protected function createImagesImportedFromUDB2(ImageCollection $images)
467
    {
468
        return new ImagesImportedFromUDB2($this->placeId, $images);
469
    }
470
471
    /**
472
     * @inheritDoc
473
     * @return ImagesUpdatedFromUDB2
474
     */
475
    protected function createImagesUpdatedFromUDB2(ImageCollection $images)
476
    {
477
        return new ImagesUpdatedFromUDB2($this->placeId, $images);
478
    }
479
480
    protected function createTypeUpdatedEvent(EventType $type)
481
    {
482
        return new TypeUpdated($this->placeId, $type);
483
    }
484
485
    protected function createThemeUpdatedEvent(Theme $theme)
486
    {
487
        return new ThemeUpdated($this->placeId, $theme);
488
    }
489
490
    /**
491
     * @inheritdoc
492
     */
493
    protected function createFacilitiesUpdatedEvent(array $facilities)
494
    {
495
        return new FacilitiesUpdated($this->placeId, $facilities);
496
    }
497
}
498