Completed
Pull Request — master (#342)
by Luc
10:55
created

Place::allowAddressUpdate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 2
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 Title $title
100
     * @param EventType $eventType
101
     * @param Address $address
102
     * @param CalendarInterface $calendar
103
     * @param Theme|null $theme
104
     * @param DateTimeImmutable|null $publicationDate
105
     *
106
     * @return self
107
     */
108 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...
109
        $id,
110
        Title $title,
111
        EventType $eventType,
112
        Address $address,
113
        CalendarInterface $calendar,
114
        Theme $theme = null,
115
        DateTimeImmutable $publicationDate = null
116
    ) {
117
        $place = new self();
118
        $place->apply(new PlaceCreated(
119
            $id,
120
            $title,
121
            $eventType,
122
            $address,
123
            $calendar,
124
            $theme,
125
            $publicationDate
126
        ));
127
128
        return $place;
129
    }
130
131
    /**
132
     * Apply the place created event.
133
     * @param PlaceCreated $placeCreated
134
     */
135
    protected function applyPlaceCreated(PlaceCreated $placeCreated)
136
    {
137
        $this->addresses[$this->mainLanguage->getCode()] = $placeCreated->getAddress();
138
        $this->placeId = $placeCreated->getPlaceId();
139
        $this->workflowStatus = WorkflowStatus::DRAFT();
140
    }
141
142
    /**
143
     * Update the major info.
144
     *
145
     * @param Title $title
146
     * @param EventType $eventType
147
     * @param Address $address
148
     * @param CalendarInterface $calendar
149
     * @param Theme $theme
150
     */
151
    public function updateMajorInfo(
152
        Title $title,
153
        EventType $eventType,
154
        Address $address,
155
        CalendarInterface $calendar,
156
        Theme $theme = null
157
    ) {
158
        $this->apply(
159
            new MajorInfoUpdated(
160
                $this->placeId,
161
                $title,
162
                $eventType,
163
                $address,
164
                $calendar,
165
                $theme
166
            )
167
        );
168
    }
169
170
    /**
171
     * @param MajorInfoUpdated $majorInfoUpdated
172
     */
173
    public function applyMajorInfoUpdated(MajorInfoUpdated $majorInfoUpdated)
174
    {
175
        $this->addresses[$this->mainLanguage->getCode()] = $majorInfoUpdated->getAddress();
176
    }
177
178
    /**
179
     * @param Address $address
180
     * @param Language $language
181
     */
182
    public function updateAddress(Address $address, Language $language)
183
    {
184
        if ($language->getCode() === $this->mainLanguage->getCode()) {
185
            $event = new AddressUpdated($this->placeId, $address);
186
        } else {
187
            $event = new AddressTranslated($this->placeId, $address, $language);
188
        }
189
190
        if ($this->allowAddressUpdate($address, $language)) {
191
            $this->apply($event);
192
        }
193
    }
194
195
    /**
196
     * @param AddressUpdated $addressUpdated
197
     */
198
    protected function applyAddressUpdated(AddressUpdated $addressUpdated)
199
    {
200
        $this->addresses[$this->mainLanguage->getCode()] = $addressUpdated->getAddress();
201
    }
202
203
    /**
204
     * @param AddressTranslated $addressTranslated
205
     */
206
    protected function applyAddressTranslated(AddressTranslated $addressTranslated)
207
    {
208
        $this->addresses[$addressTranslated->getLanguage()->getCode()] = $addressTranslated->getAddress();
209
    }
210
211
    /**
212
     * @param Address $address
213
     * @param Language $language
214
     * @return bool
215
     */
216
    private function allowAddressUpdate(Address $address, Language $language)
217
    {
218
        // No current address in the provided language so update with new address is allowed.
219
        if (!isset($this->addresses[$language->getCode()])) {
220
            return true;
221
        }
222
223
        // The current address in de the provided language is different then the new address, so update allowed.
224
        if (!$this->addresses[$language->getCode()]->sameAs($address)) {
225
            return true;
226
        }
227
228
        return false;
229
    }
230
231
    /**
232
     * Import from UDB2.
233
     *
234
     * @param string $actorId
235
     *   The actor id.
236
     * @param string $cdbXml
237
     *   The cdb xml.
238
     * @param string $cdbXmlNamespaceUri
239
     *   The cdb xml namespace uri.
240
     *
241
     * @return Place
242
     */
243
    public static function importFromUDB2Actor(
244
        $actorId,
245
        $cdbXml,
246
        $cdbXmlNamespaceUri
247
    ) {
248
        $place = new static();
249
        $place->apply(
250
            new PlaceImportedFromUDB2(
251
                $actorId,
252
                $cdbXml,
253
                $cdbXmlNamespaceUri
254
            )
255
        );
256
257
        return $place;
258
    }
