Completed
Pull Request — master (#475)
by
unknown
02:00
created

DissectEventAdapter::getRRule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace HDNET\Calendarize\Ical;
6
7
use HDNET\Calendarize\Domain\Model\ConfigurationInterface;
8
use HDNET\Calendarize\Utility\DateTimeUtility;
9
10
/**
11
 * Class DissectEvent.
12
 */
13
class DissectEventAdapter implements ICalEvent
14
{
15
    /**
16
     * @var \JMBTechnologyLimited\ICalDissect\ICalEvent
17
     */
18
    protected $event;
19
20
    /**
21
     * DissectEvent constructor.
22
     *
23
     * @param \JMBTechnologyLimited\ICalDissect\ICalEvent $event
24
     */
25
    public function __construct(\JMBTechnologyLimited\ICalDissect\ICalEvent $event)
26
    {
27
        $this->event = $event;
28
    }
29
30
    /**
31
     * @return array
32
     */
33
    public function getRawData(): array
34
    {
35
        return $this->event->getRaw();
36
    }
37
38
    /**
39
     * @return \JMBTechnologyLimited\ICalDissect\ICalEvent
40
     */
41
    public function getEvent(): \JMBTechnologyLimited\ICalDissect\ICalEvent
42
    {
43
        return $this->event;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getUid(): string
50
    {
51
        return $this->event->getUid();
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getTitle(): ?string
58
    {
59
        return $this->event->getSummary();
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getDescription(): ?string
66
    {
67
        return $this->event->getDescription();
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getLocation(): ?string
74
    {
75
        return $this->event->getLocation();
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function getOrganizer(): ?string
82
    {
83
        return $this->getEvent()->getRaw('ORGANIZER')[0] ?? null;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function getStartDate(): ?\DateTime
90
    {
91
        $startDate = $this->event->getStart();
92
        if (empty($startDate)) {
93
            return null;
94
        }
95
        if ($this->isAllDay()) {
96
            // Allows the date to be reparsed with the right timezone.
97
            $startDate = $startDate->format('Y-m-d');
98
        }
99
100
        return DateTimeUtility::getDayStart($startDate);
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function getStartTime(): int
107
    {
108
        if ($this->isAllDay() || empty($this->event->getStart())) {
109
            return self::ALLDAY_START_TIME;
110
        }
111
112
        return DateTimeUtility::getNormalizedDaySecondsOfDateTime($this->event->getStart());
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function getEndDate(): ?\DateTime
119
    {
120
        $endDate = $this->event->getEnd();
121
        if (empty($endDate)) {
122
            return null;
123
        }
124
        if ($this->isAllDay()) {
125
            // Converts the exclusive enddate to inclusive
126
            $endDate = (clone $endDate)->sub(new \DateInterval('P1D'));
127
            // Allows the date to be reparsed with the right timezone.
128
            $endDate = $endDate->format('Y-m-d');
129
        }
130
131
        return DateTimeUtility::getDayStart($endDate);
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function getEndTime(): int
138
    {
139
        if ($this->isAllDay() || empty($this->event->getEnd())) {
140
            return self::ALLDAY_END_TIME;
141
        }
142
143
        return DateTimeUtility::getNormalizedDaySecondsOfDateTime($this->event->getEnd());
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function isAllDay(): bool
150
    {
151
        // The ICalDissect\ICalEvent does not provide information,
152
        // if DTSTART is a DATE or DATE-TIME
153
        // If no time was given, the class sets following values
154
        //  starTime to 00:00:00 = 0
155
        //  endTime to 23:59:59 = (24h*60m*60s - 1) = 86399
156
        // So we check for these values.
157
        // Note: By spec (RFC) DTEND is irrelevant for allday events,
158
        //       so this may produce invalid results.
159
        $start = true;
160
        $end = true;
161
        if (!empty($this->event->getStart())) {
162
            $start = 0 === DateTimeUtility::getDaySecondsOfDateTime($this->event->getStart());
163
        }
164
        if (!empty($this->event->getEnd())) {
165
            $end = 86399 === DateTimeUtility::getDaySecondsOfDateTime($this->event->getEnd());
166
        }
167
168
        return $start && $end;
169
    }
170
171
    /**
172
     * {@inheritdoc}
173
     */
174
    public function isOpenEndTime(): bool
175
    {
176
        return false;
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182
    public function getState(): string
183
    {
184
        if ($this->event->isDeleted()) {
185
            return ConfigurationInterface::STATE_CANCELED;
186
        }
187
188
        return ConfigurationInterface::STATE_DEFAULT;
189
    }
190
191
    /**
192
     * {@inheritdoc}
193
     */
194
    public function getRRule(): array
195
    {
196
        return $this->event->getRRule() ?? [];
197
    }
198
}
199