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

VObjectEventAdapter::getRRule()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
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
use Sabre\VObject\Component\VEvent;
10
11
class VObjectEventAdapter implements ICalEvent
12
{
13
    /**
14
     * @var VEvent
15
     */
16
    protected $event;
17
18
    /**
19
     * VObjectEvent constructor.
20
     *
21
     * @param VEvent $event
22
     */
23
    public function __construct(VEvent $event)
24
    {
25
        $this->event = $event;
26
    }
27
28
    /**
29
     * @return VEvent
30
     */
31
    public function getEvent(): VEvent
32
    {
33
        return $this->event;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getRawData(): array
40
    {
41
        return $this->event->children();
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getUid(): string
48
    {
49
        return $this->event->UID->getValue();
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getTitle(): ?string
56
    {
57
        if (!isset($this->event->SUMMARY)) {
58
            return null;
59
        }
60
61
        return $this->event->SUMMARY->getValue();
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getDescription(): ?string
68
    {
69
        if (!isset($this->event->DESCRIPTION)) {
70
            return null;
71
        }
72
73
        return $this->event->DESCRIPTION->getValue();
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function getLocation(): ?string
80
    {
81
        if (!isset($this->event->LOCATION)) {
82
            return null;
83
        }
84
85
        return $this->event->LOCATION->getValue();
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function getOrganizer(): ?string
92
    {
93
        if (!isset($this->event->ORGANIZER)) {
94
            return null;
95
        }
96
97
        return $this->event->ORGANIZER->getValue();
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function getStartDate(): ?\DateTime
104
    {
105
        if (!isset($this->event->DTSTART)) {
106
            return null;
107
        }
108
        $start = $this->event->DTSTART->getDateTime();
109
110
        if ($this->isAllDay()) {
111
            // Allows the date to be reparsed with the right timezone.
112
            $start = $start->format('Y-m-d');
113
        }
114
115
        return DateTimeUtility::getDayStart($start);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function getEndDate(): ?\DateTime
122
    {
123
        $end = $this->getEndDateTime();
124
        if (empty($end)) {
125
            return null;
126
        }
127
128
        if ($this->isAllDay()) {
129
            // Converts the exclusive enddate to inclusive
130
            $end = (clone $end)->sub(new \DateInterval('P1D'));
131
            // Allows the date to be reparsed with the right timezone.
132
            $end = $end->format('Y-m-d');
133
        }
134
135
        return DateTimeUtility::getDayStart($end);
136
    }
137
138
    /**
139
     * Gets the end datetime, determines it with the duration or returns null.
140
     *
141
     * @return \DateTimeImmutable|null
142
     */
143
    protected function getEndDateTime(): ?\DateTimeImmutable
144
    {
145
        if (isset($this->event->DTEND)) {
146
            /** @var \Sabre\VObject\Property\ICalendar\DateTime $dtEnd */
147
            $dtEnd = $this->event->DTEND;
148
149
            return $dtEnd->getDateTime();
150
        }
151
        if (isset($this->event->DURATION)) {
152
            /** @var \Sabre\VObject\Property\ICalendar\DateTime $dtStart */
153
            $dtStart = $this->event->DTSTART;
154
            $duration = $this->event->DURATION->getDateInterval();
155
156
            return (clone $dtStart)->getDateTime()->add($duration);
157
        }
158
159
        return null;
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    public function getStartTime(): int
166
    {
167
        if ($this->isAllDay() || !isset($this->event->DTSTART)) {
168
            return self::ALLDAY_START_TIME;
169
        }
170
171
        /** @var \Sabre\VObject\Property\ICalendar\DateTime $start */
172
        $start = $this->event->DTSTART;
173
174
        return DateTimeUtility::getNormalizedDaySecondsOfDateTime($start->getDateTime());
0 ignored issues
show
Bug introduced by
It seems like $start->getDateTime() can be null; however, getNormalizedDaySecondsOfDateTime() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180
    public function getEndTime(): int
181
    {
182
        $end = $this->getEndDateTime();
183
        if ($this->isAllDay() || empty($end)) {
184
            return self::ALLDAY_END_TIME;
185
        }
186
187
        return DateTimeUtility::getNormalizedDaySecondsOfDateTime($end);
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193
    public function isAllDay(): bool
194
    {
195
        if (!isset($this->event->DTSTART)) {
196
            return true;
197
        }
198
        /** @var \Sabre\VObject\Property\ICalendar\DateTime $start */
199
        $start = $this->event->DTSTART;
200
201
        return !$start->hasTime();
202
    }
203
204
    /**
205
     * {@inheritdoc}
206
     */
207
    public function isOpenEndTime(): bool
208
    {
209
        return false;
210
    }
211
212
    /**
213
     * {@inheritdoc}
214
     */
215
    public function getState(): string
216
    {
217
        if (isset($this->event->STATUS)) {
218
            $status = $this->event->STATUS->getValue();
219
            if ('CANCELLED' === $status) {
220
                return ConfigurationInterface::STATE_CANCELED;
221
            }
222
        }
223
224
        return ConfigurationInterface::STATE_DEFAULT;
225
    }
226
227
    /**
228
     * {@inheritdoc}
229
     */
230
    public function getRRule(): array
231
    {
232
        if (!isset($this->event->RRULE)) {
233
            return [];
234
        }
235
        $rrule = $this->event->RRULE->getValue();
236
        if (\is_string($rrule)) {
237
            $rrule = \Sabre\VObject\Property\ICalendar\Recur::stringToArray($rrule);
238
        }
239
240
        return $rrule;
241
    }
242
}
243