1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\Event; |
4
|
|
|
|
5
|
|
|
use Broadway\CommandHandling\CommandBusInterface; |
6
|
|
|
use Broadway\Repository\AggregateNotFoundException; |
7
|
|
|
use Broadway\Repository\RepositoryInterface; |
8
|
|
|
use Broadway\UuidGenerator\UuidGeneratorInterface; |
9
|
|
|
use CultuurNet\UDB3\CalendarInterface; |
10
|
|
|
use CultuurNet\UDB3\Event\Commands\UpdateAudience; |
11
|
|
|
use CultuurNet\UDB3\Event\Commands\UpdateLocation; |
12
|
|
|
use CultuurNet\UDB3\Event\Commands\UpdateMajorInfo; |
13
|
|
|
use CultuurNet\UDB3\Event\ReadModel\DocumentRepositoryInterface; |
14
|
|
|
use CultuurNet\UDB3\Event\ValueObjects\Audience; |
15
|
|
|
use CultuurNet\UDB3\Label\LabelServiceInterface; |
16
|
|
|
use CultuurNet\UDB3\Language; |
17
|
|
|
use CultuurNet\UDB3\Location\Location; |
18
|
|
|
use CultuurNet\UDB3\Location\LocationId; |
19
|
|
|
use CultuurNet\UDB3\Offer\Commands\OfferCommandFactoryInterface; |
20
|
|
|
use CultuurNet\UDB3\Offer\DefaultOfferEditingService; |
21
|
|
|
use CultuurNet\UDB3\Theme; |
22
|
|
|
use CultuurNet\UDB3\Title; |
23
|
|
|
|
24
|
|
|
class DefaultEventEditingService extends DefaultOfferEditingService implements EventEditingServiceInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var EventServiceInterface |
28
|
|
|
*/ |
29
|
|
|
protected $eventService; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var RepositoryInterface |
33
|
|
|
*/ |
34
|
|
|
protected $writeRepository; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param EventServiceInterface $eventService |
38
|
|
|
* @param CommandBusInterface $commandBus |
39
|
|
|
* @param UuidGeneratorInterface $uuidGenerator |
40
|
|
|
* @param DocumentRepositoryInterface $readRepository |
41
|
|
|
* @param OfferCommandFactoryInterface $commandFactory |
42
|
|
|
* @param RepositoryInterface $writeRepository |
43
|
|
|
* @param LabelServiceInterface $labelService |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
46
|
|
|
EventServiceInterface $eventService, |
47
|
|
|
CommandBusInterface $commandBus, |
48
|
|
|
UuidGeneratorInterface $uuidGenerator, |
49
|
|
|
DocumentRepositoryInterface $readRepository, |
50
|
|
|
OfferCommandFactoryInterface $commandFactory, |
51
|
|
|
RepositoryInterface $writeRepository, |
52
|
|
|
LabelServiceInterface $labelService |
53
|
|
|
) { |
54
|
|
|
parent::__construct( |
55
|
|
|
$commandBus, |
56
|
|
|
$uuidGenerator, |
57
|
|
|
$readRepository, |
58
|
|
|
$commandFactory, |
59
|
|
|
$labelService, |
60
|
|
|
new EventTypeResolver(), |
61
|
|
|
new EventThemeResolver() |
62
|
|
|
); |
63
|
|
|
$this->eventService = $eventService; |
64
|
|
|
$this->writeRepository = $writeRepository; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
public function createEvent( |
|
|
|
|
71
|
|
|
Language $mainLanguage, |
72
|
|
|
Title $title, |
73
|
|
|
EventType $eventType, |
74
|
|
|
Location $location, |
75
|
|
|
CalendarInterface $calendar, |
76
|
|
|
$theme = null |
77
|
|
|
) { |
78
|
|
|
$eventId = $this->uuidGenerator->generate(); |
79
|
|
|
|
80
|
|
|
$event = Event::create( |
81
|
|
|
$eventId, |
82
|
|
|
$mainLanguage, |
83
|
|
|
$title, |
84
|
|
|
$eventType, |
85
|
|
|
$location, |
86
|
|
|
$calendar, |
87
|
|
|
$theme, |
88
|
|
|
$this->publicationDate |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
$this->writeRepository->save($event); |
92
|
|
|
|
93
|
|
|
return $eventId; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @inheritdoc |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
public function createApprovedEvent( |
|
|
|
|
100
|
|
|
Language $mainLanguage, |
101
|
|
|
Title $title, |
102
|
|
|
EventType $eventType, |
103
|
|
|
Location $location, |
104
|
|
|
CalendarInterface $calendar, |
105
|
|
|
Theme $theme = null |
106
|
|
|
) { |
107
|
|
|
$eventId = $this->uuidGenerator->generate(); |
108
|
|
|
|
109
|
|
|
$event = Event::create( |
110
|
|
|
$eventId, |
111
|
|
|
$mainLanguage, |
112
|
|
|
$title, |
113
|
|
|
$eventType, |
114
|
|
|
$location, |
115
|
|
|
$calendar, |
116
|
|
|
$theme |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
$publicationDate = $this->publicationDate ? $this->publicationDate : new \DateTimeImmutable(); |
120
|
|
|
$event->publish($publicationDate); |
121
|
|
|
$event->approve(); |
122
|
|
|
|
123
|
|
|
$this->writeRepository->save($event); |
124
|
|
|
|
125
|
|
|
return $eventId; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @inheritdoc |
130
|
|
|
*/ |
131
|
|
|
public function copyEvent($originalEventId, CalendarInterface $calendar) |
132
|
|
|
{ |
133
|
|
|
if (!is_string($originalEventId)) { |
134
|
|
|
throw new \InvalidArgumentException( |
135
|
|
|
'Expected originalEventId to be a string, received ' . gettype($originalEventId) |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
try { |
140
|
|
|
/** @var Event $event */ |
141
|
|
|
$event = $this->writeRepository->load($originalEventId); |
142
|
|
|
} catch (AggregateNotFoundException $exception) { |
143
|
|
|
throw new \InvalidArgumentException( |
144
|
|
|
'No original event found to copy with id ' . $originalEventId |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$eventId = $this->uuidGenerator->generate(); |
149
|
|
|
|
150
|
|
|
$newEvent = $event->copy($eventId, $calendar); |
151
|
|
|
|
152
|
|
|
$this->writeRepository->save($newEvent); |
153
|
|
|
|
154
|
|
|
return $eventId; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritdoc} |
159
|
|
|
*/ |
160
|
|
View Code Duplication |
public function updateMajorInfo($eventId, Title $title, EventType $eventType, Location $location, CalendarInterface $calendar, $theme = null) |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
$this->guardId($eventId); |
163
|
|
|
|
164
|
|
|
return $this->commandBus->dispatch( |
165
|
|
|
new UpdateMajorInfo($eventId, $title, $eventType, $location, $calendar, $theme) |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @inheritdoc |
171
|
|
|
*/ |
172
|
|
|
public function updateLocation($eventId, LocationId $locationId) |
173
|
|
|
{ |
174
|
|
|
$this->guardId($eventId); |
175
|
|
|
|
176
|
|
|
return $this->commandBus->dispatch( |
177
|
|
|
new UpdateLocation($eventId, $locationId) |
178
|
|
|
); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @inheritdoc |
183
|
|
|
*/ |
184
|
|
|
public function updateAudience($eventId, Audience $audience) |
185
|
|
|
{ |
186
|
|
|
return $this->commandBus->dispatch( |
187
|
|
|
new UpdateAudience($eventId, $audience) |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* {@inheritdoc} |
193
|
|
|
*/ |
194
|
|
|
public function deleteEvent($eventId) |
195
|
|
|
{ |
196
|
|
|
return $this->delete($eventId); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
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.