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

DissectEvent::isAllDay()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
namespace HDNET\Calendarize\Domain;
5
6
use HDNET\Calendarize\Domain\Model\Configuration;
7
use HDNET\Calendarize\Domain\Model\ConfigurationInterface;
8
use HDNET\Calendarize\Domain\Model\Event;
9
use HDNET\Calendarize\Utility\DateTimeUtility;
10
11
class DissectEvent implements ICalEvent
12
{
13
14
    /**
15
     * @var \JMBTechnologyLimited\ICalDissect\ICalEvent
16
     */
17
    protected $event;
18
19
    /**
20
     * DissectEvent constructor.
21
     * @param \JMBTechnologyLimited\ICalDissect\ICalEvent $event
22
     */
23
    public function __construct(\JMBTechnologyLimited\ICalDissect\ICalEvent $event)
24
    {
25
        $this->event = $event;
26
    }
27
28
    public function getRawData(): array
29
    {
30
        return $this->event->getRaw();
31
    }
32
33
    public function updateEvent(Event $eventObject)
34
    {
35
        // TODO how to handle existing event / by uid
36
        // TODO how to handle pid
37
38
        $eventObject->setTitle($this->event->getSummary());
39
        $eventObject->setDescription($this->event->getDescription());
40
        $eventObject->setLocation($this->event->getLocation());
41
42
        $configuration = new Configuration();
43
44
        $startOfDay = $this->event->getStart()->format('H:i:s') === '00:00:00';
45
        $endOfDay = $this->event->getEnd()->format('H:i:s') === '23:59:59';
46
47
        if ($startOfDay && $endOfDay) {
48
            $configuration->setAllDay(true);
49
        } else {
50
            $configuration->setAllDay(false);
51
        }
52
    }
53
54
    // tstamp (record edited)
55
    // crdate (record created)
56
    // cruser_id
57
58
    // ??! deleted 	sorting 	hidden
59
    //  	starttime 	endtime (visibility)
60
61
    // fe_group 	sys_language_uid 	l10n_parent 	l10n_diffsource 	l10n_state 	unique_register_key 	foreign_table 	foreign_uid
62
63
    // editlock
64
65
    // uid 	pid
66
67
    // As getter
68
    // start_date 	end_date 	start_time 	end_time
69
    // all_day 	open_end_time 	state
70
71
    /**
72
     * Fixes a parser related bug where the DTEND is EXCLUSIVE.
73
     * The parser uses it inclusive so every event is one day
74
     * longer than it should be.
75
     *
76
     * @param \JMBTechnologyLimited\ICalDissect\ICalEvent $event
77
     *
78
     * @return \DateTime|null
79
     */
80
    protected function getEventsFixedEndDate(\JMBTechnologyLimited\ICalDissect\ICalEvent $event)
81
    {
82
        if (!$event->getEnd() instanceof \DateTimeInterface) {
83
            return null;
84
        }
85
86
        $end = clone $event->getEnd();
87
        $end->sub(new \DateInterval('P1D'));
88
        if ($end->format('Ymd') === $event->getStart()->format('Ymd')) {
89
            return $end;
90
        }
91
92
        return $event->getEnd();
93
    }
94
95
    /**
96
     * @return \JMBTechnologyLimited\ICalDissect\ICalEvent
97
     */
98
    public function getEvent(): ICalEvent
99
    {
100
        return $this->event;
101
    }
102
103
    /**
104
     * @inheritDoc
105
     */
106
    public function getUid(): string
107
    {
108
        return $this->event->getUid();
109
    }
110
111
    /**
112
     * @inheritDoc
113
     */
114
    public function getTitle(): string
115
    {
116
        return $this->event->getSummary();
117
    }
118
119
    /**
120
     * @inheritDoc
121
     */
122
    public function getDescription(): ?string
123
    {
124
        return $this->event->getDescription();
125
    }
126
127
    /**
128
     * @inheritDoc
129
     */
130
    public function getLocation(): ?string
131
    {
132
        return $this->event->getLocation();
133
    }
134
135
    /**
136
     * @inheritDoc
137
     */
138
    public function getOrganizer(): ?string
139
    {
140
        // TODO: Implement getOrganizer() method.
141
        return null;
142
    }
143
144
    /**
145
     * @inheritDoc
146
     */
147
    public function getStartDate(): ?\DateTime
148
    {
149
        return DateTimeUtility::getDayStart($this->event->getStart());
150
    }
151
152
    /**
153
     * @inheritDoc
154
     */
155
    public function getEndDate(): ?\DateTime
156
    {
157
        // TODO: How to handle events without end?
158
        $endDate = $this->getEventsFixedEndDate($this->event);
159
        if ($endDate === null) {
160
            return null;
161
        }
162
163
        return DateTimeUtility::getDayStart($endDate);
164
    }
165
166
    /**
167
     * @inheritDoc
168
     */
169
    public function getStartTime(): int
170
    {
171
        if ($this->isAllDay()) {
172
            return self::ALLDAY_START_TIME;
173
        }
174
        return DateTimeUtility::getDaySecondsOfDateTime($this->event->getStart());
175
    }
176
177
    /**
178
     * @inheritDoc
179
     */
180
    public function getEndTime(): int
181
    {
182
        if ($this->isOpenEndTime() || $this->isAllDay()) {
183
            return self::ALLDAY_END_TIME;
184
        }
185
        return DateTimeUtility::getDaySecondsOfDateTime($this->event->getEnd());
186
    }
187
188
    /**
189
     * @inheritDoc
190
     */
191
    public function isAllDay(): bool
192
    {
193
        $startTime = DateTimeUtility::getDaySecondsOfDateTime($this->event->getStart());
194
        $endTime = DateTimeUtility::getDaySecondsOfDateTime($this->event->getEnd());
195
196
        // If the event has no time
197
        //  ICalDissect\ICalEvent sets
198
        //  starTime to 00:00:00 = 0
199
        //  endTime to 23:59:59 = (24h*60m*60s - 1) = 86399
200
        return $startTime === 0 && $endTime === 86399;
201
    }
202
203
    /**
204
     * @inheritDoc
205
     */
206
    public function isOpenEndTime(): bool
207
    {
208
        // If no end DateTime is given, we assume it is open end
209
        return $this->getEndDate() === null;
210
    }
211
212
    /**
213
     * @inheritDoc
214
     */
215
    public function getState(): string
216
    {
217
        if ($this->event->isDeleted()) {
218
            return ConfigurationInterface::STATE_CANCELED;
219
        }
220
        return ConfigurationInterface::STATE_DEFAULT;
221
    }
222
}
223