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

CalendarService::getEvents()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 27
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 11
c 2
b 1
f 0
dl 0
loc 27
rs 9.2222
ccs 0
cts 21
cp 0
cc 6
nc 5
nop 2
crap 42
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
use OCA\Polls\Model\CalendarEvent;
30
31
class CalendarService {
32
	private $calendarManager;
33
	private $calendars;
34
	private $preferencesService;
35
	private $preferences;
36
37
	public function __construct(
38
		CalendarManager $calendarManager,
39
		PreferencesService $preferencesService
40
	) {
41
		$this->calendarManager = $calendarManager;
42
		$this->preferencesService = $preferencesService;
43
		$this->preferences = $this->preferencesService->get();
44
		$this->calendars = $this->calendarManager->getCalendars();
45
	}
46
47
	/**
48
	 * getEvents - get events from the user's calendars inside given timespan
49
	 * @NoAdminRequired
50
	 * @param DateTime $from
51
	 * @param DateTime $to
52
	 * @return Array
53
	 */
54
	public function getEvents($from, $to) {
55
		$events = [];
56
		foreach ($this->calendars as $calendar) {
57
58
			// Skip not configured calendars
59
			if (!in_array($calendar->getKey(), json_decode($this->preferences->getPreferences())->checkCalendars)) {
60
				continue;
61
			}
62
63
			// search for all events which
64
			// - start before the end of the requested timespan ($to) and
65
			// - end before the start of the requested timespan ($from)
66
			$foundEvents = $calendar->search('', ['SUMMARY'], ['timerange' => ['start' => $from, 'end' => $to]]);
67
			foreach ($foundEvents as $event) {
68
				$calendarEvent = new CalendarEvent($event, $calendar);
69
70
				// since we get back recurring events of other days, just make sure this event
71
				// matches the search pattern
72
				// TODO: identify possible time zone issues, whan handling all day events
73
				if (($from->getTimestamp() < $calendarEvent->getEnd())
74
					&& ($to->getTimestamp() > $calendarEvent->getStart())) {
75
					array_push($events, $calendarEvent);
76
				}
77
			}
78
		}
79
80
		return $events;
81
	}
82
83
	/**
84
	 * Get user's calendars
85
	 * @NoAdminRequired
86
	 * @return Array
87
	 */
88
	public function getCalendars() {
89
		$calendars =  [];
90
		foreach ($this->calendars as $calendar) {
91
			$calendars[] = [
92
				'name' => $calendar->getDisplayName(),
93
				'key' => $calendar->getKey(),
94
				'displayColor' => $calendar->getDisplayColor(),
95
				'permissions' => $calendar->getPermissions(),
96
			];
97
		}
98
		return $calendars;
99
	}
100
}
101