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

DefaultOfferEditingService   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 331
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

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