Completed
Push — master ( 65255d...9dfb0b )
by Luc
07:57 queued 02:17
created

DefaultOfferEditingService::updateCalendar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
1
<?php
2
3
namespace CultuurNet\UDB3\Offer;
4
5
use Broadway\CommandHandling\CommandBusInterface;
6
use Broadway\UuidGenerator\UuidGeneratorInterface;
7
use CultuurNet\UDB3\BookingInfo;
8
use CultuurNet\UDB3\Calendar;
9
use CultuurNet\UDB3\ContactPoint;
10
use CultuurNet\UDB3\Description;
11
use CultuurNet\UDB3\EntityNotFoundException;
12
use CultuurNet\UDB3\Event\ReadModel\DocumentGoneException;
13
use CultuurNet\UDB3\Event\ReadModel\DocumentRepositoryInterface;
14
use CultuurNet\UDB3\Label;
15
use CultuurNet\UDB3\Label\LabelServiceInterface;
16
use CultuurNet\UDB3\Label\ValueObjects\LabelName;
17
use CultuurNet\UDB3\Language;
18
use CultuurNet\UDB3\Media\Image;
19
use CultuurNet\UDB3\Offer\Commands\OfferCommandFactoryInterface;
20
use CultuurNet\UDB3\PriceInfo\PriceInfo;
21
use ValueObjects\StringLiteral\StringLiteral;
22
23
class DefaultOfferEditingService implements OfferEditingServiceInterface
24
{
25
    /**
26
     * @var CommandBusInterface
27
     */
28
    protected $commandBus;
29
30
    /**
31
     * @var UuidGeneratorInterface
32
     */
33
    protected $uuidGenerator;
34
35
    /**
36
     * @var DocumentRepositoryInterface
37
     */
38
    protected $readRepository;
39
40
    /**
41
     * @var OfferCommandFactoryInterface
42
     */
43
    protected $commandFactory;
44
45
    /**
46
     * @var LabelServiceInterface
47
     */
48
    private $labelService;
49
50
    /**
51
     * @var \DateTimeImmutable|null
52
     */
53
    protected $publicationDate;
54
55
    /**
56
     * @param CommandBusInterface $commandBus
57
     * @param UuidGeneratorInterface $uuidGenerator
58
     * @param DocumentRepositoryInterface $readRepository
59
     * @param OfferCommandFactoryInterface $commandFactory
60
     * @param LabelServiceInterface $labelService
61
     */
62
    public function __construct(
63
        CommandBusInterface $commandBus,
64
        UuidGeneratorInterface $uuidGenerator,
65
        DocumentRepositoryInterface $readRepository,
66
        OfferCommandFactoryInterface $commandFactory,
67
        LabelServiceInterface $labelService
68
    ) {
69
        $this->commandBus = $commandBus;
70
        $this->uuidGenerator = $uuidGenerator;
71
        $this->readRepository = $readRepository;
72
        $this->commandFactory = $commandFactory;
73
        $this->labelService = $labelService;
74
        $this->publicationDate = null;
75
    }
76
77
    /**
78
     * @param \DateTimeImmutable $publicationDate
79
     * @return static
80
     */
81
    public function withFixedPublicationDateForNewOffers(
82
        \DateTimeImmutable $publicationDate
83
    ) {
84
        $c = clone $this;
85
        $c->publicationDate = $publicationDate;
86
        return $c;
87
    }
88
89
    /**
90
     * @param $id
91
     * @param Label $label
92
     * @return string
93
     */
94
    public function addLabel($id, Label $label)
95
    {
96
        $this->guardId($id);
97
98
        $this->labelService->createLabelAggregateIfNew(
99
            new LabelName((string) $label),
100
            $label->isVisible()
101
        );
102
103
        return $this->commandBus->dispatch(
104
            $this->commandFactory->createAddLabelCommand(
105
                $id,
106
                $label
107
            )
108
        );
109
    }
110
111
    /**
112
     * @param $id
113
     * @param Label $label
114
     * @return string
115
     */
116
    public function removeLabel($id, Label $label)
117
    {
118
        $this->guardId($id);
119
120
        return $this->commandBus->dispatch(
121
            $this->commandFactory->createRemoveLabelCommand(
122
                $id,
123
                $label
124
            )
125
        );
126
    }
127
128
    /**
129
     * @param $id
130
     * @param Language $language
131
     * @param StringLiteral $title
132
     * @return string
133
     */
134
    public function updateTitle($id, Language $language, StringLiteral $title)
135
    {
136
        $this->guardId($id);
137
138
        return $this->commandBus->dispatch(
139
            $this->commandFactory->createUpdateTitleCommand(
140
                $id,
141
                $language,
142
                $title
143
            )
144
        );
145
    }
146
147
    /**
148
     * @param $id
149
     * @param Language $language
150
     * @param Description $description
151
     * @return string
152
     */
153
    public function updateDescription($id, Language $language, Description $description)
154
    {
155
        $this->guardId($id);
156
157
        return $this->commandBus->dispatch(
158
            $this->commandFactory->createUpdateDescriptionCommand(
159
                $id,
160
                $language,
161
                $description
162
            )
163
        );
164
    }
165
166
    /**
167
     * @inheritdoc
168
     */
169
    public function updateCalendar($id, Calendar $calendar)
170
    {
171
        $this->guardId($id);
172
173
        return $this->commandBus->dispatch(
174
            $this->commandFactory->createUpdateCalendarCommand(
175
                $id,
176
                $calendar
177
            )
178
        );
179
    }
180
181
    /**
182
     * @param string $id
183
     * @param Image $image
184
     * @return string
185
     */
186
    public function addImage($id, Image $image)
187
    {
188
        $this->guardId($id);
189
190
        return $this->commandBus->dispatch(
191
            $this->commandFactory->createAddImageCommand($id, $image)
192
        );
193
    }
194
195
    /**
196
     * @param string $id
197
     * @param Image $image
198
     * @param StringLiteral $description
199
     * @param StringLiteral $copyrightHolder
200
     * @return string
201
     */
202
    public function updateImage(
203
        $id,
204
        Image $image,
205
        StringLiteral $description,
206
        StringLiteral $copyrightHolder
207
    ) {
208
        $this->guardId($id);
209
210
        return $this->commandBus->dispatch(
211
            $this->commandFactory->createUpdateImageCommand(
212
                $id,
213
                $image->getMediaObjectId(),
214
                $description,
215
                $copyrightHolder
216
            )
217
        );
218
    }
219
220
    /**
221
     * @param $id
222
     *  Id of the offer to remove the image from.
223
     *
224
     * @param Image $image
225
     *  The image that should be removed.
226
     *
227
     * @return string
228
     */
229
    public function removeImage($id, Image $image)
230
    {
231
        $this->guardId($id);
232
233
        return $this->commandBus->dispatch(
234
            $this->commandFactory->createRemoveImageCommand($id, $image)
235
        );
236
    }
237
238
    /**
239
     * @param $id
240
     * @param Image $image
241
     * @return string
242
     */
243
    public function selectMainImage($id, Image $image)
244
    {
245
        $this->guardId($id);
246
247
        return $this->commandBus->dispatch(
248
            $this->commandFactory->createSelectMainImageCommand($id, $image)
249
        );
250
    }
251
252
    /**
253
     * @param string $id
254
     * @param AgeRange $ageRange
255
     * @return string
256
     */
257
    public function updateTypicalAgeRange($id, AgeRange $ageRange)
258
    {
259
        $this->guardId($id);
260
261
        return $this->commandBus->dispatch(
262
            $this->commandFactory->createUpdateTypicalAgeRangeCommand($id, $ageRange)
263
        );
264
    }
265
266
    /**
267
     * @param string $id
268
     * @return string
269
     */
270
    public function deleteTypicalAgeRange($id)
271
    {
272
        $this->guardId($id);
273
274
        return $this->commandBus->dispatch(
275
            $this->commandFactory->createDeleteTypicalAgeRangeCommand($id)
276
        );
277
278
    }
279
280
    /**
281
     * @param string $id
282
     * @param string $organizerId
283
     * @return string
284
     */
285
    public function updateOrganizer($id, $organizerId)
286
    {
287
        $this->guardId($id);
288
289
        return $this->commandBus->dispatch(
290
            $this->commandFactory->createUpdateOrganizerCommand($id, $organizerId)
291
        );
292
    }
293
294
    /**
295
     * @param string $id
296
     * @param string $organizerId
297
     * @return string
298
     */
299
    public function deleteOrganizer($id, $organizerId)
300
    {
301
        $this->guardId($id);
302
303
        return $this->commandBus->dispatch(
304
            $this->commandFactory->createDeleteOrganizerCommand($id, $organizerId)
305
        );
306
    }
307
308
    /**
309
     * @param string $id
310
     * @param ContactPoint $contactPoint
311
     * @return string
312
     */
313
    public function updateContactPoint($id, ContactPoint $contactPoint)
314
    {
315
        $this->guardId($id);
316
317
        return $this->commandBus->dispatch(
318
            $this->commandFactory->createUpdateContactPointCommand($id, $contactPoint)
319
        );
320
321
    }
322
323
    /**
324
     * @param string $id
325
     * @param BookingInfo $bookingInfo
326
     * @return string
327
     */
328
    public function updateBookingInfo($id, BookingInfo $bookingInfo)
329
    {
330
        $this->guardId($id);
331
332
        return $this->commandBus->dispatch(
333
            $this->commandFactory->createUpdateBookingInfoCommand($id, $bookingInfo)
334
        );
335
    }
336
337
    /**
338
     * @param $id
339
     * @param PriceInfo $priceInfo
340
     */
341
    public function updatePriceInfo($id, PriceInfo $priceInfo)
342
    {
343
        $this->guardId($id);
344
345
        return $this->commandBus->dispatch(
346
            $this->commandFactory->createUpdatePriceInfoCommand($id, $priceInfo)
347
        );
348
    }
349
350
    /**
351
     * @param string $id
352
     * @return string
353
     */
354
    public function delete($id)
355
    {
356
        return $this->commandBus->dispatch(
357
            $this->commandFactory->createDeleteOfferCommand($id)
358
        );
359
    }
360
361
    /**
362
     * @param string $id
363
     *
364
     * @throws EntityNotFoundException|DocumentGoneException
365
     */
366
    public function guardId($id)
367
    {
368
        $offer = $this->readRepository->get($id);
369
370
        if (is_null($offer)) {
371
            throw new EntityNotFoundException(
372
                sprintf('Offer with id: %s not found.', $id)
373
            );
374
        }
375
    }
376
}
377