Completed
Push — master ( 5ace45...cd9bd4 )
by
unknown
14s queued 10s
created

OfferCommandHandler::getRemoveLabelClassName()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 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\AbstractRemoveImage;
32
use CultuurNet\UDB3\Offer\Commands\Image\AbstractSelectMainImage;
33
use CultuurNet\UDB3\Offer\Commands\Image\AbstractUpdateImage;
34
use CultuurNet\UDB3\Offer\Commands\AbstractUpdateTitle;
35
use CultuurNet\UDB3\Offer\Commands\Moderation\AbstractApprove;
36
use CultuurNet\UDB3\Offer\Commands\Moderation\AbstractFlagAsDuplicate;
37
use CultuurNet\UDB3\Offer\Commands\Moderation\AbstractFlagAsInappropriate;
38
use CultuurNet\UDB3\Offer\Commands\Moderation\AbstractPublish;
39
use CultuurNet\UDB3\Offer\Commands\Moderation\AbstractReject;
40
use CultuurNet\UDB3\Organizer\Organizer;
41
use ValueObjects\StringLiteral\StringLiteral;
42
43
abstract class OfferCommandHandler extends Udb3CommandHandler
44
{
45
    /**
46
     * @var RepositoryInterface
47
     */
48
    protected $offerRepository;
49
50
    /**
51
     * @var RepositoryInterface
52
     */
53
    protected $organizerRepository;
54
55
    /**
56
     * @var RepositoryInterface
57
     */
58
    protected $labelRepository;
59
60
    /**
61
     * @var MediaManagerInterface|MediaManager
62
     */
63
    protected $mediaManager;
64
65
    /**
66
     * @param RepositoryInterface $offerRepository
67
     * @param RepositoryInterface $organizerRepository
68
     * @param ReadRepositoryInterface $labelRepository
69
     * @param MediaManagerInterface $mediaManager
70
     */
71
    public function __construct(
72
        RepositoryInterface $offerRepository,
73
        RepositoryInterface $organizerRepository,
74
        ReadRepositoryInterface $labelRepository,
75
        MediaManagerInterface $mediaManager
76
    ) {
77
        $this->offerRepository = $offerRepository;
78
        $this->organizerRepository = $organizerRepository;
79
        $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...
80
        $this->mediaManager = $mediaManager;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 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...
87
    {
88
        $commandName = get_class($command);
89
        $commandHandlers = $this->getCommandHandlers();
90
91
        if (isset($commandHandlers[$commandName])) {
92
            $handler = $commandHandlers[$commandName];
93
            call_user_func(array($this, $handler), $command);
94
        } else {
95
            parent::handle($command);
96
        }
97
    }
98
99
    /**
100
     * @return string[]
101
     *   An associative array of commands and their handler methods.
102
     */
103 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...
104
    {
105
        $commands = [];
106
107
        foreach (get_class_methods($this) as $method) {
108
            $matches = [];
109
            if (preg_match('/^handle(.+)$/', $method, $matches)) {
110
                $command = $matches[1];
111
                $classNameMethod = 'get' . $command . 'ClassName';
112
113
                if (method_exists($this, $classNameMethod)) {
114
                    $commandFullClassName = call_user_func(array($this, $classNameMethod));
115
                    $commands[$commandFullClassName] = $method;
116
                }
117
            }
118
        }
119
120
        return $commands;
121
    }
122
123
    /**
124
     * @return string
125
     */
126
    abstract protected function getAddLabelClassName();
127
128
    /**
129
     * @return string
130
     */
131
    abstract protected function getRemoveLabelClassName();
132
133
    /**
134
     * @return string
135
     */
136
    abstract protected function getImportLabelsClassName();
137
138
    /**
139
     * @return string
140
     */
141
    abstract protected function getUpdateTitleClassName();
142
143
    /**
144
     * @return string
145
     */
146
    abstract protected function getAddImageClassName();
147
148
    /**
149
     * @return string
150
     */
151
    abstract protected function getUpdateImageClassName();
152
153
    /**
154
     * @return string
155
     */
156
    abstract protected function getRemoveImageClassName();
157
158
    /**
159
     * @return string
160
     */
161
    abstract protected function getSelectMainImageClassName();
162
163
    /**
164
     * @return string
165
     */
166
    abstract protected function getUpdateDescriptionClassName();
167
168
    /**
169
     * @return string
170
     */
171
    abstract protected function getUpdateCalendarClassName();
172
173
    /**
174
     * @return string
175
     */
176
    abstract protected function getUpdateTypicalAgeRangeClassName();
177
178
    /**
179
     * @return string
180
     */
181
    abstract protected function getDeleteTypicalAgeRangeClassName();
182
183
    /**
184
     * @return string
185
     */
186
    abstract protected function getUpdateOrganizerClassName();
187
188
    /**
189
     * @return string
190
     */
191
    abstract protected function getDeleteOrganizerClassName();
192
193
    /**
194
     * @return string
195
     */
196
    abstract protected function getDeleteCurrentOrganizerClassName();
197
198
    /**
199
     * @return string
200
     */
201
    abstract protected function getUpdateContactPointClassName();
202
203
    /**
204
     * @return string
205
     */
206
    abstract protected function getUpdateBookingInfoClassName();
207
208
    /**
209
     * @return string
210
     */
211
    abstract protected function getUpdatePriceInfoClassName();
212
213
    /**
214
     * @return string
215
     */
216
    abstract protected function getDeleteOfferClassName();
217
218
    /**
219
     * @return string
220
     */
221
    abstract protected function getPublishClassName();
222
223
    /**
224
     * @return string
225
     */
226
    abstract protected function getApproveClassName();
227
228
    /**
229
     * @return string
230
     */
231
    abstract protected function getRejectClassName();
232
233
    /**
234
     * @return string
235
     */
236
    abstract protected function getFlagAsDuplicateClassName();
237
238
    /**
239
     * @return string
240
     */
241
    abstract protected function getFlagAsInappropriateClassName();
242
243
    /**
244
     * @return string
245
     */
246
    abstract protected function getUpdateTypeClassName();
247
248
    /**
249
     * @return string
250
     */
251
    abstract protected function getUpdateThemeClassName();
252
253
    /**
254
     * @return string
255
     */
256
    abstract protected function getUpdateFacilitiesClassName();
257
258
    /**
259
     * @param AbstractUpdateType $updateType
260
     */
261
    public function handleUpdateType(AbstractUpdateType $updateType)
262
    {
263
        $offer = $this->load($updateType->getItemId());
264
265
        $offer->updateType($updateType->getType());
266
267
        $this->offerRepository->save($offer);
268
    }
269
270
    /**
271
     * @param AbstractUpdateTheme $updateTheme
272
     */
273
    public function handleUpdateTheme(AbstractUpdateTheme $updateTheme)
274
    {
275
        $offer = $this->load($updateTheme->getItemId());
276
277
        $offer->updateTheme($updateTheme->getTheme());
278
279
        $this->offerRepository->save($offer);
280
    }
281
282
    /**
283
     * @param AbstractUpdateFacilities $updateFacilities
284
     */
285
    public function handleUpdateFacilities(AbstractUpdateFacilities $updateFacilities)
286
    {
287
        $offer = $this->load($updateFacilities->getItemId());
288
289
        $offer->updateFacilities($updateFacilities->getFacilities());
290
291
        $this->offerRepository->save($offer);
292
    }
293
294
    /**
295
     * @param AbstractAddLabel $addLabel
296
     */
297
    private function handleAddLabel(AbstractAddLabel $addLabel)
298
    {
299
        $offer = $this->load($addLabel->getItemId());
300
301
        $offer->addLabel($this->createLabel($addLabel));
302
303
        $this->offerRepository->save($offer);
304
    }
305
306
    /**
307
     * @param AbstractRemoveLabel $removeLabel
308
     */
309
    private function handleRemoveLabel(AbstractRemoveLabel $removeLabel)
310
    {
311
        $offer = $this->load($removeLabel->getItemId());
312
313
        $offer->removeLabel($this->createLabel($removeLabel));
314
315
        $this->offerRepository->save($offer);
316
    }
317
318
    /**
319
     * @param AbstractImportLabels $importLabels
320
     */
321
    private function handleImportLabels(AbstractImportLabels $importLabels)
322
    {
323
        $offer = $this->load($importLabels->getItemId());
324
325
        $offer->importLabels($importLabels->getLabels());
326
327
        $this->offerRepository->save($offer);
328
    }
329
330
    /**
331
     * @param AbstractLabelCommand $labelCommand
332
     * @return Label
333
     */
334 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...
335
    {
336
        $labelName = new StringLiteral((string) $labelCommand->getLabel());
337
        $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...
338
339
        return new Label(
340
            $labelName->toNative(),
341
            $label->getVisibility() === Visibility::VISIBLE()
342
        );
343
    }
344
345
    /**
346
     * @param AbstractUpdateTitle $translateTitle
347
     */
348
    private function handleUpdateTitle(AbstractUpdateTitle $translateTitle)
349
    {
350
        $offer = $this->load($translateTitle->getItemId());
351
        $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...
352
        $this->offerRepository->save($offer);
353
    }
354
355
    /**
356
     * Handle an add image command.
357
     * @param AbstractAddImage $addImage
358
     */
359
    public function handleAddImage(AbstractAddImage $addImage)
360
    {
361
        $offer = $this->load($addImage->getItemId());
362
363
        $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...
364
        $offer->addImage($image);
365
366
        $this->offerRepository->save($offer);
367
    }
368
369
    /**
370
     * @param AbstractRemoveImage $removeImage
371
     */
372
    public function handleRemoveImage(AbstractRemoveImage $removeImage)
373
    {
374
        $offer = $this->load($removeImage->getItemId());
375
        $offer->removeImage($removeImage->getImage());
376
        $this->offerRepository->save($offer);
377
    }
378
379
    /**
380
     * @param AbstractUpdateImage $updateImage
381
     */
382
    public function handleUpdateImage(AbstractUpdateImage $updateImage)
383
    {
384
        $offer = $this->load($updateImage->getItemId());
385
        $offer->updateImage($updateImage);
386
        $this->offerRepository->save($offer);
387
    }
388
389
    /**
390
     * @param AbstractSelectMainImage $selectMainImage
391
     */
392
    public function handleSelectMainImage(AbstractSelectMainImage $selectMainImage)
393
    {
394
        $offer = $this->load($selectMainImage->getItemId());
395
        $offer->selectMainImage($selectMainImage->getImage());
396
        $this->offerRepository->save($offer);
397
    }
398
399
    /**
400
     * Handle the update of description on a place.
401
     * @param AbstractUpdateDescription $updateDescription
402
     */
403
    public function handleUpdateDescription(AbstractUpdateDescription $updateDescription)
404
    {
405
        $offer = $this->load($updateDescription->getItemId());
406
407
        $offer->updateDescription(
408
            $updateDescription->getDescription(),
409
            $updateDescription->getLanguage()
410
        );
411
412
        $this->offerRepository->save($offer);
413
414
    }
415
416
    /**
417
     * @param AbstractUpdateCalendar $updateCalendar
418
     */
419
    public function handleUpdateCalendar(AbstractUpdateCalendar $updateCalendar)
420
    {
421
        $offer = $this->load($updateCalendar->getItemId());
422
423
        $offer->updateCalendar($updateCalendar->getCalendar());
424
425
        $this->offerRepository->save($offer);
426
    }
427
428
    /**
429
     * Handle the update of typical age range on a place.
430
     * @param AbstractUpdateTypicalAgeRange $updateTypicalAgeRange
431
     */
432
    public function handleUpdateTypicalAgeRange(AbstractUpdateTypicalAgeRange $updateTypicalAgeRange)
433
    {
434
        $offer = $this->load($updateTypicalAgeRange->getItemId());
435
436
        $offer->updateTypicalAgeRange(
437
            $updateTypicalAgeRange->getTypicalAgeRange()
438
        );
439
440
        $this->offerRepository->save($offer);
441
442
    }
443
444
    /**
445
     * Handle the deletion of typical age range on a place.
446
     * @param AbstractDeleteTypicalAgeRange $deleteTypicalAgeRange
447
     */
448
    public function handleDeleteTypicalAgeRange(AbstractDeleteTypicalAgeRange $deleteTypicalAgeRange)
449
    {
450
        $offer = $this->load($deleteTypicalAgeRange->getItemId());
451
452
        $offer->deleteTypicalAgeRange();
453
454
        $this->offerRepository->save($offer);
455
456
    }
457
458
    /**
459
     * Handle an update command to update organizer of a place.
460
     * @param AbstractUpdateOrganizer $updateOrganizer
461
     */
462
    public function handleUpdateOrganizer(AbstractUpdateOrganizer $updateOrganizer)
463
    {
464
        $offer = $this->load($updateOrganizer->getItemId());
465
        $this->loadOrganizer($updateOrganizer->getOrganizerId());
466
467
        $offer->updateOrganizer(
468
            $updateOrganizer->getOrganizerId()
469
        );
470
471
        $this->offerRepository->save($offer);
472
    }
473
474
    /**
475
     * Handle an update command to delete the organizer.
476
     * @param AbstractDeleteOrganizer $deleteOrganizer
477
     */
478
    public function handleDeleteOrganizer(AbstractDeleteOrganizer $deleteOrganizer)
479
    {
480
        $offer = $this->load($deleteOrganizer->getItemId());
481
482
        $offer->deleteOrganizer(
483
            $deleteOrganizer->getOrganizerId()
484
        );
485
486
        $this->offerRepository->save($offer);
487
    }
488
489
    /**
490
     * @param AbstractDeleteCurrentOrganizer $deleteCurrentOrganizer
491
     */
492
    public function handleDeleteCurrentOrganizer(AbstractDeleteCurrentOrganizer $deleteCurrentOrganizer)
493
    {
494
        $offer = $this->load($deleteCurrentOrganizer->getItemId());
495
496
        $offer->deleteCurrentOrganizer();
497
498
        $this->offerRepository->save($offer);
499
    }
500
501
    /**
502
     * Handle an update command to updated the contact point.
503
     * @param AbstractUpdateContactPoint $updateContactPoint
504
     */
505
    public function handleUpdateContactPoint(AbstractUpdateContactPoint $updateContactPoint)
506
    {
507
        $offer = $this->load($updateContactPoint->getItemId());
508
509
        $offer->updateContactPoint(
510
            $updateContactPoint->getContactPoint()
511
        );
512
513
        $this->offerRepository->save($offer);
514
515
    }
516
517
    /**
518
     * Handle an update command to updated the booking info.
519
     * @param AbstractUpdateBookingInfo $updateBookingInfo
520
     */
521
    public function handleUpdateBookingInfo(AbstractUpdateBookingInfo $updateBookingInfo)
522
    {
523
        $offer = $this->load($updateBookingInfo->getItemId());
524
525
        $offer->updateBookingInfo(
526
            $updateBookingInfo->getBookingInfo()
527
        );
528
529
        $this->offerRepository->save($offer);
530
    }
531
532
    /**
533
     * @param AbstractUpdatePriceInfo $updatePriceInfo
534
     */
535
    private function handleUpdatePriceInfo(AbstractUpdatePriceInfo $updatePriceInfo)
536
    {
537
        $offer = $this->load($updatePriceInfo->getItemId());
538
539
        $offer->updatePriceInfo(
540
            $updatePriceInfo->getPriceInfo()
541
        );
542
543
        $this->offerRepository->save($offer);
544
    }
545
546
    /**
547
     * @param AbstractDeleteOffer $deleteOffer
548
     */
549
    private function handleDeleteOffer(AbstractDeleteOffer $deleteOffer)
550
    {
551
        $offer = $this->load($deleteOffer->getItemId());
552
        $offer->delete();
553
        $this->offerRepository->save($offer);
554
    }
555
556
    /**
557
     * @param AbstractPublish $publish
558
     */
559
    private function handlePublish(AbstractPublish $publish)
560
    {
561
        $offer = $this->load($publish->getItemId());
562
        $offer->publish($publish->getPublicationDate());
563
        $this->offerRepository->save($offer);
564
    }
565
566
    /**
567
     * @param AbstractApprove $approve
568
     */
569
    private function handleApprove(AbstractApprove $approve)
570
    {
571
        $offer = $this->load($approve->getItemId());
572
        $offer->approve();
573
        $this->offerRepository->save($offer);
574
    }
575
576
    /**
577
     * @param AbstractReject $reject
578
     */
579
    private function handleReject(AbstractReject $reject)
580
    {
581
        $offer = $this->load($reject->getItemId());
582
        $offer->reject($reject->getReason());
583
        $this->offerRepository->save($offer);
584
    }
585
586
    /**
587
     * @param AbstractFlagAsDuplicate $flagAsDuplicate
588
     */
589
    private function handleFlagAsDuplicate(AbstractFlagAsDuplicate $flagAsDuplicate)
590
    {
591
        $offer = $this->load($flagAsDuplicate->getItemId());
592
        $offer->flagAsDuplicate();
593
        $this->offerRepository->save($offer);
594
    }
595
596
    /**
597
     * @param AbstractFlagAsInappropriate $flagAsInappropriate
598
     */
599
    private function handleFlagAsInappropriate(AbstractFlagAsInappropriate $flagAsInappropriate)
600
    {
601
        $offer = $this->load($flagAsInappropriate->getItemId());
602
        $offer->flagAsInappropriate();
603
        $this->offerRepository->save($offer);
604
    }
605
606
    /**
607
     * Makes it easier to type-hint to Offer.
608
     *
609
     * @param string $id
610
     * @return Offer
611
     */
612
    private function load($id)
613
    {
614
        return $this->offerRepository->load($id);
615
    }
616
617
    /**
618
     * Makes it easier to type-hint to Organizer.
619
     *
620
     * @param string $id
621
     * @return Organizer
622
     */
623
    private function loadOrganizer($id)
624
    {
625
        return $this->organizerRepository->load($id);
626
627
    }
628
}
629