Passed
Pull Request — master (#747)
by René
04:11
created

CalendarService::getEventsAround()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 2
rs 10
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 OCP\Calendar\ICalendar;
30
31
class CalendarService {
32
33
	private $calendarManager;
34
	private $calendars;
35
36
	public function __construct(
37
		CalendarManager $calendarManager
38
	) {
39
		$this->calendarManager = $calendarManager;
40
		$this->calendars = $this->calendarManager->getCalendars();
41
	}
42
43
	/**
44
	 * getEvents - get events from the user's calendars inside given timespan
45
	 * @NoAdminRequired
46
	 * @param DateTime $from
47
	 * @param DateTime $to
48
	 * @return Array
49
	 */
50
	public function getEvents($from, $to) {
51
		$events = [];
52
53
		foreach ($this->calendars as $calendar) {
54
			$foundEvents = $calendar->search('' ,['SUMMARY'], ['timerange' => ['start' => $from, 'end' => $to]]);
55
			foreach ($foundEvents as $event) {
56
				array_push($events, [
57
					'relatedFrom' => $from->getTimestamp(),
58
					'relatedTo' => $to->getTimestamp(),
59
					'name' => $calendar->getDisplayName(),
60
					'key' => $calendar->getKey(),
61
					'displayColor' => $calendar->getDisplayColor(),
62
					'permissions' => $calendar->getPermissions(),
63
					'eventId' => $event['id'],
64
					'UID' => $event['objects'][0]['UID'][0],
65
					'summary' => isset($event['objects'][0]['SUMMARY'][0])? $event['objects'][0]['SUMMARY'][0] : '',
66
					'description' => isset($event['objects'][0]['DESCRIPTION'][0])? $event['objects'][0]['DESCRIPTION'][0] : '',
67
					'location' => isset($event['objects'][0]['LOCATION'][0]) ? $event['objects'][0]['LOCATION'][0] : '',
68
					'eventFrom' => isset($event['objects'][0]['DTSTART'][0]) ? $event['objects'][0]['DTSTART'][0]->getTimestamp() : 0,
69
					'eventTo' => isset($event['objects'][0]['DTEND'][0] ) ? $event['objects'][0]['DTEND'][0]->getTimestamp() : 0,
70
					'calDav' => $event
71
				]);
72
			}
73
		}
74
		return $events;
75
	}
76
77
	/**
78
	 * Get user's calendars
79
	 * @NoAdminRequired
80
	 * @return Array
81
	 */
82
	public function getCalendars() {
83
		return $this->calendars;
84
	}
85
86
87
	/**
88
	 * Get events from the user's calendar which are 2 hours before and 3 hours after the timestamp
89
	 * @NoAdminRequired
90
	 * @param DateTime $from
91
	 * @return Array
92
	 */
93
	public function getEventsAround($from) {
94
		$from = new DateTime($from);
0 ignored issues
show
Bug introduced by
$from of type DateTime is incompatible with the type string expected by parameter $time of DateTime::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
		$from = new DateTime(/** @scrutinizer ignore-type */ $from);
Loading history...
95
		$to = new DateTime($from);
96
		return $this->getEvents(
97
			$from->sub(new DateInterval('P2H')),
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Service\DateInterval was not found. Did you mean DateInterval? If so, make sure to prefix the type with \.
Loading history...
98
			$to->add(new DateInterval('P3H'))
99
		);
100
	}
101
102
}
103