Passed
Pull Request — master (#1187)
by René
04:10
created

CalendarEvent::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 15
rs 9.7998
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]>
4
 *
5
 * @author René Gieling <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Polls\Model;
25
26
use \OCP\Calendar\ICalendar;
27
28
class CalendarEvent implements \JsonSerializable {
29
30
	/** @var Array */
31
	protected $calDav;
32
33
	/** @var Array */
34
	protected $event;
35
36
	/** @var ICalendar */
37
	protected $calendar;
38
39
	/**
40
	 * CalendarEvent constructor.
41
	 * @param Array $calDav
42
	 * @param ICalendar $calendar
43
	 */
44
	public function __construct(
45
		$calDav,
46
		$calendar
47
	) {
48
		$this->calDav = $calDav;
49
		$this->calendar = $calendar;
50
		$this->event = $this->calDav['objects'][0];
51
	}
52
53
54
	/**
55
	 * getAllDay
56
	 * @return string
57
	 */
58
	public function getAllDay() {
59
		if ($this->getEnd() - $this->getStart() === 86400) {
60
			return $this->event['DTSTART'][0]->format('Y-m-d');
61
		} else {
62
			return '';
63
		}
64
	}
65
66
	/**
67
	 * getCalendarName
68
	 * @return string
69
	 */
70
	public function getCalendarName() {
71
		return $this->calendar->getDisplayName();
72
	}
73
74
	/**
75
	 * getCalendarKey
76
	 * @return string
77
	 */
78
	public function getCalendarKey() {
79
		return $this->calendar->getKey();
80
	}
81
82
	/**
83
	 * getDisplayColor
84
	 * @return string
85
	 */
86
	public function getDisplayColor() {
87
		return $this->calendar->getDisplayColor();
88
	}
89
90
	/**
91
	 * getId
92
	 * @return int
93
	 */
94
	public function getId() {
95
		return $this->calDav['id'];
96
	}
97
98
	/**
99
	 * getUID
100
	 * @return string
101
	 */
102
	public function getUID() {
103
		return $this->event['UID'][0];
104
	}
105
106
	/**
107
	 * getSummary
108
	 * @return string
109
	 */
110
	public function getSummary() {
111
		return $this->event['SUMMARY'][0];
112
	}
113
114
	/**
115
	 * getDescription
116
	 * @return string
117
	 */
118
	public function getDescription() {
119
		if (isset($this->event['DESCRIPTION'][0])) {
120
			return $this->event['DESCRIPTION'][0];
121
		} else {
122
			return '';
123
		}
124
	}
125
126
	/**
127
	 * getLocation
128
	 * @return string
129
	 */
130
	public function getLocation() {
131
		if (isset($this->event['LOCATION'][0])) {
132
			return $this->event['LOCATION'][0];
133
		} else {
134
			return '';
135
		}
136
	}
137
138
	/**
139
	 * getStart
140
	 * @return int
141
	 */
142
	public function getStart() {
143
		if (isset($this->event['DTSTART'][0])) {
144
			return $this->event['DTSTART'][0]->getTimestamp();
145
		} else {
146
			return 0;
147
		}
148
	}
149
150
	/**
151
	 * getEnd
152
	 * @return int
153
	 */
154
	public function getEnd() {
155
		if (isset($this->event['DTEND'][0])) {
156
			return $this->event['DTEND'][0]->getTimestamp();
157
		} else {
158
			return 0;
159
		}
160
	}
161
162
	/**
163
	 * getStatus
164
	 * @return string
165
	 */
166
	public function getStatus() {
167
		if (isset($this->event['STATUS'][0])) {
168
			return $this->event['STATUS'][0];
169
		} else {
170
			return '';
171
		}
172
	}
173
174
	/**
175
	 * getCalDav
176
	 * @return
177
	 */
178
	public function getCalDav() {
179
		return $this->calDav;
180
	}
181
182
	/**
183
	 * @return array
184
	 */
185
	public function jsonSerialize(): array {
186
		return	[
187
			'id'			=> $this->getId(),
188
			'UID'			=> $this->getUID(),
189
			'calendarKey'	=> $this->getCalendarKey(),
190
			'calendarName'	=> $this->getCalendarName(),
191
			'displayColor'	=> $this->getDisplayColor(),
192
			'allDay'		=> $this->getAllDay(),
193
			'description'	=> $this->getDescription(),
194
			'end'			=> $this->getEnd(),
195
			'location'		=> $this->getLocation(),
196
			'start'			=> $this->getStart(),
197
			'status'		=> $this->getStatus(),
198
			'summary'		=> $this->getSummary(),
199
			'calDav'		=> $this->getCalDav(),
200
		];
201
	}
202
}
203