-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathEventFactory.php
More file actions
299 lines (253 loc) · 9.91 KB
/
EventFactory.php
File metadata and controls
299 lines (253 loc) · 9.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
/*
* This file is part of the eluceo/iCal package.
*
* (c) 2024 Markus Poerschke <markus@poerschke.nrw>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Eluceo\iCal\Presentation\Factory;
use DateInterval;
use Eluceo\iCal\Domain\Collection\Events;
use Eluceo\iCal\Domain\Entity\Event;
use Eluceo\iCal\Domain\Enum\EventStatus;
use Eluceo\iCal\Domain\Enum\Transparency;
use Eluceo\iCal\Domain\ValueObject\Alarm;
use Eluceo\iCal\Domain\ValueObject\Attachment;
use Eluceo\iCal\Domain\ValueObject\MultiDay;
use Eluceo\iCal\Domain\ValueObject\Occurrence;
use Eluceo\iCal\Domain\ValueObject\Organizer;
use Eluceo\iCal\Domain\ValueObject\SingleDay;
use Eluceo\iCal\Domain\ValueObject\TimeSpan;
use Eluceo\iCal\Presentation\Component;
use Eluceo\iCal\Presentation\Component\Property;
use Eluceo\iCal\Presentation\Component\Property\Parameter;
use Eluceo\iCal\Presentation\Component\Property\Value\AppleLocationGeoValue;
use Eluceo\iCal\Presentation\Component\Property\Value\BinaryValue;
use Eluceo\iCal\Presentation\Component\Property\Value\DateTimeValue;
use Eluceo\iCal\Presentation\Component\Property\Value\DateValue;
use Eluceo\iCal\Presentation\Component\Property\Value\GeoValue;
use Eluceo\iCal\Presentation\Component\Property\Value\IntegerValue;
use Eluceo\iCal\Presentation\Component\Property\Value\ListValue;
use Eluceo\iCal\Presentation\Component\Property\Value\TextValue;
use Eluceo\iCal\Presentation\Component\Property\Value\UriValue;
use Generator;
use UnexpectedValueException;
/**
* @SuppressWarnings("CouplingBetweenObjects")
*/
class EventFactory
{
private AlarmFactory $alarmFactory;
private DateTimeFactory $dateTimeFactory;
private AttendeeFactory $attendeeFactory;
public function __construct(AlarmFactory $alarmFactory = null, DateTimeFactory $dateTimeFactory = null, AttendeeFactory $attendeeFactory = null)
{
$this->alarmFactory = $alarmFactory ?? new AlarmFactory();
$this->dateTimeFactory = $dateTimeFactory ?? new DateTimeFactory();
$this->attendeeFactory = $attendeeFactory ?? new AttendeeFactory();
}
/**
* @return Generator<Component>
*/
final public function createComponents(Events $events): Generator
{
foreach ($events as $event) {
yield $this->createComponent($event);
}
}
public function createComponent(Event $event): Component
{
return new Component(
'VEVENT',
iterator_to_array($this->getProperties($event), false),
iterator_to_array($this->getComponents($event), false)
);
}
/**
* @return Generator<Property>
*
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
protected function getProperties(Event $event): Generator
{
yield new Property('UID', new TextValue((string) $event->getUniqueIdentifier()));
yield new Property('DTSTAMP', new DateTimeValue($event->getTouchedAt()));
if ($event->hasLastModified()) {
yield new Property('LAST-MODIFIED', new DateTimeValue($event->getLastModified()));
}
if ($event->hasSummary()) {
yield new Property('SUMMARY', new TextValue($event->getSummary()));
}
if ($event->hasDescription()) {
yield new Property('DESCRIPTION', new TextValue($event->getDescription()));
}
if ($event->hasUrl()) {
yield new Property('URL', new TextValue($event->getUrl()->getUri()));
}
if ($event->hasOccurrence()) {
yield from $this->getOccurrenceProperties($event->getOccurrence());
}
if ($event->hasLocation()) {
yield from $this->getLocationProperties($event);
}
if ($event->hasOrganizer()) {
yield $this->getOrganizerProperty($event->getOrganizer());
}
if ($event->hasAttendee()) {
foreach ($event->getAttendees() as $attendee) {
yield $this->attendeeFactory->createProperty($attendee);
}
}
if ($event->hasCategories()) {
yield $this->getCategoryProperties($event);
}
if ($event->hasStatus()) {
yield new Property('STATUS', $this->getEventStatusTextValue($event->getStatus()));
}
if ($event->hasTransparency()) {
yield new Property('TRANSP', $this->getTransparencyTextValue($event->getTransparency()));
}
foreach ($event->getAttachments() as $attachment) {
yield from $this->getAttachmentProperties($attachment);
}
}
/**
* @return Generator<Component>
*/
protected function getComponents(Event $event): Generator
{
yield from array_map(
fn (Alarm $alarm) => $this->alarmFactory->createComponent($alarm),
$event->getAlarms()
);
}
/**
* @return Generator<Property>
*/
private function getOccurrenceProperties(Occurrence $occurrence): Generator
{
if ($occurrence instanceof SingleDay) {
yield new Property(
'DTSTART',
new DateValue($occurrence->getDate()),
[
new Parameter('VALUE', new TextValue('DATE')),
]
);
}
if ($occurrence instanceof MultiDay) {
yield new Property(
'DTSTART',
new DateValue($occurrence->getFirstDay()),
[
new Parameter('VALUE', new TextValue('DATE')),
]
);
yield new Property(
'DTEND',
new DateValue($occurrence->getLastDay()->add(new DateInterval('P1D'))),
[
new Parameter('VALUE', new TextValue('DATE')),
]
);
}
if ($occurrence instanceof TimeSpan) {
yield $this->dateTimeFactory->createProperty('DTSTART', $occurrence->getBegin());
yield $this->dateTimeFactory->createProperty('DTEND', $occurrence->getEnd());
}
}
/**
* @return Generator<Property>
*/
private function getLocationProperties(Event $event): Generator
{
yield new Property('LOCATION', new TextValue((string) $event->getLocation()));
if ($event->getLocation()->hasGeographicalPosition()) {
yield new Property('GEO', new GeoValue($event->getLocation()->getGeographicPosition()));
yield new Property(
'X-APPLE-STRUCTURED-LOCATION',
new AppleLocationGeoValue($event->getLocation()->getGeographicPosition()),
[
new Parameter('VALUE', new TextValue('URI')),
new Parameter('X-ADDRESS', new TextValue((string) $event->getLocation())),
new Parameter('X-APPLE-RADIUS', new IntegerValue(49)),
new Parameter('X-TITLE', new TextValue($event->getLocation()->getTitle())),
]
);
}
}
/**
* @return Generator<Property>
*/
private function getAttachmentProperties(Attachment $attachment): Generator
{
$parameters = [];
if ($attachment->hasMimeType()) {
$parameters[] = new Parameter('FMTTYPE', new TextValue($attachment->getMimeType()));
}
if ($attachment->hasUri()) {
yield new Property(
'ATTACH',
new UriValue($attachment->getUri()),
$parameters
);
}
if ($attachment->hasBinaryContent()) {
$parameters[] = new Parameter('ENCODING', new TextValue('BASE64'));
$parameters[] = new Parameter('VALUE', new TextValue('BINARY'));
yield new Property(
'ATTACH',
new BinaryValue($attachment->getBinaryContent()),
$parameters
);
}
}
private function getOrganizerProperty(Organizer $organizer): Property
{
$parameters = [];
if ($organizer->hasDisplayName()) {
$parameters[] = new Parameter('CN', new TextValue($organizer->getDisplayName()));
}
if ($organizer->hasDirectoryEntry()) {
$parameters[] = new Parameter('DIR', new UriValue($organizer->getDirectoryEntry()));
}
if ($organizer->isSentInBehalfOf()) {
$parameters[] = new Parameter('SENT-BY', new UriValue($organizer->getSentBy()->toUri()));
}
return new Property('ORGANIZER', new UriValue($organizer->getEmailAddress()->toUri()), $parameters);
}
private function getCategoryProperties(Event $event): Property
{
$categories = [];
foreach ($event->getCategories() as $category) {
$categories[] = new TextValue((string) $category);
}
return new Property('CATEGORIES', new ListValue($categories));
}
private function getEventStatusTextValue(EventStatus $status): TextValue
{
if ($status === EventStatus::CANCELLED()) {
return new TextValue('CANCELLED');
}
if ($status === EventStatus::CONFIRMED()) {
return new TextValue('CONFIRMED');
}
if ($status === EventStatus::TENTATIVE()) {
return new TextValue('TENTATIVE');
}
throw new UnexpectedValueException(sprintf('The enum %s resulted into an unknown status type value that is not yet implemented.', EventStatus::class));
}
public function getTransparencyTextValue(Transparency $transparency): TextValue
{
if ($transparency === Transparency::OPAQUE()) {
return new TextValue('OPAQUE');
}
if ($transparency === Transparency::TRANSPARENT()) {
return new TextValue('TRANSPARENT');
}
throw new UnexpectedValueException(sprintf('The enum %s resulted into an unknown transparency type value that is not yet implemented.', Transparency::class));
}
}