Completed
Pull Request — master (#443)
by
unknown
03:08
created

VObjectEventAdapter::__construct()   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 1
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
    /**
15
     * @var VEvent
16
     */
17
    protected $event;
18
19
    /**
20
     * VObjectEvent constructor.
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
        return $this->event->SUMMARY->getValue();
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66
    public function getDescription(): ?string
67
    {
68
        if (!isset($this->event->DESCRIPTION)) {
69
            return null;
70
        }
71
72
        return $this->event->DESCRIPTION->getValue();
73
    }
74
75
    /**
76
     * @inheritDoc
77
     */
78
    public function getLocation(): ?string
79
    {
80
        if (!isset($this->event->LOCATION)) {
81
            return null;
82
        }
83
84
        return $this->event->LOCATION->getValue();
85
    }
86
87
    /**
88
     * @inheritDoc
89
     */
90
    public function getOrganizer(): ?string
91
    {
92
        if (!isset($this->event->ORGANIZER)) {
93
            return null;
94
        }
95
96
        return $this->event->ORGANIZER->getValue();
97
    }
98
99
    /**
100
     * @inheritDoc
101
     */
102
    public function getStartDate(): ?\DateTime
103
    {
104
        if (!isset($this->event->DTSTART)) {
105
            return null;
106
        }
107
108
        /** @var \Sabre\VObject\Property\ICalendar\DateTime $start */
109
        $start = $this->event->DTSTART;
110
        return DateTimeUtility::getDayStart($start->getDateTime());
111
    }
112
113
    /**
114
     * @inheritDoc
115
     */
116
    public function getEndDate(): ?\DateTime
117
    {
118
        $end = $this->getEndDateTime();
119
        if (empty($end)) {
120
            return null;
121
        }
122
123
        if ($this->isAllDay()) {
124
            // Converts the exclusive enddate to inclusive
125
            $end = (clone $end)->sub(new \DateInterval('P1D'));
126
        }
127
128
        return DateTimeUtility::getDayStart($end);
0 ignored issues
show
Security Bug introduced by
It seems like $end defined by (clone $end)->sub(new \DateInterval('P1D')) on line 125 can also be of type false; however, HDNET\Calendarize\Utilit...eUtility::getDayStart() does only seem to accept integer|string|object<DateTimeInterface>|null, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
129
    }
130
131
    /**
132
     * Gets the end datetime, determines it with the duration or returns null.
133
     * @return \DateTimeImmutable|null
134
     */
135
    protected function getEndDateTime(): ?\DateTimeImmutable
136
    {
137
        if (isset($this->event->DTEND)) {
138
            /** @var \Sabre\VObject\Property\ICalendar\DateTime $dtEnd */
139
            $dtEnd = $this->event->DTEND;
140
141
            return $dtEnd->getDateTime();
142
        }
143
        if (isset($this->event->DURATION)) {
144
            /** @var \Sabre\VObject\Property\ICalendar\DateTime $dtStart */
145
            $dtStart = $this->event->DTSTART;
146
            $duration = $this->event->DURATION->getDateInterval();
147
148
            return (clone $dtStart)->getDateTime()->add($duration);
149
        }
150
        return null;
151
    }
152
153
    /**
154
     * @inheritDoc
155
     */
156
    public function getStartTime(): int
157
    {
158
        if ($this->isAllDay() || !isset($this->event->DTSTART)) {
159
            return self::ALLDAY_START_TIME;
160
        }
161
162
        /** @var \Sabre\VObject\Property\ICalendar\DateTime $start */
163
        $start = $this->event->DTSTART;
164
        return DateTimeUtility::getDaySecondsOfDateTime($start->getDateTime());
0 ignored issues
show
Bug introduced by
It seems like $start->getDateTime() can be null; however, getDaySecondsOfDateTime() 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...
165
    }
166
167
    /**
168
     * @inheritDoc
169
     */
170
    public function getEndTime(): int
171
    {
172
        $end = $this->getEndDateTime();
173
        if ($this->isAllDay() || empty($end)) {
174
            return self::ALLDAY_END_TIME;
175
        }
176
177
        return DateTimeUtility::getDaySecondsOfDateTime($end);
178
    }
179
180
    /**
181
     * @inheritDoc
182
     */
183
    public function isAllDay(): bool
184
    {
185
        if (!isset($this->event->DTSTART)) {
186
            return true;
187
        }
188
        /** @var \Sabre\VObject\Property\ICalendar\DateTime $start */
189
        $start = $this->event->DTSTART;
190
        return !$start->hasTime();
191
    }
192
193
    /**
194
     * @inheritDoc
195
     */
196
    public function isOpenEndTime(): bool
197
    {
198
        return false;
199
    }
200
201
    /**
202
     * @inheritDoc
203
     */
204
    public function getState(): string
205
    {
206
        if (isset($this->event->STATUS)) {
207
            $status = $this->event->STATUS->getValue();
208
            if ($status === 'CANCELLED') {
209
                return ConfigurationInterface::STATE_CANCELED;
210
            }
211
        }
212
        return ConfigurationInterface::STATE_DEFAULT;
213
    }
214
}
215