Completed
Pull Request — master (#321)
by
unknown
04:34
created

DefaultOfferEditingService   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 339
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
wmc 20
lcom 2
cbo 8
dl 0
loc 339
rs 10
c 0
b 0
f 0

19 Methods

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