Completed
Pull Request — master (#1038)
by René
06:20
created

CalendarService::getEvents()   B

Complexity

Conditions 8
Paths 3

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 25
ccs 0
cts 24
cp 0
rs 8.4444
cc 8
nc 3
nop 2
crap 72
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
25
namespace OCA\Polls\Service;
26
27
use DateTime;
28
use OCP\Calendar\IManager as CalendarManager;
29
30
class CalendarService {
31
	private $calendarManager;
32
	private $calendars;
33
34
	public function __construct(
35
		CalendarManager $calendarManager
36
	) {
37
		$this->calendarManager = $calendarManager;
38
		$this->calendars = $this->calendarManager->getCalendars();
39
	}
40
41
	/**
42
	 * getEvents - get events from the user's calendars inside given timespan
43
	 * @NoAdminRequired
44
	 * @param DateTime $from
45
	 * @param DateTime $to
46
	 * @return Array
47
	 */
48
	public function getEvents($from, $to) {
49
		$events = [];
50
51
		foreach ($this->calendars as $calendar) {
52
			$foundEvents = $calendar->search('', ['SUMMARY'], ['timerange' => ['start' => $from, 'end' => $to]]);
53
			foreach ($foundEvents as $event) {
54
				array_push($events, [
55
					'relatedFrom' => $from->getTimestamp(),
56
					'relatedTo' => $to->getTimestamp(),
57
					'name' => $calendar->getDisplayName(),
58
					'key' => $calendar->getKey(),
59
					'displayColor' => $calendar->getDisplayColor(),
60
					'permissions' => $calendar->getPermissions(),
61
					'eventId' => $event['id'],
62
					'UID' => $event['objects'][0]['UID'][0],
63
					'summary' => isset($event['objects'][0]['SUMMARY'][0]) ? $event['objects'][0]['SUMMARY'][0] : '',
64
					'description' => isset($event['objects'][0]['DESCRIPTION'][0]) ? $event['objects'][0]['DESCRIPTION'][0] : '',
65
					'location' => isset($event['objects'][0]['LOCATION'][0]) ? $event['objects'][0]['LOCATION'][0] : '',
66
					'eventFrom' => isset($event['objects'][0]['DTSTART'][0]) ? $event['objects'][0]['DTSTART'][0]->getTimestamp() : 0,
67
					'eventTo' => isset($event['objects'][0]['DTEND'][0]) ? $event['objects'][0]['DTEND'][0]->getTimestamp() : 0,
68
					'calDav' => $event
69
				]);
70
			}
71
		}
72
		return $events;
73
	}
74
75
	/**
76
	 * Get user's calendars
77
	 * @NoAdminRequired
78
	 * @return Array
79
	 */
80
	public function getCalendars() {
81
		return $this->calendars;
82
	}
83
}
84