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

OfferCommandHandler::handleUpdateType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\Offer;
4
5
use Broadway\Repository\RepositoryInterface;
6
use CultuurNet\UDB3\CommandHandling\Udb3CommandHandler;
7
use CultuurNet\UDB3\Label;
8
use CultuurNet\UDB3\Label\ReadModels\JSON\Repository\ReadRepositoryInterface;
9
use CultuurNet\UDB3\Label\ValueObjects\Visibility;
10
use CultuurNet\UDB3\Offer\Commands\AbstractAddLabel;
11
use CultuurNet\UDB3\Offer\Commands\AbstractLabelCommand;
12
use CultuurNet\UDB3\Offer\Commands\AbstractRemoveLabel;
13
use CultuurNet\UDB3\Offer\Commands\AbstractDeleteOffer;
14
use CultuurNet\UDB3\Offer\Commands\AbstractDeleteOrganizer;
15
use CultuurNet\UDB3\Offer\Commands\AbstractDeleteTypicalAgeRange;
16
use CultuurNet\UDB3\Offer\Commands\AbstractUpdateBookingInfo;
17
use CultuurNet\UDB3\Offer\Commands\AbstractUpdateCalendar;
18
use CultuurNet\UDB3\Offer\Commands\AbstractUpdateContactPoint;
19
use CultuurNet\UDB3\Offer\Commands\AbstractUpdateDescription;
20
use CultuurNet\UDB3\Offer\Commands\AbstractUpdateFacilities;
21
use CultuurNet\UDB3\Offer\Commands\AbstractUpdateOrganizer;
22
use CultuurNet\UDB3\Offer\Commands\AbstractUpdatePriceInfo;
23
use CultuurNet\UDB3\Offer\Commands\AbstractUpdateTheme;
24
use CultuurNet\UDB3\Offer\Commands\AbstractUpdateType;
25
use CultuurNet\UDB3\Offer\Commands\AbstractUpdateTypicalAgeRange;
26
use CultuurNet\UDB3\Offer\Commands\Image\AbstractAddImage;
27
use CultuurNet\UDB3\Offer\Commands\Image\AbstractRemoveImage;
28
use CultuurNet\UDB3\Offer\Commands\Image\AbstractSelectMainImage;
29
use CultuurNet\UDB3\Offer\Commands\Image\AbstractUpdateImage;
30
use CultuurNet\UDB3\Offer\Commands\AbstractUpdateTitle;
31
use CultuurNet\UDB3\Offer\Commands\Moderation\AbstractApprove;
32
use CultuurNet\UDB3\Offer\Commands\Moderation\AbstractFlagAsDuplicate;
33
use CultuurNet\UDB3\Offer\Commands\Moderation\AbstractFlagAsInappropriate;
34
use CultuurNet\UDB3\Offer\Commands\Moderation\AbstractPublish;
35
use CultuurNet\UDB3\Offer\Commands\Moderation\AbstractReject;
36
use CultuurNet\UDB3\Organizer\Organizer;
37
use ValueObjects\StringLiteral\StringLiteral;
38
39
abstract class OfferCommandHandler extends Udb3CommandHandler
40
{
41
    /**
42
     * @var RepositoryInterface
43
     */
44
    protected $offerRepository;
45
46
    /**
47
     * @var RepositoryInterface
48
     */
49
    protected $organizerRepository;
50
51
    /**
52
     * @var RepositoryInterface
53
     */
54
    protected $labelRepository;
55
56
    /**
57
     * @param RepositoryInterface $offerRepository
58
     * @param RepositoryInterface $organizerRepository
59
     * @param ReadRepositoryInterface $labelRepository
60
     */
61
    public function __construct(
62
        RepositoryInterface $offerRepository,
63
        RepositoryInterface $organizerRepository,
64
        ReadRepositoryInterface $labelRepository
65
    ) {
66
        $this->offerRepository = $offerRepository;
67
        $this->organizerRepository = $organizerRepository;
68
        $this->labelRepository = $labelRepository;
0 ignored issues
show
Documentation Bug introduced by
It seems like $labelRepository of type object<CultuurNet\UDB3\L...eadRepositoryInterface> is incompatible with the declared type object<Broadway\Repository\RepositoryInterface> of property $labelRepository.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function handle($command)
75
    {
76
        $commandName = get_class($command);
77
        $commandHandlers = $this->getCommandHandlers();
78
79
        if (isset($commandHandlers[$commandName])) {
80
            $handler = $commandHandlers[$commandName];
81
            call_user_func(array($this, $handler), $command);
82
        } else {
83
            parent::handle($command);
84
        }
85
    }
86
87
    /**
88
     * @return string[]
89
     *   An associative array of commands and their handler methods.
90
     */
91 View Code Duplication
    private function getCommandHandlers()
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...
92
    {
93
        $commands = [];
94
95
        foreach (get_class_methods($this) as $method) {
96
            $matches = [];
97
            if (preg_match('/^handle(.+)$/', $method, $matches)) {
98
                $command = $matches[1];
99
                $classNameMethod = 'get' . $command . 'ClassName';
100
101
                if (method_exists($this, $classNameMethod)) {
102
                    $commandFullClassName = call_user_func(array($this, $classNameMethod));
103
                    $commands[$commandFullClassName] = $method;
104
                }
105
            }
106
        }
107
108
        return $commands;
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    abstract protected function getAddLabelClassName();
115
116
    /**
117
     * @return string
118
     */
119
    abstract protected function getRemoveLabelClassName();
120
121
    /**
122
     * @return string
123
     */
124
    abstract protected function getUpdateTitleClassName();
125
126
    /**
127
     * @return string
128
     */
129
    abstract protected function getAddImageClassName();
130
131
    /**
132
     * @return string
133
     */
134
    abstract protected function getUpdateImageClassName();
135
136
    /**
137
     * @return string
138
     */
139
    abstract protected function getRemoveImageClassName();
140
141
    /**
142
     * @return string
143
     */
144
    abstract protected function getSelectMainImageClassName();
145
146
    /**
147
     * @return string
148
     */
149
    abstract protected function getUpdateDescriptionClassName();
150
151
    /**
152
     * @return string
153
     */
154
    abstract protected function getUpdateCalendarClassName();
155
156
    /**
157
     * @return string
158
     */
159
    abstract protected function getUpdateTypicalAgeRangeClassName();
160
161
    /**
162
     * @return string
163
     */
164
    abstract protected function getDeleteTypicalAgeRangeClassName();
165
166
    /**
167
     * @return string
168
     */
169
    abstract protected function getUpdateOrganizerClassName();
170
171
    /**
172
     * @return string
173
     */
174
    abstract protected function getDeleteOrganizerClassName();
175
176
    /**
177
     * @return string
178
     */
179
    abstract protected function getUpdateContactPointClassName();
180
181
    /**
182
     * @return string
183
     */
184
    abstract protected function getUpdateBookingInfoClassName();
185
186
    /**
187
     * @return string
188
     */
189
    abstract protected function getUpdatePriceInfoClassName();
190
191
    /**
192
     * @return string
193
     */
194
    abstract protected function getDeleteOfferClassName();
195
196
    /**
197
     * @return string
198
     */
199
    abstract protected function getPublishClassName();
200
201
    /**
202
     * @return string
203
     */
204
    abstract protected function getApproveClassName();
205
206
    /**
207
     * @return string
208
     */
209
    abstract protected function getRejectClassName();
210
211
    /**
212
     * @return string
213
     */
214
    abstract protected function getFlagAsDuplicateClassName();
215
216
    /**
217
     * @return string
218
     */
219
    abstract protected function getFlagAsInappropriateClassName();
220
221
    /**
222
     * @return string
223
     */
224
    abstract protected function getUpdateTypeClassName();
225
226
    /**
227
     * @return string
228
     */
229
    abstract protected function getUpdateThemeClassName();
230
231
    /**
232
     * @return string
233
     */
234
    abstract protected function getUpdateFacilitiesClassName();
235
236
    /**
237
     * @param AbstractUpdateType $updateType
238
     */
239
    public function handleUpdateType(AbstractUpdateType $updateType)
240
    {
241
        $offer = $this->load($updateType->getItemId());
242
243
        $offer->updateType($updateType->getType());
244
245
        $this->offerRepository->save($offer);
246
    }
247
248
    /**
249
     * @param AbstractUpdateTheme $updateTheme
250
     */
251
    public function handleUpdateTheme(AbstractUpdateTheme $updateTheme)
252
    {
253
        $offer = $this->load($updateTheme->getItemId());
254
255
        $offer->updateTheme($updateTheme->getTheme());
256
257
        $this->offerRepository->save($offer);
258
    }
259
260
    /**
261
     * @param AbstractUpdateFacilities $updateFacilities
262
     */
263
    public function handleUpdateFacilities(AbstractUpdateFacilities $updateFacilities)
264
    {
265
        $offer = $this->load($updateFacilities->getItemId());
266
267
        $offer->updateFacilities($updateFacilities->getFacilities());
268
269
        $this->offerRepository->save($offer);
270
    }
271
272
    /**
273
     * @param AbstractAddLabel $addLabel
274
     */
275
    private function handleAddLabel(AbstractAddLabel $addLabel)
276
    {
277
        $offer = $this->load($addLabel->getItemId());
278
279
        $offer->addLabel($this->createLabel($addLabel));
280
281
        $this->offerRepository->save($offer);
282
    }
283
284
    /**
285
     * @param AbstractRemoveLabel $removeLabel
286
     */
287
    private function handleRemoveLabel(AbstractRemoveLabel $removeLabel)
288
    {
289
        $offer = $this->load($removeLabel->getItemId());
290
291
        $offer->removeLabel($this->createLabel($removeLabel));
292
293
        $this->offerRepository->save($offer);
294
    }
295
296
    /**
297
     * @param AbstractLabelCommand $labelCommand
298
     * @return Label
299
     */
300 View Code Duplication
    private function createLabel(AbstractLabelCommand $labelCommand)
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...
301
    {
302
        $labelName = new StringLiteral((string) $labelCommand->getLabel());
303
        $label = $this->labelRepository->getByName($labelName);
0 ignored issues
show
Bug introduced by
The method getByName() does not seem to exist on object<Broadway\Repository\RepositoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
304
305
        return new Label(
306
            $labelName->toNative(),
307
            $label->getVisibility() === Visibility::VISIBLE()
308
        );
309
    }
310
311
    /**
312
     * @param AbstractUpdateTitle $translateTitle
313
     */
314
    private function handleUpdateTitle(AbstractUpdateTitle $translateTitle)
315
    {
316
        $offer = $this->load($translateTitle->getItemId());
317
        $offer->updateTitle($translateTitle->getLanguage(), $translateTitle->getTitle());
0 ignored issues
show
Documentation introduced by
$translateTitle->getTitle() is of type string, but the function expects a object<CultuurNet\UDB3\Title>.

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...
318
        $this->offerRepository->save($offer);
319
    }
320
321
    /**
322
     * Handle an add image command.
323
     * @param AbstractAddImage $addImage
324
     */
325
    public function handleAddImage(AbstractAddImage $addImage)
326
    {
327
        $offer = $this->load($addImage->getItemId());
328
        $offer->addImage($addImage->getImage());
329
        $this->offerRepository->save($offer);
330
    }
331
332
    /**
333
     * @param AbstractRemoveImage $removeImage
334
     */
335
    public function handleRemoveImage(AbstractRemoveImage $removeImage)
336
    {
337
        $offer = $this->load($removeImage->getItemId());
338
        $offer->removeImage($removeImage->getImage());
339
        $this->offerRepository->save($offer);
340
    }
341
342
    /**
343
     * @param AbstractUpdateImage $updateImage
344
     */
345
    public function handleUpdateImage(AbstractUpdateImage $updateImage)
346
    {
347
        $offer = $this->load($updateImage->getItemId());
348
        $offer->updateImage($updateImage);
349
        $this->offerRepository->save($offer);
350
    }
351
352
    /**
353
     * @param AbstractSelectMainImage $selectMainImage
354
     */
355
    public function handleSelectMainImage(AbstractSelectMainImage $selectMainImage)
356
    {
357
        $offer = $this->load($selectMainImage->getItemId());
358
        $offer->selectMainImage($selectMainImage->getImage());
359
        $this->offerRepository->save($offer);
360
    }
361
362
    /**
363
     * Handle the update of description on a place.
364
     * @param AbstractUpdateDescription $updateDescription
365
     */
366
    public function handleUpdateDescription(AbstractUpdateDescription $updateDescription)
367
    {
368
        $offer = $this->load($updateDescription->getItemId());
369
370
        $offer->updateDescription(
371
            $updateDescription->getDescription(),
372
            $updateDescription->getLanguage()
373
        );
374
375
        $this->offerRepository->save($offer);
376
377
    }
378
379
    /**
380
     * @param AbstractUpdateCalendar $updateCalendar
381
     */
382
    public function handleUpdateCalendar(AbstractUpdateCalendar $updateCalendar)
383
    {
384
        $offer = $this->load($updateCalendar->getItemId());
385
386
        $offer->updateCalendar($updateCalendar->getCalendar());
387
388
        $this->offerRepository->save($offer);
389
    }
390
391
    /**
392
     * Handle the update of typical age range on a place.
393
     * @param AbstractUpdateTypicalAgeRange $updateTypicalAgeRange
394
     */
395
    public function handleUpdateTypicalAgeRange(AbstractUpdateTypicalAgeRange $updateTypicalAgeRange)
396
    {
397
        $offer = $this->load($updateTypicalAgeRange->getItemId());
398
399
        $offer->updateTypicalAgeRange(
400
            $updateTypicalAgeRange->getTypicalAgeRange()
401
        );
402
403
        $this->offerRepository->save($offer);
404
405
    }
406
407
    /**
408
     * Handle the deletion of typical age range on a place.
409
     * @param AbstractDeleteTypicalAgeRange $deleteTypicalAgeRange
410
     */
411
    public function handleDeleteTypicalAgeRange(AbstractDeleteTypicalAgeRange $deleteTypicalAgeRange)
412
    {
413
        $offer = $this->load($deleteTypicalAgeRange->getItemId());
414
415
        $offer->deleteTypicalAgeRange();
416
417
        $this->offerRepository->save($offer);
418
419
    }
420
421
    /**
422
     * Handle an update command to update organizer of a place.
423
     * @param AbstractUpdateOrganizer $updateOrganizer
424
     */
425
    public function handleUpdateOrganizer(AbstractUpdateOrganizer $updateOrganizer)
426
    {
427
        $offer = $this->load($updateOrganizer->getItemId());
428
        $this->loadOrganizer($updateOrganizer->getOrganizerId());
429
430
        $offer->updateOrganizer(
431
            $updateOrganizer->getOrganizerId()
432
        );
433
434
        $this->offerRepository->save($offer);
435
    }
436
437
    /**
438
     * Handle an update command to delete the organizer.
439
     * @param AbstractDeleteOrganizer $deleteOrganizer
440
     */
441
    public function handleDeleteOrganizer(AbstractDeleteOrganizer $deleteOrganizer)
442
    {
443
        $offer = $this->load($deleteOrganizer->getItemId());
444
445
        $offer->deleteOrganizer(
446
            $deleteOrganizer->getOrganizerId()
447
        );
448
449
        $this->offerRepository->save($offer);
450
    }
451
452
    /**
453
     * Handle an update command to updated the contact point.
454
     * @param AbstractUpdateContactPoint $updateContactPoint
455
     */
456
    public function handleUpdateContactPoint(AbstractUpdateContactPoint $updateContactPoint)
457
    {
458
        $offer = $this->load($updateContactPoint->getItemId());
459
460
        $offer->updateContactPoint(
461
            $updateContactPoint->getContactPoint()
462
        );
463
464
        $this->offerRepository->save($offer);
465
466
    }
467
468
    /**
469
     * Handle an update command to updated the booking info.
470
     * @param AbstractUpdateBookingInfo $updateBookingInfo
471
     */
472
    public function handleUpdateBookingInfo(AbstractUpdateBookingInfo $updateBookingInfo)
473
    {
474
        $offer = $this->load($updateBookingInfo->getItemId());
475
476
        $offer->updateBookingInfo(
477
            $updateBookingInfo->getBookingInfo()
478
        );
479
480
        $this->offerRepository->save($offer);
481
    }
482
483
    /**
484
     * @param AbstractUpdatePriceInfo $updatePriceInfo
485
     */
486
    private function handleUpdatePriceInfo(AbstractUpdatePriceInfo $updatePriceInfo)
487
    {
488
        $offer = $this->load($updatePriceInfo->getItemId());
489
490
        $offer->updatePriceInfo(
491
            $updatePriceInfo->getPriceInfo()
492
        );
493
494
        $this->offerRepository->save($offer);
495
    }
496
497
    /**
498
     * @param AbstractDeleteOffer $deleteOffer
499
     */
500
    private function handleDeleteOffer(AbstractDeleteOffer $deleteOffer)
501
    {
502
        $offer = $this->load($deleteOffer->getItemId());
503
        $offer->delete();
504
        $this->offerRepository->save($offer);
505
    }
506
507
    /**
508
     * @param AbstractPublish $publish
509
     */
510
    private function handlePublish(AbstractPublish $publish)
511
    {
512
        $offer = $this->load($publish->getItemId());
513
        $offer->publish($publish->getPublicationDate());
514
        $this->offerRepository->save($offer);
515
    }
516
517
    /**
518
     * @param AbstractApprove $approve
519
     */
520
    private function handleApprove(AbstractApprove $approve)
521
    {
522
        $offer = $this->load($approve->getItemId());
523
        $offer->approve();
524
        $this->offerRepository->save($offer);
525
    }
526
527
    /**
528
     * @param AbstractReject $reject
529
     */
530
    private function handleReject(AbstractReject $reject)
531
    {
532
        $offer = $this->load($reject->getItemId());
533
        $offer->reject($reject->getReason());
534
        $this->offerRepository->save($offer);
535
    }
536
537
    /**
538
     * @param AbstractFlagAsDuplicate $flagAsDuplicate
539
     */
540
    private function handleFlagAsDuplicate(AbstractFlagAsDuplicate $flagAsDuplicate)
541
    {
542
        $offer = $this->load($flagAsDuplicate->getItemId());
543
        $offer->flagAsDuplicate();
544
        $this->offerRepository->save($offer);
545
    }
546
547
    /**
548
     * @param AbstractFlagAsInappropriate $flagAsInappropriate
549
     */
550
    private function handleFlagAsInappropriate(AbstractFlagAsInappropriate $flagAsInappropriate)
551
    {
552
        $offer = $this->load($flagAsInappropriate->getItemId());
553
        $offer->flagAsInappropriate();
554
        $this->offerRepository->save($offer);
555
    }
556
557
    /**
558
     * Makes it easier to type-hint to Offer.
559
     *
560
     * @param string $id
561
     * @return Offer
562
     */
563
    private function load($id)
564
    {
565
        return $this->offerRepository->load($id);
566
    }
567
568
    /**
569
     * Makes it easier to type-hint to Organizer.
570
     *
571
     * @param string $id
572
     * @return Organizer
573
     */
574
    private function loadOrganizer($id)
575
    {
576
        return $this->organizerRepository->load($id);
577
578
    }
579
}
580