Completed
Pull Request — master (#352)
by
unknown
04:54
created

OfferCommandHandler::handleImportImages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
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\Media\MediaManager;
11
use CultuurNet\UDB3\Media\MediaManagerInterface;
12
use CultuurNet\UDB3\Offer\Commands\AbstractAddLabel;
13
use CultuurNet\UDB3\Offer\Commands\AbstractDeleteCurrentOrganizer;
14
use CultuurNet\UDB3\Offer\Commands\AbstractImportLabels;
15
use CultuurNet\UDB3\Offer\Commands\AbstractLabelCommand;
16
use CultuurNet\UDB3\Offer\Commands\AbstractRemoveLabel;
17
use CultuurNet\UDB3\Offer\Commands\AbstractDeleteOffer;
18
use CultuurNet\UDB3\Offer\Commands\AbstractDeleteOrganizer;
19
use CultuurNet\UDB3\Offer\Commands\AbstractDeleteTypicalAgeRange;
20
use CultuurNet\UDB3\Offer\Commands\AbstractUpdateBookingInfo;
21
use CultuurNet\UDB3\Offer\Commands\AbstractUpdateCalendar;
22
use CultuurNet\UDB3\Offer\Commands\AbstractUpdateContactPoint;
23
use CultuurNet\UDB3\Offer\Commands\AbstractUpdateDescription;
24
use CultuurNet\UDB3\Offer\Commands\AbstractUpdateFacilities;
25
use CultuurNet\UDB3\Offer\Commands\AbstractUpdateOrganizer;
26
use CultuurNet\UDB3\Offer\Commands\AbstractUpdatePriceInfo;
27
use CultuurNet\UDB3\Offer\Commands\AbstractUpdateTheme;
28
use CultuurNet\UDB3\Offer\Commands\AbstractUpdateType;
29
use CultuurNet\UDB3\Offer\Commands\AbstractUpdateTypicalAgeRange;
30
use CultuurNet\UDB3\Offer\Commands\Image\AbstractAddImage;
31
use CultuurNet\UDB3\Offer\Commands\Image\AbstractImportImages;
32
use CultuurNet\UDB3\Offer\Commands\Image\AbstractRemoveImage;
33
use CultuurNet\UDB3\Offer\Commands\Image\AbstractSelectMainImage;
34
use CultuurNet\UDB3\Offer\Commands\Image\AbstractUpdateImage;
35
use CultuurNet\UDB3\Offer\Commands\AbstractUpdateTitle;
36
use CultuurNet\UDB3\Offer\Commands\Moderation\AbstractApprove;
37
use CultuurNet\UDB3\Offer\Commands\Moderation\AbstractFlagAsDuplicate;
38
use CultuurNet\UDB3\Offer\Commands\Moderation\AbstractFlagAsInappropriate;
39
use CultuurNet\UDB3\Offer\Commands\Moderation\AbstractPublish;
40
use CultuurNet\UDB3\Offer\Commands\Moderation\AbstractReject;
41
use CultuurNet\UDB3\Organizer\Organizer;
42
use ValueObjects\StringLiteral\StringLiteral;
43
44
abstract class OfferCommandHandler extends Udb3CommandHandler
45
{
46
    /**
47
     * @var RepositoryInterface
48
     */
49
    protected $offerRepository;
50
51
    /**
52
     * @var RepositoryInterface
53
     */
54
    protected $organizerRepository;
55
56
    /**
57
     * @var RepositoryInterface
58
     */
59
    protected $labelRepository;
60
61
    /**
62
     * @var MediaManagerInterface|MediaManager
63
     */
64
    protected $mediaManager;
65
66
    /**
67
     * @param RepositoryInterface $offerRepository
68
     * @param RepositoryInterface $organizerRepository
69
     * @param ReadRepositoryInterface $labelRepository
70
     * @param MediaManagerInterface $mediaManager
71
     */
72
    public function __construct(
73
        RepositoryInterface $offerRepository,
74
        RepositoryInterface $organizerRepository,
75
        ReadRepositoryInterface $labelRepository,
76
        MediaManagerInterface $mediaManager
77
    ) {
78
        $this->offerRepository = $offerRepository;
79
        $this->organizerRepository = $organizerRepository;
80
        $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...
81
        $this->mediaManager = $mediaManager;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 View Code Duplication
    public function handle($command)
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...
88
    {
89
        $commandName = get_class($command);
90
        $commandHandlers = $this->getCommandHandlers();
91
92
        if (isset($commandHandlers[$commandName])) {
93
            $handler = $commandHandlers[$commandName];
94
            call_user_func(array($this, $handler), $command);
95
        } else {
96
            parent::handle($command);
97
        }
98
    }
99
100
    /**
101
     * @return string[]
102
     *   An associative array of commands and their handler methods.
103
     */
104 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...
105
    {
106
        $commands = [];
107
108
        foreach (get_class_methods($this) as $method) {
109
            $matches = [];
110
            if (preg_match('/^handle(.+)$/', $method, $matches)) {
111
                $command = $matches[1];
112
                $classNameMethod = 'get' . $command . 'ClassName';
113
114
                if (method_exists($this, $classNameMethod)) {
115
                    $commandFullClassName = call_user_func(array($this, $classNameMethod));
116
                    $commands[$commandFullClassName] = $method;
117
                }
118
            }
119
        }
120
121
        return $commands;
122
    }
123
124
    /**
125
     * @return string
126
     */
127
    abstract protected function getAddLabelClassName();
128
129
    /**
130
     * @return string
131
     */
132
    abstract protected function getRemoveLabelClassName();
133
134
    /**
135
     * @return string
136
     */
137
    abstract protected function getImportLabelsClassName();
138
139
    /**
140
     * @return string
141
     */
142
    abstract protected function getUpdateTitleClassName();
143
144
    /**
145
     * @return string
146
     */
147
    abstract protected function getAddImageClassName();
148
149
    /**
150
     * @return string
151
     */
152
    abstract protected function getUpdateImageClassName();
153
154
    /**
155
     * @return string
156
     */
157
    abstract protected function getRemoveImageClassName();
158
159
    /**
160
     * @return string
161
     */
162
    abstract protected function getSelectMainImageClassName();
163
164
    /**
165
     * @return string
166
     */
167
    abstract protected function getImportImagesClassName();
168
169
    /**
170
     * @return string
171
     */
172
    abstract protected function getUpdateDescriptionClassName();
173
174
    /**
175
     * @return string
176
     */
177
    abstract protected function getUpdateCalendarClassName();
178
179
    /**
180
     * @return string
181
     */
182
    abstract protected function getUpdateTypicalAgeRangeClassName();
183
184
    /**
185
     * @return string
186
     */
187
    abstract protected function getDeleteTypicalAgeRangeClassName();
188
189
    /**
190
     * @return string
191
     */
192
    abstract protected function getUpdateOrganizerClassName();
193
194
    /**
195
     * @return string
196
     */
197
    abstract protected function getDeleteOrganizerClassName();
198
199
    /**
200
     * @return string
201
     */
202
    abstract protected function getDeleteCurrentOrganizerClassName();
203
204
    /**
205
     * @return string
206
     */
207
    abstract protected function getUpdateContactPointClassName();
208
209
    /**
210
     * @return string
211
     */
212
    abstract protected function getUpdateBookingInfoClassName();
213
214
    /**
215
     * @return string
216
     */
217
    abstract protected function getUpdatePriceInfoClassName();
218
219
    /**
220
     * @return string
221
     */
222
    abstract protected function getDeleteOfferClassName();
223
224
    /**
225
     * @return string
226
     */
227
    abstract protected function getPublishClassName();
228
229
    /**
230
     * @return string
231
     */
232
    abstract protected function getApproveClassName();
233
234
    /**
235
     * @return string
236
     */
237
    abstract protected function getRejectClassName();
238
239
    /**
240
     * @return string
241
     */
242
    abstract protected function getFlagAsDuplicateClassName();
243
244
    /**
245
     * @return string
246
     */
247
    abstract protected function getFlagAsInappropriateClassName();
248
249
    /**
250
     * @return string
251
     */
252
    abstract protected function getUpdateTypeClassName();
253
254
    /**
255
     * @return string
256
     */
257
    abstract protected function getUpdateThemeClassName();
258
259
    /**
260
     * @return string
261
     */
262
    abstract protected function getUpdateFacilitiesClassName();
263
264
    /**
265
     * @param AbstractUpdateType $updateType
266
     */
267
    public function handleUpdateType(AbstractUpdateType $updateType)
268
    {
269
        $offer = $this->load($updateType->getItemId());
270
271
        $offer->updateType($updateType->getType());
272
273
        $this->offerRepository->save($offer);
274
    }
275
276
    /**
277
     * @param AbstractUpdateTheme $updateTheme
278
     */
279
    public function handleUpdateTheme(AbstractUpdateTheme $updateTheme)
280
    {
281
        $offer = $this->load($updateTheme->getItemId());
282
283
        $offer->updateTheme($updateTheme->getTheme());
284
285
        $this->offerRepository->save($offer);
286
    }
287
288
    /**
289
     * @param AbstractUpdateFacilities $updateFacilities
290
     */
291
    public function handleUpdateFacilities(AbstractUpdateFacilities $updateFacilities)
292
    {
293
        $offer = $this->load($updateFacilities->getItemId());
294
295
        $offer->updateFacilities($updateFacilities->getFacilities());
296
297
        $this->offerRepository->save($offer);
298
    }
299
300
    /**
301
     * @param AbstractAddLabel $addLabel
302
     */
303
    private function handleAddLabel(AbstractAddLabel $addLabel)
304
    {
305
        $offer = $this->load($addLabel->getItemId());
306
307
        $offer->addLabel($this->createLabel($addLabel));
308
309
        $this->offerRepository->save($offer);
310
    }
311
312
    /**
313
     * @param AbstractRemoveLabel $removeLabel
314
     */
315
    private function handleRemoveLabel(AbstractRemoveLabel $removeLabel)
316
    {
317
        $offer = $this->load($removeLabel->getItemId());
318
319
        $offer->removeLabel($this->createLabel($removeLabel));
320
321
        $this->offerRepository->save($offer);
322
    }
323
324
    /**
325
     * @param AbstractImportLabels $importLabels
326
     */
327
    private function handleImportLabels(AbstractImportLabels $importLabels)
328
    {
329
        $offer = $this->load($importLabels->getItemId());
330
331
        $offer->importLabels($importLabels->getLabels());
332
333
        $this->offerRepository->save($offer);
334
    }
335
336
    /**
337
     * @param AbstractLabelCommand $labelCommand
338
     * @return Label
339
     */
340 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...
341
    {
342
        $labelName = new StringLiteral((string) $labelCommand->getLabel());
343
        $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...
344
345
        return new Label(
346
            $labelName->toNative(),
347
            $label->getVisibility() === Visibility::VISIBLE()
348
        );
349
    }
350
351
    /**
352
     * @param AbstractUpdateTitle $translateTitle
353
     */
354
    private function handleUpdateTitle(AbstractUpdateTitle $translateTitle)
355
    {
356
        $offer = $this->load($translateTitle->getItemId());
357
        $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...
358
        $this->offerRepository->save($offer);
359
    }
360
361
    /**
362
     * Handle an add image command.
363
     * @param AbstractAddImage $addImage
364
     */
365
    public function handleAddImage(AbstractAddImage $addImage)
366
    {
367
        $offer = $this->load($addImage->getItemId());
368
369
        $image = $this->mediaManager->getImage($addImage->getImageId());
0 ignored issues
show
Bug introduced by
The method getImage does only exist in CultuurNet\UDB3\Media\MediaManager, but not in CultuurNet\UDB3\Media\MediaManagerInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
370
        $offer->addImage($image);
371
372
        $this->offerRepository->save($offer);
373
    }
374
375
    /**
376
     * @param AbstractRemoveImage $removeImage
377
     */
378
    public function handleRemoveImage(AbstractRemoveImage $removeImage)
379
    {
380
        $offer = $this->load($removeImage->getItemId());
381
        $offer->removeImage($removeImage->getImage());
382
        $this->offerRepository->save($offer);
383
    }
384
385
    /**
386
     * @param AbstractUpdateImage $updateImage
387
     */
388
    public function handleUpdateImage(AbstractUpdateImage $updateImage)
389
    {
390
        $offer = $this->load($updateImage->getItemId());
391
        $offer->updateImage($updateImage);
392
        $this->offerRepository->save($offer);
393
    }
394
395
    /**
396
     * @param AbstractSelectMainImage $selectMainImage
397
     */
398
    public function handleSelectMainImage(AbstractSelectMainImage $selectMainImage)
399
    {
400
        $offer = $this->load($selectMainImage->getItemId());
401
        $offer->selectMainImage($selectMainImage->getImage());
402
        $this->offerRepository->save($offer);
403
    }
404
405
    /**
406
     * @param AbstractImportImages $importImages
407
     */
408
    public function handleImportImages(AbstractImportImages $importImages)
409
    {
410
        $offer = $this->load($importImages->getItemId());
411
        $offer->importImages($importImages->getImages());
412
        $this->offerRepository->save($offer);
413
    }
414
415
    /**
416
     * Handle the update of description on a place.
417
     * @param AbstractUpdateDescription $updateDescription
418
     */
419
    public function handleUpdateDescription(AbstractUpdateDescription $updateDescription)
420
    {
421
        $offer = $this->load($updateDescription->getItemId());
422
423
        $offer->updateDescription(
424
            $updateDescription->getDescription(),
425
            $updateDescription->getLanguage()
426
        );
427
428
        $this->offerRepository->save($offer);
429
430
    }
431
432
    /**
433
     * @param AbstractUpdateCalendar $updateCalendar
434
     */
435
    public function handleUpdateCalendar(AbstractUpdateCalendar $updateCalendar)
436
    {
437
        $offer = $this->load($updateCalendar->getItemId());
438
439
        $offer->updateCalendar($updateCalendar->getCalendar());
440
441
        $this->offerRepository->save($offer);
442
    }
443
444
    /**
445
     * Handle the update of typical age range on a place.
446
     * @param AbstractUpdateTypicalAgeRange $updateTypicalAgeRange
447
     */
448
    public function handleUpdateTypicalAgeRange(AbstractUpdateTypicalAgeRange $updateTypicalAgeRange)
449
    {
450
        $offer = $this->load($updateTypicalAgeRange->getItemId());
451
452
        $offer->updateTypicalAgeRange(
453
            $updateTypicalAgeRange->getTypicalAgeRange()
454
        );
455
456
        $this->offerRepository->save($offer);
457
458
    }
459
460
    /**
461
     * Handle the deletion of typical age range on a place.
462
     * @param AbstractDeleteTypicalAgeRange $deleteTypicalAgeRange
463
     */
464
    public function handleDeleteTypicalAgeRange(AbstractDeleteTypicalAgeRange $deleteTypicalAgeRange)
465
    {
466
        $offer = $this->load($deleteTypicalAgeRange->getItemId());
467
468
        $offer->deleteTypicalAgeRange();
469
470
        $this->offerRepository->save($offer);
471
472
    }
473
474
    /**
475
     * Handle an update command to update organizer of a place.
476
     * @param AbstractUpdateOrganizer $updateOrganizer
477
     */
478
    public function handleUpdateOrganizer(AbstractUpdateOrganizer $updateOrganizer)
479
    {
480
        $offer = $this->load($updateOrganizer->getItemId());
481
        $this->loadOrganizer($updateOrganizer->getOrganizerId());
482
483
        $offer->updateOrganizer(
484
            $updateOrganizer->getOrganizerId()
485
        );
486
487
        $this->offerRepository->save($offer);
488
    }
489
490
    /**
491
     * Handle an update command to delete the organizer.
492
     * @param AbstractDeleteOrganizer $deleteOrganizer
493
     */
494
    public function handleDeleteOrganizer(AbstractDeleteOrganizer $deleteOrganizer)
495
    {
496
        $offer = $this->load($deleteOrganizer->getItemId());
497
498
        $offer->deleteOrganizer(
499
            $deleteOrganizer->getOrganizerId()
500
        );
501
502
        $this->offerRepository->save($offer);
503
    }
504
505
    /**
506
     * @param AbstractDeleteCurrentOrganizer $deleteCurrentOrganizer
507
     */
508
    public function handleDeleteCurrentOrganizer(AbstractDeleteCurrentOrganizer $deleteCurrentOrganizer)
509
    {
510
        $offer = $this->load($deleteCurrentOrganizer->getItemId());
511
512
        $offer->deleteCurrentOrganizer();
513
514
        $this->offerRepository->save($offer);
515
    }
516
517
    /**
518
     * Handle an update command to updated the contact point.
519
     * @param AbstractUpdateContactPoint $updateContactPoint
520
     */
521
    public function handleUpdateContactPoint(AbstractUpdateContactPoint $updateContactPoint)
522
    {
523
        $offer = $this->load($updateContactPoint->getItemId());
524
525
        $offer->updateContactPoint(
526
            $updateContactPoint->getContactPoint()
527
        );
528
529
        $this->offerRepository->save($offer);
530
531
    }
532
533
    /**
534
     * Handle an update command to updated the booking info.
535
     * @param AbstractUpdateBookingInfo $updateBookingInfo
536
     */
537
    public function handleUpdateBookingInfo(AbstractUpdateBookingInfo $updateBookingInfo)
538
    {
539
        $offer = $this->load($updateBookingInfo->getItemId());
540
541
        $offer->updateBookingInfo(
542
            $updateBookingInfo->getBookingInfo()
543
        );
544
545
        $this->offerRepository->save($offer);
546
    }
547
548
    /**
549
     * @param AbstractUpdatePriceInfo $updatePriceInfo
550
     */
551
    private function handleUpdatePriceInfo(AbstractUpdatePriceInfo $updatePriceInfo)
552
    {
553
        $offer = $this->load($updatePriceInfo->getItemId());
554
555
        $offer->updatePriceInfo(
556
            $updatePriceInfo->getPriceInfo()
557
        );
558
559
        $this->offerRepository->save($offer);
560
    }
561
562
    /**
563
     * @param AbstractDeleteOffer $deleteOffer
564
     */
565
    private function handleDeleteOffer(AbstractDeleteOffer $deleteOffer)
566
    {
567
        $offer = $this->load($deleteOffer->getItemId());
568
        $offer->delete();
569
        $this->offerRepository->save($offer);
570
    }
571
572
    /**
573
     * @param AbstractPublish $publish
574
     */
575
    private function handlePublish(AbstractPublish $publish)
576
    {
577
        $offer = $this->load($publish->getItemId());
578
        $offer->publish($publish->getPublicationDate());
579
        $this->offerRepository->save($offer);
580
    }
581
582
    /**
583
     * @param AbstractApprove $approve
584
     */
585
    private function handleApprove(AbstractApprove $approve)
586
    {
587
        $offer = $this->load($approve->getItemId());
588
        $offer->approve();
589
        $this->offerRepository->save($offer);
590
    }
591
592
    /**
593
     * @param AbstractReject $reject
594
     */
595
    private function handleReject(AbstractReject $reject)
596
    {
597
        $offer = $this->load($reject->getItemId());
598
        $offer->reject($reject->getReason());
599
        $this->offerRepository->save($offer);
600
    }
601
602
    /**
603
     * @param AbstractFlagAsDuplicate $flagAsDuplicate
604
     */
605
    private function handleFlagAsDuplicate(AbstractFlagAsDuplicate $flagAsDuplicate)
606
    {
607
        $offer = $this->load($flagAsDuplicate->getItemId());
608
        $offer->flagAsDuplicate();
609
        $this->offerRepository->save($offer);
610
    }
611
612
    /**
613
     * @param AbstractFlagAsInappropriate $flagAsInappropriate
614
     */
615
    private function handleFlagAsInappropriate(AbstractFlagAsInappropriate $flagAsInappropriate)
616
    {
617
        $offer = $this->load($flagAsInappropriate->getItemId());
618
        $offer->flagAsInappropriate();
619
        $this->offerRepository->save($offer);
620
    }
621
622
    /**
623
     * Makes it easier to type-hint to Offer.
624
     *
625
     * @param string $id
626
     * @return Offer
627
     */
628
    private function load($id)
629
    {
630
        return $this->offerRepository->load($id);
631
    }
632
633
    /**
634
     * Makes it easier to type-hint to Organizer.
635
     *
636
     * @param string $id
637
     * @return Organizer
638
     */
639
    private function loadOrganizer($id)
640
    {
641
        return $this->organizerRepository->load($id);
642
643
    }
644
}
645