259
260
    /**
261
     * @param PlaceImportedFromUDB2 $placeImported
262
     */
263 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...
264
        PlaceImportedFromUDB2 $placeImported
265
    ) {
266
        $this->placeId = $placeImported->getActorId();
267
268
        $udb2Actor = ActorItemFactory::createActorFromCdbXml(
269
            $placeImported->getCdbXmlNamespaceUri(),
270
            $placeImported->getCdbXml()
271
        );
272
273
        $this->importWorkflowStatus($udb2Actor);
274
        $this->labels = LabelCollection::fromKeywords($udb2Actor->getKeywords(true));
275
    }
276
277
    /**
278
     * @param PlaceUpdatedFromUDB2 $placeUpdatedFromUDB2
279
     */
280 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...
281
        PlaceUpdatedFromUDB2 $placeUpdatedFromUDB2
282
    ) {
283
        $udb2Actor = ActorItemFactory::createActorFromCdbXml(
284
            $placeUpdatedFromUDB2->getCdbXmlNamespaceUri(),
285
            $placeUpdatedFromUDB2->getCdbXml()
286
        );
287
288
        $this->importWorkflowStatus($udb2Actor);
289
        $this->labels = LabelCollection::fromKeywords($udb2Actor->getKeywords(true));
290
    }
291
292
    /**
293
     * @inheritdoc
294
     */
295
    public function updateWithCdbXml($cdbXml, $cdbXmlNamespaceUri)
296
    {
297
        ActorItemFactory::createActorFromCdbXml($cdbXmlNamespaceUri, $cdbXml);
298
299
        $this->apply(
300
            new PlaceUpdatedFromUDB2(
301
                $this->placeId,
302
                $cdbXml,
303
                $cdbXmlNamespaceUri
304
            )
305
        );
306
    }
307
308
    /**
309
     * @param Label $label
310
     * @return LabelAdded
311
     */
312
    protected function createLabelAddedEvent(Label $label)
313
    {
314
        return new LabelAdded($this->placeId, $label);
315
    }
316
317
    /**
318
     * @param Label $label
319
     * @return LabelRemoved
320
     */
321
    protected function createLabelRemovedEvent(Label $label)
322
    {
323
        return new LabelRemoved($this->placeId, $label);
324
    }
325
326
    protected function createImageAddedEvent(Image $image)
327
    {
328
        return new ImageAdded($this->placeId, $image);
329
    }
330
331
    protected function createImageRemovedEvent(Image $image)
332
    {
333
        return new ImageRemoved($this->placeId, $image);
334
    }
335
336
    protected function createImageUpdatedEvent(
337
        AbstractUpdateImage $updateImageCommand
338
    ) {
339
        return new ImageUpdated(
340
            $this->placeId,
341
            $updateImageCommand->getMediaObjectId(),
342
            $updateImageCommand->getDescription(),
343
            $updateImageCommand->getCopyrightHolder()
344
        );
345
    }
346
347
    protected function createMainImageSelectedEvent(Image $image)
348
    {
349
        return new MainImageSelected($this->placeId, $image);
350
    }
351
352
    /**
353
     * @param Language $language
354
     * @param StringLiteral $title
355
     * @return TitleTranslated
356
     */
357
    protected function createTitleTranslatedEvent(Language $language, StringLiteral $title)
358
    {
359
        return new TitleTranslated($this->placeId, $language, $title);
360
    }
361
362
    /**
363
     * @param Title $title
364
     * @return TitleUpdated
365
     */
366
    protected function createTitleUpdatedEvent(Title $title)
367
    {
368
        return new TitleUpdated($this->placeId, $title);
369
    }
370
371
    /**
372
     * @param Language $language
373
     * @param StringLiteral $description
374
     * @return DescriptionTranslated
375
     */
376
    protected function createDescriptionTranslatedEvent(Language $language, StringLiteral $description)
377
    {
378
        return new DescriptionTranslated($this->placeId, $language, $description);
379
    }
380
381
    /**
382
     * @param string $description
383
     * @return DescriptionUpdated
384
     */
385
    protected function createDescriptionUpdatedEvent($description)
386
    {
387
        return new DescriptionUpdated($this->placeId, $description);
388
    }
389
390
    /**
391
     * @inheritdoc
392
     */
393
    protected function createCalendarUpdatedEvent(Calendar $calendar)
394
    {
395
        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...
396
    }
397
    
