Passed
Pull Request — master (#1187)
by René
07:30 queued 03:09
created

CalendarService::getEvents()   C

Complexity

Conditions 13
Paths 9

Size

Total Lines 69
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 41
c 2
b 1
f 0
dl 0
loc 69
ccs 0
cts 24
cp 0
rs 6.6166
cc 13
nc 9
nop 2
crap 182

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
	private $preferencesService;
34
	private $preferences;
35
36
	public function __construct(
37
		CalendarManager $calendarManager,
38
		PreferencesService $preferencesService
39
	) {
40
		$this->calendarManager = $calendarManager;
41
		$this->preferencesService = $preferencesService;
42
		$this->preferences = $this->preferencesService->get();
43
		$this->calendars = $this->calendarManager->getCalendars();
44
	}
45
46
	/**
47
	 * getEvents - get events from the user's calendars inside given timespan
48
	 * @NoAdminRequired
49
	 * @param DateTime $from
50
	 * @param DateTime $to
51
	 * @return Array
52
	 */
53
	public function getEvents($from, $to) {
54
		$events = [];
55
		foreach ($this->calendars as $calendar) {
56
57
			// Skip not configured calendars
58
			if (!in_array($calendar->getKey(), json_decode($this->preferences->getPreferences())->checkCalendars)) {
59
				continue;
60
			}
61
			$searchFromTs = $from->getTimestamp();
62
			$searchToTs = $to->getTimestamp();
63
64
			// search for all events which
65
			// - start before the end of the requested timespan ($to) and
66
			// - end before the start of the requested timespan ($from)
67
			$foundEvents = $calendar->search('', ['SUMMARY'], ['timerange' => ['start' => $from, 'end' => $to]]);
68
			foreach ($foundEvents as $event) {
69
				if (isset($event['objects'][0]['DTSTART'][0]) && isset($event['objects'][0]['DTEND'][0])) {
70
71
					// INFO: all days events always start at 00:00 UTC and end at 00:00 UTC the next day
72
					$eventStartTs = $event['objects'][0]['DTSTART'][0]->getTimestamp();
73
					$eventEndTs = $event['objects'][0]['DTEND'][0]->getTimestamp();
74
75
					$eventStartsBefore = ($searchToTs - $eventStartTs > 0);
76
					$eventEndsafter = ($eventEndTs - $searchFromTs > 0);
77
78
					// since we get back recurring events of other days, just make sure this event
79
					// matches the search pattern
80
					// TODO: identify possible time zone issues, whan handling all day events
81
					if (!$eventStartsBefore || !$eventEndsafter) {
82
						continue;
83
					}
84
85
					// check, if the event is an all day event
86
					$allDay = '';
87
					if ($eventEndTs-$eventStartTs === 86400) {
88
						$allDay = $event['objects'][0]['DTSTART'][0]->format('Y-m-d');
89
					}
90
91
					// get the events status (cancelled or tentative)
92
					$status = '';
93
					if (isset($event['objects'][0]['STATUS'])) {
94
						$status = $event['objects'][0]['STATUS'][0];
95
					}
96
				} else {
97
					continue;
98
				}
99
100
101
				array_push($events, [
102
					'relatedFrom' => $searchFromTs,
103
					'allDay' => $allDay,
104
					'relatedTo' => $searchToTs,
105
					'name' => $calendar->getDisplayName(),
106
					'key' => $calendar->getKey(),
107
					'displayColor' => $calendar->getDisplayColor(),
108
					'permissions' => $calendar->getPermissions(),
109
					'eventId' => $event['id'],
110
					'UID' => $event['objects'][0]['UID'][0],
111
					'summary' => isset($event['objects'][0]['SUMMARY'][0]) ? $event['objects'][0]['SUMMARY'][0] : '',
112
					'description' => isset($event['objects'][0]['DESCRIPTION'][0]) ? $event['objects'][0]['DESCRIPTION'][0] : '',
113
					'location' => isset($event['objects'][0]['LOCATION'][0]) ? $event['objects'][0]['LOCATION'][0] : '',
114
					'eventFrom' => $eventStartTs,
115
					'eventTo' => $eventEndTs,
116
					'status' => $status,
117
					'calDav' => $event
118
				]);
119
			}
120
		}
121
		return $events;
122
	}
123
124
	/**
125
	 * Get user's calendars
126
	 * @NoAdminRequired
127
	 * @return Array
128
	 */
129
	public function getCalendars() {
130
		$calendars =  [];
131
		foreach ($this->calendars as $calendar) {
132
			$calendars[] = [
133
				'name' => $calendar->getDisplayName(),
134
				'key' => $calendar->getKey(),
135
				'displayColor' => $calendar->getDisplayColor(),
136
				'permissions' => $calendar->getPermissions(),
137
			];
138
		}
139
		return $calendars;
140
	}
141
}
142