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