Completed
Push — master ( 44b423...f32fbb )
by Morris
13:09
created

Manager::getCalendars()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright 2017, Georg Ehrke <[email protected]>
4
 *
5
 * @author Georg Ehrke <[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
namespace OC\Calendar;
25
26
use OCP\Calendar\ICalendar;
27
28
class Manager implements \OCP\Calendar\IManager {
29
30
	/**
31
	 * @var ICalendar[] holds all registered calendars
32
	 */
33
	private $calendars=[];
34
35
	/**
36
	 * @var \Closure[] to call to load/register calendar providers
37
	 */
38
	private $calendarLoaders=[];
39
40
	/**
41
	 * This function is used to search and find objects within the user's calendars.
42
	 * In case $pattern is empty all events/journals/todos will be returned.
43
	 *
44
	 * @param string $pattern which should match within the $searchProperties
45
	 * @param array $searchProperties defines the properties within the query pattern should match
46
	 * @param array $options - optional parameters:
47
	 * 	['timerange' => ['start' => new DateTime(...), 'end' => new DateTime(...)]]
48
	 * @param integer|null $limit - limit number of search results
49
	 * @param integer|null $offset - offset for paging of search results
50
	 * @return array an array of events/journals/todos which are arrays of arrays of key-value-pairs
51
	 * @since 13.0.0
52
	 */
53
	public function search($pattern, array $searchProperties=[], array $options=[], $limit=null, $offset=null) {
54
		$this->loadCalendars();
55
		$result = [];
56
		foreach($this->calendars as $calendar) {
57
			$r = $calendar->search($pattern, $searchProperties, $options, $limit, $offset);
58
			foreach($r as $o) {
59
				$o['calendar-key'] = $calendar->getKey();
60
				$result[] = $o;
61
			}
62
		}
63
64
		return $result;
65
	}
66
67
	/**
68
	 * Check if calendars are available
69
	 *
70
	 * @return bool true if enabled, false if not
71
	 * @since 13.0.0
72
	 */
73
	public function isEnabled() {
74
		return !empty($this->calendars) || !empty($this->calendarLoaders);
75
	}
76
77
	/**
78
	 * Registers a calendar
79
	 *
80
	 * @param ICalendar $calendar
81
	 * @return void
82
	 * @since 13.0.0
83
	 */
84
	public function registerCalendar(ICalendar $calendar) {
85
		$this->calendars[$calendar->getKey()] = $calendar;
86
	}
87
88
	/**
89
	 * Unregisters a calendar
90
	 *
91
	 * @param ICalendar $calendar
92
	 * @return void
93
	 * @since 13.0.0
94
	 */
95
	public function unregisterCalendar(ICalendar $calendar) {
96
		unset($this->calendars[$calendar->getKey()]);
97
	}
98
99
	/**
100
	 * In order to improve lazy loading a closure can be registered which will be called in case
101
	 * calendars are actually requested
102
	 *
103
	 * @param \Closure $callable
104
	 * @return void
105
	 * @since 13.0.0
106
	 */
107
	public function register(\Closure $callable) {
108
		$this->calendarLoaders[] = $callable;
109
	}
110
111
	/**
112
	 * @return ICalendar[]
113
	 * @since 13.0.0
114
	 */
115
	public function getCalendars() {
116
		$this->loadCalendars();
117
118
		return array_values($this->calendars);
119
	}
120
121
	/**
122
	 * removes all registered calendar instances
123
	 * @return void
124
	 * @since 13.0.0
125
	 */
126
	public function clear() {
127
		$this->calendars = [];
128
		$this->calendarLoaders = [];
129
	}
130
131
	/**
132
	 * loads all calendars
133
	 */
134
	private function loadCalendars() {
135
		foreach($this->calendarLoaders as $callable) {
136
			$callable($this);
137
		}
138
		$this->calendarLoaders = [];
139
	}
140
}
141