Completed
Pull Request — master (#314)
by Luc
04:56
created

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

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