Completed
Pull Request — master (#238)
by Luc
09:35 queued 04:39
created

OfferCommandHandler::handleSyncLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
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\Offer\Commands\AbstractAddLabel;
11
use CultuurNet\UDB3\Offer\Commands\AbstractDeleteLabel;
12
use CultuurNet\UDB3\Offer\Commands\AbstractDeleteOffer;
13
use CultuurNet\UDB3\Offer\Commands\AbstractDeleteOrganizer;
14
use CultuurNet\UDB3\Offer\Commands\AbstractDeleteTypicalAgeRange;
15
use CultuurNet\UDB3\Offer\Commands\AbstractSyncLabels;
16
use CultuurNet\UDB3\Offer\Commands\AbstractUpdateBookingInfo;
17
use CultuurNet\UDB3\Offer\Commands\AbstractUpdateContactPoint;
18
use CultuurNet\UDB3\Offer\Commands\AbstractUpdateDescription;
19
use CultuurNet\UDB3\Offer\Commands\AbstractUpdateOrganizer;
20
use CultuurNet\UDB3\Offer\Commands\AbstractUpdatePriceInfo;
21
use CultuurNet\UDB3\Offer\Commands\AbstractUpdateTypicalAgeRange;
22
use CultuurNet\UDB3\Offer\Commands\Image\AbstractAddImage;
23
use CultuurNet\UDB3\Offer\Commands\Image\AbstractRemoveImage;
24
use CultuurNet\UDB3\Offer\Commands\Image\AbstractSelectMainImage;
25
use CultuurNet\UDB3\Offer\Commands\Image\AbstractUpdateImage;
26
use CultuurNet\UDB3\Offer\Commands\AbstractTranslateDescription;
27
use CultuurNet\UDB3\Offer\Commands\AbstractTranslateTitle;
28
use CultuurNet\UDB3\Offer\Commands\Moderation\AbstractApprove;
29
use CultuurNet\UDB3\Offer\Commands\Moderation\AbstractFlagAsDuplicate;
30
use CultuurNet\UDB3\Offer\Commands\Moderation\AbstractFlagAsInappropriate;
31
use CultuurNet\UDB3\Offer\Commands\Moderation\AbstractPublish;
32
use CultuurNet\UDB3\Offer\Commands\Moderation\AbstractReject;
33
use CultuurNet\UDB3\Organizer\Organizer;
34
use ValueObjects\String\String as StringLiteral;
35
36
abstract class OfferCommandHandler extends Udb3CommandHandler
37
{
38
    /**
39
     * @var RepositoryInterface
40
     */
41
    protected $offerRepository;
42
43
    /**
44
     * @var RepositoryInterface
45
     */
46
    protected $organizerRepository;
47
48
    /**
49
     * @var RepositoryInterface
50
     */
51
    protected $labelRepository;
52
53
    /**
54
     * @param RepositoryInterface $offerRepository
55
     * @param RepositoryInterface $organizerRepository
56
     * @param ReadRepositoryInterface $labelRepository
57
     */
58
    public function __construct(
59
        RepositoryInterface $offerRepository,
60
        RepositoryInterface $organizerRepository,
61
        ReadRepositoryInterface $labelRepository
62
    ) {
63
        $this->offerRepository = $offerRepository;
64
        $this->organizerRepository = $organizerRepository;
65
        $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...
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function handle($command)
72
    {
73
        $commandName = get_class($command);
74
        $commandHandlers = $this->getCommandHandlers();
75
76
        if (isset($commandHandlers[$commandName])) {
77
            $handler = $commandHandlers[$commandName];
78
            call_user_func(array($this, $handler), $command);
79
        } else {
80
            parent::handle($command);
81
        }
82
    }
83
84
    /**
85
     * @return string[]
86
     *   An associative array of commands and their handler methods.
87
     */
88 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...
89
    {
90
        $commands = [];
91
92
        foreach (get_class_methods($this) as $method) {
93
            $matches = [];
94
            if (preg_match('/^handle(.+)$/', $method, $matches)) {
95
                $command = $matches[1];
96
                $classNameMethod = 'get' . $command . 'ClassName';
97
98
                if (method_exists($this, $classNameMethod)) {
99
                    $commandFullClassName = call_user_func(array($this, $classNameMethod));
100
                    $commands[$commandFullClassName] = $method;
101
                }
102
            }
103
        }
104
105
        return $commands;
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    abstract protected function getAddLabelClassName();
112
113
    /**
114
     * @return string
115
     */
116
    abstract protected function getDeleteLabelClassName();
117
118
    /**
119
     * @return string
120
     */
121
    abstract protected function getSyncLabelsClassName();
122
123
    /**
124
     * @return string
125
     */
126
    abstract protected function getTranslateTitleClassName();
127
128
    /**
129
     * @return string
130
     */
131
    abstract protected function getTranslateDescriptionClassName();
132
133
    /**
134
     * @return string
135
     */
136
    abstract protected function getAddImageClassName();
137
138
    /**
139
     * @return string
140
     */
141
    abstract protected function getUpdateImageClassName();
142
143
    /**
144
     * @return string
145
     */
146
    abstract protected function getRemoveImageClassName();
147
148
    /**
149
     * @return string
150
     */
151
    abstract protected function getSelectMainImageClassName();
152
153
    /**
154
     * @return string
155
     */
156
    abstract protected function getUpdateDescriptionClassName();
157
158
    /**
159
     * @return string
160
     */
161
    abstract protected function getUpdateTypicalAgeRangeClassName();
162
163
    /**
164
     * @return string
165
     */
166
    abstract protected function getDeleteTypicalAgeRangeClassName();
167
168
    /**
169
     * @return string
170
     */
171
    abstract protected function getUpdateOrganizerClassName();
172
173
    /**
174
     * @return string
175
     */
176
    abstract protected function getDeleteOrganizerClassName();
177
178
    /**
179
     * @return string
180
     */
181
    abstract protected function getUpdateContactPointClassName();
182
183
    /**
184
     * @return string
185
     */
186
    abstract protected function getUpdateBookingInfoClassName();
187
188
    /**
189
     * @return string
190
     */
191
    abstract protected function getUpdatePriceInfoClassName();
192
193
    /**
194
     * @return string
195
     */
196
    abstract protected function getDeleteOfferClassName();
197
198
    /**
199
     * @return string
200
     */
201
    abstract protected function getPublishClassName();
202
203
    /**
204
     * @return string
205
     */
206
    abstract protected function getApproveClassName();
207
208
    /**
209
     * @return string
210
     */
211
    abstract protected function getRejectClassName();
212
213
    /**
214
     * @return string
215
     */
216
    abstract protected function getFlagAsDuplicateClassName();
217
218
    /**
219
     * @return string
220
     */
221
    abstract protected function getFlagAsInappropriateClassName();
222
223
    /**
224
     * @param AbstractAddLabel $addLabel
225
     */
226
    private function handleAddLabel(AbstractAddLabel $addLabel)
227
    {
228
        $offer = $this->load($addLabel->getItemId());
229
230
        $labelName = new StringLiteral((string)$addLabel->getLabel());
231
        $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...
232
233
        $offer->addLabel(new Label(
234
            $label->getName()->toNative(),
235
            $label->getVisibility() === Visibility::VISIBLE()
236
        ));
237
238
        $this->offerRepository->save($offer);
239
    }
240
241
    /**
242
     * @param AbstractDeleteLabel $deleteLabel
243
     */
244
    private function handleDeleteLabel(AbstractDeleteLabel $deleteLabel)
245
    {
246
        $offer = $this->load($deleteLabel->getItemId());
247
        $offer->deleteLabel($deleteLabel->getLabel());
248
        $this->offerRepository->save($offer);
249
    }
250
251
    /**
252
     * @param AbstractSyncLabels $syncLabels
253
     */
254
    protected function handleSyncLabels(AbstractSyncLabels $syncLabels)
255
    {
256
        $offer = $this->load($syncLabels->getItemId());
257
        $offer->syncLabels($syncLabels->getLabelCollection());
258
        $this->offerRepository->save($offer);
259
    }
260
261
    /**
262
     * @param AbstractTranslateTitle $translateTitle
263
     */
264
    private function handleTranslateTitle(AbstractTranslateTitle $translateTitle)
265
    {
266
        $offer = $this->load($translateTitle->getItemId());
267
        $offer->translateTitle($translateTitle->getLanguage(), $translateTitle->getTitle());
0 ignored issues
show
Documentation introduced by
$translateTitle->getTitle() is of type string, but the function expects a object<ValueObjects\String\String>.

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...
268
        $this->offerRepository->save($offer);
269
    }
270
271
    /**
272
     * @param AbstractTranslateDescription $translateDescription
273
     */
274
    private function handleTranslateDescription(AbstractTranslateDescription $translateDescription)
275
    {
276
        $offer = $this->load($translateDescription->getItemId());
277
        $offer->translateDescription($translateDescription->getLanguage(), $translateDescription->getDescription());
0 ignored issues
show
Documentation introduced by
$translateDescription->getDescription() is of type string, but the function expects a object<ValueObjects\String\String>.

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...
278
        $this->offerRepository->save($offer);
279
    }
280
281
    /**
282
     * Handle an add image command.
283
     * @param AbstractAddImage $addImage
284
     */
285
    public function handleAddImage(AbstractAddImage $addImage)
286
    {
287
        $offer = $this->load($addImage->getItemId());
288
        $offer->addImage($addImage->getImage());
289
        $this->offerRepository->save($offer);
290
    }
291
292
    /**
293
     * @param AbstractRemoveImage $removeImage
294
     */
295
    public function handleRemoveImage(AbstractRemoveImage $removeImage)
296
    {
297
        $offer = $this->load($removeImage->getItemId());
298
        $offer->removeImage($removeImage->getImage());
299
        $this->offerRepository->save($offer);
300
    }
301
302
    /**
303
     * @param AbstractUpdateImage $updateImage
304
     */
305
    public function handleUpdateImage(AbstractUpdateImage $updateImage)
306
    {
307
        $offer = $this->load($updateImage->getItemId());
308
        $offer->updateImage($updateImage);
309
        $this->offerRepository->save($offer);
310
    }
311
312
    /**
313
     * @param AbstractSelectMainImage $selectMainImage
314
     */
315
    public function handleSelectMainImage(AbstractSelectMainImage $selectMainImage)
316
    {
317
        $offer = $this->load($selectMainImage->getItemId());
318
        $offer->selectMainImage($selectMainImage->getImage());
319
        $this->offerRepository->save($offer);
320
    }
321
322
    /**
323
     * Handle the update of description on a place.
324
     * @param AbstractUpdateDescription $updateDescription
325
     */
326
    public function handleUpdateDescription(AbstractUpdateDescription $updateDescription)
327
    {
328
        $offer = $this->load($updateDescription->getItemId());
329
330
        $offer->updateDescription(
331
            $updateDescription->getDescription()
332
        );
333
334
        $this->offerRepository->save($offer);
335
336
    }
337
338
    /**
339
     * Handle the update of typical age range on a place.
340
     * @param AbstractUpdateTypicalAgeRange $updateTypicalAgeRange
341
     */
342
    public function handleUpdateTypicalAgeRange(AbstractUpdateTypicalAgeRange $updateTypicalAgeRange)
343
    {
344
        $offer = $this->load($updateTypicalAgeRange->getItemId());
345
346
        $offer->updateTypicalAgeRange(
347
            $updateTypicalAgeRange->getTypicalAgeRange()
348
        );
349
350
        $this->offerRepository->save($offer);
351
352
    }
353
354
    /**
355
     * Handle the deletion of typical age range on a place.
356
     * @param AbstractDeleteTypicalAgeRange $deleteTypicalAgeRange
357
     */
358
    public function handleDeleteTypicalAgeRange(AbstractDeleteTypicalAgeRange $deleteTypicalAgeRange)
359
    {
360
        $offer = $this->load($deleteTypicalAgeRange->getItemId());
361
362
        $offer->deleteTypicalAgeRange();
363
364
        $this->offerRepository->save($offer);
365
366
    }
367
368
    /**
369
     * Handle an update command to update organizer of a place.
370
     * @param AbstractUpdateOrganizer $updateOrganizer
371
     */
372
    public function handleUpdateOrganizer(AbstractUpdateOrganizer $updateOrganizer)
373
    {
374
        $offer = $this->load($updateOrganizer->getItemId());
375
        $this->loadOrganizer($updateOrganizer->getOrganizerId());
376
377
        $offer->updateOrganizer(
378
            $updateOrganizer->getOrganizerId()
379
        );
380
381
        $this->offerRepository->save($offer);
382
    }
383
384
    /**
385
     * Handle an update command to delete the organizer.
386
     * @param AbstractDeleteOrganizer $deleteOrganizer
387
     */
388
    public function handleDeleteOrganizer(AbstractDeleteOrganizer $deleteOrganizer)
389
    {
390
        $offer = $this->load($deleteOrganizer->getItemId());
391
392
        $offer->deleteOrganizer(
393
            $deleteOrganizer->getOrganizerId()
394
        );
395
396
        $this->offerRepository->save($offer);
397
    }
398
399
    /**
400
     * Handle an update command to updated the contact point.
401
     * @param AbstractUpdateContactPoint $updateContactPoint
402
     */
403
    public function handleUpdateContactPoint(AbstractUpdateContactPoint $updateContactPoint)
404
    {
405
        $offer = $this->load($updateContactPoint->getId());
406
407
        $offer->updateContactPoint(
408
            $updateContactPoint->getContactPoint()
409
        );
410
411
        $this->offerRepository->save($offer);
412
413
    }
414
415
    /**
416
     * Handle an update command to updated the booking info.
417
     * @param AbstractUpdateBookingInfo $updateBookingInfo
418
     */
419
    public function handleUpdateBookingInfo(AbstractUpdateBookingInfo $updateBookingInfo)
420
    {
421
        $offer = $this->load($updateBookingInfo->getItemId());
422
423
        $offer->updateBookingInfo(
424
            $updateBookingInfo->getBookingInfo()
425
        );
426
427
        $this->offerRepository->save($offer);
428
    }
429
430
    /**
431
     * @param AbstractUpdatePriceInfo $updatePriceInfo
432
     */
433
    private function handleUpdatePriceInfo(AbstractUpdatePriceInfo $updatePriceInfo)
434
    {
435
        $offer = $this->load($updatePriceInfo->getItemId());
436
437
        $offer->updatePriceInfo(
438
            $updatePriceInfo->getPriceInfo()
439
        );
440
441
        $this->offerRepository->save($offer);
442
    }
443
444
    /**
445
     * @param AbstractDeleteOffer $deleteOffer
446
     */
447
    private function handleDeleteOffer(AbstractDeleteOffer $deleteOffer)
448
    {
449
        $offer = $this->load($deleteOffer->getItemId());
450
        $offer->delete();
451
        $this->offerRepository->save($offer);
452
    }
453
454
    /**
455
     * @param AbstractPublish $publish
456
     */
457
    private function handlePublish(AbstractPublish $publish)
458
    {
459
        $offer = $this->load($publish->getItemId());
460
        $offer->publish($publish->getPublicationDate());
461
        $this->offerRepository->save($offer);
462
    }
463
464
    /**
465
     * @param AbstractApprove $approve
466
     */
467
    private function handleApprove(AbstractApprove $approve)
468
    {
469
        $offer = $this->load($approve->getItemId());
470
        $offer->approve();
471
        $this->offerRepository->save($offer);
472
    }
473
474
    /**
475
     * @param AbstractReject $reject
476
     */
477
    private function handleReject(AbstractReject $reject)
478
    {
479
        $offer = $this->load($reject->getItemId());
480
        $offer->reject($reject->getReason());
481
        $this->offerRepository->save($offer);
482
    }
483
484
    /**
485
     * @param AbstractFlagAsDuplicate $flagAsDuplicate
486
     */
487
    private function handleFlagAsDuplicate(AbstractFlagAsDuplicate $flagAsDuplicate)
488
    {
489
        $offer = $this->load($flagAsDuplicate->getItemId());
490
        $offer->flagAsDuplicate();
491
        $this->offerRepository->save($offer);
492
    }
493
494
    /**
495
     * @param AbstractFlagAsInappropriate $flagAsInappropriate
496
     */
497
    private function handleFlagAsInappropriate(AbstractFlagAsInappropriate $flagAsInappropriate)
498
    {
499
        $offer = $this->load($flagAsInappropriate->getItemId());
500
        $offer->flagAsInappropriate();
501
        $this->offerRepository->save($offer);
502
    }
503
504
    /**
505
     * Makes it easier to type-hint to Offer.
506
     *
507
     * @param string $id
508
     * @return Offer
509
     */
510
    private function load($id)
511
    {
512
        return $this->offerRepository->load($id);
513
    }
514
515
    /**
516
     * Makes it easier to type-hint to Organizer.
517
     *
518
     * @param string $id
519
     * @return Organizer
520
     */
521
    private function loadOrganizer($id)
522
    {
523
        return $this->organizerRepository->load($id);
524
525
    }
526
}
527