Completed
Pull Request — master (#350)
by
unknown
05:43
created

handleDeleteCurrentOrganizer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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