Completed
Pull Request — master (#244)
by Luc
04:17
created

OfferCommandHandler::createLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
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\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 getRemoveLabelClassName();
117
118
    /**
119
     * @return string
120
     */
121
    abstract protected function getTranslateTitleClassName();
122
123
    /**
124
     * @return string
125
     */
126
    abstract protected function getTranslateDescriptionClassName();
127
128
    /**
129
     * @return string
130
     */
131
    abstract protected function getAddImageClassName();
132
133
    /**
134
     * @return string
135
     */
136
    abstract protected function getUpdateImageClassName();
137
138
    /**
139
     * @return string
140
     */
141
    abstract protected function getRemoveImageClassName();
142
143
    /**
144
     * @return string
145
     */
146
    abstract protected function getSelectMainImageClassName();
147
148
    /**
149
     * @return string
150
     */
151
    abstract protected function getUpdateDescriptionClassName();
152
153
    /**
154
     * @return string
155
     */
156
    abstract protected function getUpdateTypicalAgeRangeClassName();
157
158
    /**
159
     * @return string
160
     */
161
    abstract protected function getDeleteTypicalAgeRangeClassName();
162
163
    /**
164
     * @return string
165
     */
166
    abstract protected function getUpdateOrganizerClassName();
167
168
    /**
169
     * @return string
170
     */
171
    abstract protected function getDeleteOrganizerClassName();
172
173
    /**
174
     * @return string
175
     */
176
    abstract protected function getUpdateContactPointClassName();
177
178
    /**
179
     * @return string
180
     */
181
    abstract protected function getUpdateBookingInfoClassName();
182
183
    /**
184
     * @return string
185
     */
186
    abstract protected function getUpdatePriceInfoClassName();
187
188
    /**
189
     * @return string
190
     */
191
    abstract protected function getDeleteOfferClassName();
192
193
    /**
194
     * @return string
195
     */
196
    abstract protected function getPublishClassName();
197
198
    /**
199
     * @return string
200
     */
201
    abstract protected function getApproveClassName();
202
203
    /**
204
     * @return string
205
     */
206
    abstract protected function getRejectClassName();
207
208
    /**
209
     * @return string
210
     */
211
    abstract protected function getFlagAsDuplicateClassName();
212
213
    /**
214
     * @return string
215
     */
216
    abstract protected function getFlagAsInappropriateClassName();
217
218
    /**
219
     * @param AbstractAddLabel $addLabel
220
     */
221
    private function handleAddLabel(AbstractAddLabel $addLabel)
222
    {
223
        $offer = $this->load($addLabel->getItemId());
224
225
        $offer->addLabel($this->createLabel($addLabel));
226
227
        $this->offerRepository->save($offer);
228
    }
229
230
    /**
231
     * @param AbstractRemoveLabel $removeLabel
232
     */
233
    private function handleRemoveLabel(AbstractRemoveLabel $removeLabel)
234
    {
235
        $offer = $this->load($removeLabel->getItemId());
236
237
        $offer->removeLabel($this->createLabel($removeLabel));
238
239
        $this->offerRepository->save($offer);
240
    }
241
242
    /**
243
     * @param AbstractLabelCommand $labelCommand
244
     * @return Label
245
     */
246 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...
247
    {
248
        $labelName = new StringLiteral((string) $labelCommand->getLabel());
249
        $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...
250
251
        return new Label(
252
            $labelName->toNative(),
253
            $label->getVisibility() === Visibility::VISIBLE()
254
        );
255
    }
256
257
    /**
258
     * @param AbstractTranslateTitle $translateTitle
259
     */
260
    private function handleTranslateTitle(AbstractTranslateTitle $translateTitle)
261
    {
262
        $offer = $this->load($translateTitle->getItemId());
263
        $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...
264
        $this->offerRepository->save($offer);
265
    }
266
267
    /**
268
     * @param AbstractTranslateDescription $translateDescription
269
     */
270
    private function handleTranslateDescription(AbstractTranslateDescription $translateDescription)
271
    {
272
        $offer = $this->load($translateDescription->getItemId());
273
        $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...
274
        $this->offerRepository->save($offer);
275
    }
276
277
    /**
278
     * Handle an add image command.
279
     * @param AbstractAddImage $addImage
280
     */
281
    public function handleAddImage(AbstractAddImage $addImage)
282
    {
283
        $offer = $this->load($addImage->getItemId());
284
        $offer->addImage($addImage->getImage());
285
        $this->offerRepository->save($offer);
286
    }
287
288
    /**
289
     * @param AbstractRemoveImage $removeImage
290
     */
291
    public function handleRemoveImage(AbstractRemoveImage $removeImage)
292
    {
293
        $offer = $this->load($removeImage->getItemId());
294
        $offer->removeImage($removeImage->getImage());
295
        $this->offerRepository->save($offer);
296
    }
297
298
    /**
299
     * @param AbstractUpdateImage $updateImage
300
     */
301
    public function handleUpdateImage(AbstractUpdateImage $updateImage)
302
    {
303
        $offer = $this->load($updateImage->getItemId());
304
        $offer->updateImage($updateImage);
305
        $this->offerRepository->save($offer);
306
    }
307
308
    /**
309
     * @param AbstractSelectMainImage $selectMainImage
310
     */
311
    public function handleSelectMainImage(AbstractSelectMainImage $selectMainImage)
312
    {
313
        $offer = $this->load($selectMainImage->getItemId());
314
        $offer->selectMainImage($selectMainImage->getImage());
315
        $this->offerRepository->save($offer);
316
    }
317
318
    /**
319
     * Handle the update of description on a place.
320
     * @param AbstractUpdateDescription $updateDescription
321
     */
322
    public function handleUpdateDescription(AbstractUpdateDescription $updateDescription)
323
    {
324
        $offer = $this->load($updateDescription->getItemId());
325
326
        $offer->updateDescription(
327
            $updateDescription->getDescription()
328
        );
329
330
        $this->offerRepository->save($offer);
331
332
    }
333
334
    /**
335
     * Handle the update of typical age range on a place.
336
     * @param AbstractUpdateTypicalAgeRange $updateTypicalAgeRange
337
     */
338
    public function handleUpdateTypicalAgeRange(AbstractUpdateTypicalAgeRange $updateTypicalAgeRange)
339
    {
340
        $offer = $this->load($updateTypicalAgeRange->getItemId());
341
342
        $offer->updateTypicalAgeRange(
343
            $updateTypicalAgeRange->getTypicalAgeRange()
344
        );
345
346
        $this->offerRepository->save($offer);
347
348
    }
349
350
    /**
351
     * Handle the deletion of typical age range on a place.
352
     * @param AbstractDeleteTypicalAgeRange $deleteTypicalAgeRange
353
     */
354
    public function handleDeleteTypicalAgeRange(AbstractDeleteTypicalAgeRange $deleteTypicalAgeRange)
355
    {
356
        $offer = $this->load($deleteTypicalAgeRange->getItemId());
357
358
        $offer->deleteTypicalAgeRange();
359
360
        $this->offerRepository->save($offer);
361
362
    }
363
364
    /**
365
     * Handle an update command to update organizer of a place.
366
     * @param AbstractUpdateOrganizer $updateOrganizer
367
     */
368
    public function handleUpdateOrganizer(AbstractUpdateOrganizer $updateOrganizer)
369
    {
370
        $offer = $this->load($updateOrganizer->getItemId());
371
        $this->loadOrganizer($updateOrganizer->getOrganizerId());
372
373
        $offer->updateOrganizer(
374
            $updateOrganizer->getOrganizerId()
375
        );
376
377
        $this->offerRepository->save($offer);
378
    }
379
380
    /**
381
     * Handle an update command to delete the organizer.
382
     * @param AbstractDeleteOrganizer $deleteOrganizer
383
     */
384
    public function handleDeleteOrganizer(AbstractDeleteOrganizer $deleteOrganizer)
385
    {
386
        $offer = $this->load($deleteOrganizer->getItemId());
387
388
        $offer->deleteOrganizer(
389
            $deleteOrganizer->getOrganizerId()
390
        );
391
392
        $this->offerRepository->save($offer);
393
    }
394
395
    /**
396
     * Handle an update command to updated the contact point.
397
     * @param AbstractUpdateContactPoint $updateContactPoint
398
     */
399
    public function handleUpdateContactPoint(AbstractUpdateContactPoint $updateContactPoint)
400
    {
401
        $offer = $this->load($updateContactPoint->getId());
402
403
        $offer->updateContactPoint(
404
            $updateContactPoint->getContactPoint()
405
        );
406
407
        $this->offerRepository->save($offer);
408
409
    }
410
411
    /**
412
     * Handle an update command to updated the booking info.
413
     * @param AbstractUpdateBookingInfo $updateBookingInfo
414
     */
415
    public function handleUpdateBookingInfo(AbstractUpdateBookingInfo $updateBookingInfo)
416
    {
417
        $offer = $this->load($updateBookingInfo->getItemId());
418
419
        $offer->updateBookingInfo(
420
            $updateBookingInfo->getBookingInfo()
421
        );
422
423
        $this->offerRepository->save($offer);
424
    }
425
426
    /**
427
     * @param AbstractUpdatePriceInfo $updatePriceInfo
428
     */
429
    private function handleUpdatePriceInfo(AbstractUpdatePriceInfo $updatePriceInfo)
430
    {
431
        $offer = $this->load($updatePriceInfo->getItemId());
432
433
        $offer->updatePriceInfo(
434
            $updatePriceInfo->getPriceInfo()
435
        );
436
437
        $this->offerRepository->save($offer);
438
    }
439
440
    /**
441
     * @param AbstractDeleteOffer $deleteOffer
442
     */
443
    private function handleDeleteOffer(AbstractDeleteOffer $deleteOffer)
444
    {
445
        $offer = $this->load($deleteOffer->getItemId());
446
        $offer->delete();
447
        $this->offerRepository->save($offer);
448
    }
449
450
    /**
451
     * @param AbstractPublish $publish
452
     */
453
    private function handlePublish(AbstractPublish $publish)
454
    {
455
        $offer = $this->load($publish->getItemId());
456
        $offer->publish($publish->getPublicationDate());
457
        $this->offerRepository->save($offer);
458
    }
459
460
    /**
461
     * @param AbstractApprove $approve
462
     */
463
    private function handleApprove(AbstractApprove $approve)
464
    {
465
        $offer = $this->load($approve->getItemId());
466
        $offer->approve();
467
        $this->offerRepository->save($offer);
468
    }
469
470
    /**
471
     * @param AbstractReject $reject
472
     */
473
    private function handleReject(AbstractReject $reject)
474
    {
475
        $offer = $this->load($reject->getItemId());
476
        $offer->reject($reject->getReason());
477
        $this->offerRepository->save($offer);
478
    }
479
480
    /**
481
     * @param AbstractFlagAsDuplicate $flagAsDuplicate
482
     */
483
    private function handleFlagAsDuplicate(AbstractFlagAsDuplicate $flagAsDuplicate)
484
    {
485
        $offer = $this->load($flagAsDuplicate->getItemId());
486
        $offer->flagAsDuplicate();
487
        $this->offerRepository->save($offer);
488
    }
489
490
    /**
491
     * @param AbstractFlagAsInappropriate $flagAsInappropriate
492
     */
493
    private function handleFlagAsInappropriate(AbstractFlagAsInappropriate $flagAsInappropriate)
494
    {
495
        $offer = $this->load($flagAsInappropriate->getItemId());
496
        $offer->flagAsInappropriate();
497
        $this->offerRepository->save($offer);
498
    }
499
500
    /**
501
     * Makes it easier to type-hint to Offer.
502
     *
503
     * @param string $id
504
     * @return Offer
505
     */
506
    private function load($id)
507
    {
508
        return $this->offerRepository->load($id);
509
    }
510
511
    /**
512
     * Makes it easier to type-hint to Organizer.
513
     *
514
     * @param string $id
515
     * @return Organizer
516
     */
517
    private function loadOrganizer($id)
518
    {
519
        return $this->organizerRepository->load($id);
520
521
    }
522
}
523