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

VObjectEvent::getUid()   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
namespace HDNET\Calendarize\Domain;
5
6
use HDNET\Calendarize\Domain\Model\ConfigurationInterface;
7
use HDNET\Calendarize\Utility\DateTimeUtility;
8
use Sabre\VObject\Component\VEvent;
9
10
class VObjectEvent implements ICalEvent
11
{
12
13
    /**
14
     * @var VEvent
15
     */
16
    private $event;
17
18
    /**
19
     * VObjectEvent constructor.
20
     * @param VEvent $event
21
     */
22
    public function __construct(VEvent $event)
23
    {
24
        $this->event = $event;
25
    }
26
27
    /**
28
     * @return VEvent
29
     */
30
    public function getEvent(): VEvent
31
    {
32
        return $this->event;
33
    }
34
35
    /**
36
     * @inheritDoc
37
     */
38
    public function getRawData(): array
39
    {
40
        return $this->event->children();
41
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46
    public function getUid(): string
47
    {
48
        return $this->event->UID->getValue();
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54
    public function getTitle(): string
55
    {
56
        return $this->event->SUMMARY->getValue();
57
    }
58
59
    /**
60
     * @inheritDoc
61
     */
62
    public function getDescription(): ?string
63
    {
64
        if (!isset($this->event->DESCRIPTION)) {
65
            return null;
66
        }
67
68
        return $this->event->DESCRIPTION ->getValue();
69
    }
70
71
    // TODO: nullable casting
72
    /**
73
     * @inheritDoc
74
     */
75
    public function getLocation(): ?string
76
    {
77
        if (!isset($this->event->LOCATION)) {
78
            return null;
79
        }
80
81
        return $this->event->LOCATION->getValue();
82
    }
83
84
    /**
85
     * @inheritDoc
86
     */
87
    public function getOrganizer(): ?string
88
    {
89
        if (!isset($this->event->ORGANIZER)) {
90
            return null;
91
        }
92
93
        return $this->event->ORGANIZER->getValue();
94
    }
95
96
    /**
97
     * @inheritDoc
98
     */
99
    public function getStartDate(): ?\DateTime
100
    {
101
        /** @var \Sabre\VObject\Property\ICalendar\DateTime $start */
102
        $start = $this->event->DTSTART;
103
        return DateTimeUtility::getDayStart($start->getDateTime());
104
    }
105
106
    /**
107
     * @inheritDoc
108
     */
109
    public function getEndDate(): ?\DateTime
110
    {
111
        if (!isset($this->event->DTEND)) {
112
            return null;
113
        }
114
115
        /** @var \Sabre\VObject\Property\ICalendar\DateTime $end */
116
        $end = $this->event->DTEND;
117
118
        return DateTimeUtility::getDayStart($end->getDateTime());
119
    }
120
121
    /**
122
     * @inheritDoc
123
     */
124
    public function getStartTime(): int
125
    {
126
        if ($this->isAllDay()) {
127
            return self::ALLDAY_START_TIME;
128
        }
129
        // TODO: Type / Interface ???
130
131
        /** @var \Sabre\VObject\Property\ICalendar\DateTime $start */
132
        $start = $this->event->DTSTART;
133
        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...
134
    }
135
136
    /**
137
     * @inheritDoc
138
     */
139
    public function getEndTime(): int
140
    {
141
        if ($this->isOpenEndTime() || $this->isAllDay()) {
142
            return self::ALLDAY_END_TIME;
143
        }
144
145
        /** @var \Sabre\VObject\Property\ICalendar\DateTime $end */
146
        $end = $this->event->DTEND;
147
        // TODO: Check empty response of getDateTime()
148
        return DateTimeUtility::getDaySecondsOfDateTime($end->getDateTime());
0 ignored issues
show
Bug introduced by
It seems like $end->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...
149
    }
150
151
    /**
152
     * @inheritDoc
153
     */
154
    public function isAllDay(): bool
155
    {
156
        /** @var \Sabre\VObject\Property\ICalendar\DateTime $start */
157
        $start = $this->event->DTSTART;
158
159
        if ($this->isOpenEndTime()) {
160
            return !$start->hasTime();
161
        }
162
163
        /** @var \Sabre\VObject\Property\ICalendar\DateTime $end */
164
        $end = $this->event->DTEND;
165
166
        return !$start->hasTime() && !$end->hasTime();
167
    }
168
169
    /**
170
     * @inheritDoc
171
     */
172
    public function isOpenEndTime(): bool
173
    {
174
        if (!isset($this->event->DTEND)) {
175
            return false;
176
        }
177
        return !$this->event->DTEND->hasTime();
178
    }
179
180
    /**
181
     * @inheritDoc
182
     */
183
    public function getState(): string
184
    {
185
        if (isset($this->event->STATUS)) {
186
            $status = $this->event->STATUS->getValue();
187
            if ($status === 'CANCELLED') {
188
                return ConfigurationInterface::STATE_CANCELED;
189
            }
190
        }
191
        return ConfigurationInterface::STATE_DEFAULT;
192
    }
193
}
194