Completed
Pull Request — master (#1187)
by René
06:28
created

CalendarService::getEvents()   C

Complexity

Conditions 13
Paths 9

Size

Total Lines 71
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
c 1
b 0
f 0
dl 0
loc 71
ccs 0
cts 23
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 DateInterval;
29
use OCP\Calendar\IManager as CalendarManager;
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
			$searchFromTs = $from->getTimestamp();
63
			$searchToTs = $to->getTimestamp();
64
65
			// search for all events which
66
			// - start before the end of the requested timespan ($to) and
67
			// - end before the start of the requested timespan ($from)
68
			$foundEvents = $calendar->search('', ['SUMMARY'], ['timerange' => ['start' => $from, 'end' => $to]]);
69
			foreach ($foundEvents as $event) {
70
71
				if (isset($event['objects'][0]['DTSTART'][0]) && isset($event['objects'][0]['DTEND'][0])) {
72
73
					// INFO: all days events always start at 00:00 UTC and end at 00:00 UTC the next day
74
					$eventStartTs = $event['objects'][0]['DTSTART'][0]->getTimestamp();
75
					$eventEndTs = $event['objects'][0]['DTEND'][0]->getTimestamp();
76
77
					$eventStartsBefore = ($searchToTs - $eventStartTs > 0);
78
					$eventEndsafter = ($eventEndTs - $searchFromTs > 0);
79
80
					// since we get back recurring events of other days, just make sure this event
81
					// matches the search pattern
82
					// TODO: identify possible time zone issues, whan handling all day events
83
					if (!$eventStartsBefore || !$eventEndsafter) {
84
						continue;
85
					}
86
87
					// check, if the event is an all day event
88
					$allDay = '';
89
					if ($eventEndTs-$eventStartTs === 86400) {
90
						$allDay = $event['objects'][0]['DTSTART'][0]->format('Y-m-d');
91
					}
92
93
					// get the events status (cancelled or tentative)
94
					$status = '';
95
					if (isset($event['objects'][0]['STATUS'])) {
96
						$status = $event['objects'][0]['STATUS'][0];
97
					}
98
99
				} else {
100
					continue;
101
				}
102
103
104
				array_push($events, [
105
					'relatedFrom' => $searchFromTs,
106
					'allDay' => $allDay,
107
					'relatedTo' => $searchToTs,
108
					'name' => $calendar->getDisplayName(),
109
					'key' => $calendar->getKey(),
110
					'displayColor' => $calendar->getDisplayColor(),
111
					'permissions' => $calendar->getPermissions(),
112
					'eventId' => $event['id'],
113
					'UID' => $event['objects'][0]['UID'][0],
114
					'summary' => isset($event['objects'][0]['SUMMARY'][0]) ? $event['objects'][0]['SUMMARY'][0] : '',
115
					'description' => isset($event['objects'][0]['DESCRIPTION'][0]) ? $event['objects'][0]['DESCRIPTION'][0] : '',
116
					'location' => isset($event['objects'][0]['LOCATION'][0]) ? $event['objects'][0]['LOCATION'][0] : '',
117
					'eventFrom' => $eventStartTs,
118
					'eventTo' => $eventEndTs,
119
					'status' => $status,
120
					'calDav' => $event
121
				]);
122
			}
123
		}
124
		return $events;
125
	}
126
127
	/**
128
	 * Get user's calendars
129
	 * @NoAdminRequired
130
	 * @return Array
131
	 */
132
	public function getCalendars() {
133
		$calendars =  [];
134
		foreach ($this->calendars as $calendar) {
135
			$calendars[] = [
136
				'name' => $calendar->getDisplayName(),
137
				'key' => $calendar->getKey(),
138
				'displayColor' => $calendar->getDisplayColor(),
139
				'permissions' => $calendar->getPermissions(),
140
			];
141
		}
142
		return $calendars;
143
	}
144
}
145