398
    /**
399
     * @param string $typicalAgeRange
400
     * @return TypicalAgeRangeUpdated
401
     */
402
    protected function createTypicalAgeRangeUpdatedEvent($typicalAgeRange)
403
    {
404
        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...
405
    }
406
407
    /**
408
     * @return TypicalAgeRangeDeleted
409
     */
410
    protected function createTypicalAgeRangeDeletedEvent()
411
    {
412
        return new TypicalAgeRangeDeleted($this->placeId);
413
    }
414
415
    /**
416
     * @param string $organizerId
417
     * @return OrganizerUpdated
418
     */
419
    protected function createOrganizerUpdatedEvent($organizerId)
420
    {
421
        return new OrganizerUpdated($this->placeId, $organizerId);
422
    }
423
424
    /**
425
     * @param string $organizerId
426
     * @return OrganizerDeleted
427
     */
428
    protected function createOrganizerDeletedEvent($organizerId)
429
    {
430
        return new OrganizerDeleted($this->placeId, $organizerId);
431
    }
432
433
    /**
434
     * @param ContactPoint $contactPoint
435
     * @return ContactPointUpdated
436
     */
437
    protected function createContactPointUpdatedEvent(ContactPoint $contactPoint)
438
    {
439
        return new ContactPointUpdated($this->placeId, $contactPoint);
440
    }
441
442
    /**
443
     * @inheritdoc
444
     */
445
    protected function createGeoCoordinatesUpdatedEvent(Coordinates $coordinates)
446
    {
447
        return new GeoCoordinatesUpdated($this->placeId, $coordinates);
448
    }
449
450
    /**
451
     * @param BookingInfo $bookingInfo
452
     * @return BookingInfoUpdated
453
     */
454
    protected function createBookingInfoUpdatedEvent(BookingInfo $bookingInfo)
455
    {
456
        return new BookingInfoUpdated($this->placeId, $bookingInfo);
457
    }
458
459
    /**
460
     * @param PriceInfo $priceInfo
461
     * @return PriceInfoUpdated
462
     */
463
    protected function createPriceInfoUpdatedEvent(PriceInfo $priceInfo)
464
    {
465
        return new PriceInfoUpdated($this->placeId, $priceInfo);
466
    }
467
468
    /**
469
     * @return PlaceDeleted
470
     */
471
    protected function createOfferDeletedEvent()
472
    {
473
        return new PlaceDeleted($this->placeId);
474
    }
475
476
    /**
477
     * @inheritDoc
478
     */
479
    protected function createPublishedEvent(\DateTimeInterface $publicationDate)
480
    {
481
        return new Published($this->placeId, $publicationDate);
482
    }
483
484
    /**
485
     * @inheritDoc
486
     */
487
    protected function createApprovedEvent()
488
    {
489
        return new Approved($this->placeId);
490
    }
491
492
    /**
493
     * @inheritDoc
494
     */
495
    protected function createRejectedEvent(StringLiteral $reason)
496
    {
497
        return new Rejected($this->placeId, $reason);
498
    }
499
500
    /**
501
     * @inheritDoc
502
     */
503
    protected function createFlaggedAsDuplicate()
504
    {
505
        return new FlaggedAsDuplicate($this->placeId);
506
    }
507
508
    /**
509
     * @inheritDoc
510
     */
511
    protected function createFlaggedAsInappropriate()
512
    {
513
        return new FlaggedAsInappropriate($this->placeId);
514
    }
515
516
    /**
517
     * @inheritDoc
518
     * @return ImagesImportedFromUDB2
519
     */
520
    protected function createImagesImportedFromUDB2(ImageCollection $images)
521
    {
522
        return new ImagesImportedFromUDB2($this->placeId, $images);
523
    }
524
525
    /**
526
     * @inheritDoc
527
     * @return ImagesUpdatedFromUDB2
528
     */
529
    protected function createImagesUpdatedFromUDB2(ImageCollection $images)
530
    {
531
        return new ImagesUpdatedFromUDB2($this->placeId, $images);
532
    }
533
534
    protected function createTypeUpdatedEvent(EventType $type)
535
    {
536
        return new TypeUpdated($this->placeId, $type);
537
    }
538
539
    protected function createThemeUpdatedEvent(Theme $theme)
540
    {
541
        return new ThemeUpdated($this->placeId, $theme);
542
    }
543
544
    /**
545
     * @inheritdoc
546
     */
547
    protected function createFacilitiesUpdatedEvent(array $facilities)
548
    {
549
        return new FacilitiesUpdated($this->placeId, $facilities);
550
    }
551
}
552