Passed
Push — master ( 9e596d...9f70c6 )
by Christoph
15:44 queued 10s
created

CalendarHome   A

Complexity

Total Complexity 36

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 68
dl 0
loc 163
rs 9.52
c 0
b 0
f 0
wmc 36

7 Methods

Rating   Name   Duplication   Size   Complexity  
A calendarSearch() 0 3 1
A createExtendedCollection() 0 11 3
A getCalDAVBackend() 0 2 1
D getChild() 0 50 19
A enableCachedSubscriptionsForThisRequest() 0 2 1
A __construct() 0 7 1
B getChildren() 0 41 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Christoph Wurst <[email protected]>
6
 * @author Georg Ehrke <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Lukas Reschke <[email protected]>
9
 * @author Roeland Jago Douma <[email protected]>
10
 * @author Thomas Müller <[email protected]>
11
 *
12
 * @license AGPL-3.0
13
 *
14
 * This code is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License, version 3,
16
 * as published by the Free Software Foundation.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License, version 3,
24
 * along with this program. If not, see <http://www.gnu.org/licenses/>
25
 *
26
 */
27
28
namespace OCA\DAV\CalDAV;
29
30
use OCA\DAV\AppInfo\PluginManager;
31
use OCA\DAV\CalDAV\Integration\ExternalCalendar;
32
use OCA\DAV\CalDAV\Integration\ICalendarProvider;
33
use OCA\DAV\CalDAV\Trashbin\TrashbinHome;
34
use Sabre\CalDAV\Backend\BackendInterface;
35
use Sabre\CalDAV\Backend\NotificationSupport;
36
use Sabre\CalDAV\Backend\SchedulingSupport;
37
use Sabre\CalDAV\Backend\SubscriptionSupport;
38
use Sabre\CalDAV\Schedule\Inbox;
39
use Sabre\CalDAV\Subscriptions\Subscription;
40
use Sabre\DAV\Exception\MethodNotAllowed;
41
use Sabre\DAV\Exception\NotFound;
42
use Sabre\DAV\INode;
43
use Sabre\DAV\MkCol;
44
45
class CalendarHome extends \Sabre\CalDAV\CalendarHome {
46
47
	/** @var \OCP\IL10N */
48
	private $l10n;
49
50
	/** @var \OCP\IConfig */
51
	private $config;
52
53
	/** @var PluginManager */
54
	private $pluginManager;
55
56
	/** @var bool */
57
	private $returnCachedSubscriptions = false;
58
59
	public function __construct(BackendInterface $caldavBackend, $principalInfo) {
60
		parent::__construct($caldavBackend, $principalInfo);
61
		$this->l10n = \OC::$server->getL10N('dav');
62
		$this->config = \OC::$server->getConfig();
63
		$this->pluginManager = new PluginManager(
64
			\OC::$server,
65
			\OC::$server->getAppManager()
66
		);
67
	}
68
69
	/**
70
	 * @return BackendInterface
71
	 */
72
	public function getCalDAVBackend() {
73
		return $this->caldavBackend;
74
	}
75
76
	/**
77
	 * @inheritdoc
78
	 */
79
	public function createExtendedCollection($name, MkCol $mkCol): void {
80
		$reservedNames = [
81
			BirthdayService::BIRTHDAY_CALENDAR_URI,
82
			TrashbinHome::NAME,
83
		];
84
85
		if (\in_array($name, $reservedNames, true) || ExternalCalendar::doesViolateReservedName($name)) {
86
			throw new MethodNotAllowed('The resource you tried to create has a reserved name');
87
		}
88
89
		parent::createExtendedCollection($name, $mkCol);
90
	}
91
92
	/**
93
	 * @inheritdoc
94
	 */
95
	public function getChildren() {
96
		$calendars = $this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']);
97
		$objects = [];
98
		foreach ($calendars as $calendar) {
99
			$objects[] = new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config);
100
		}
101
102
		if ($this->caldavBackend instanceof SchedulingSupport) {
103
			$objects[] = new Inbox($this->caldavBackend, $this->principalInfo['uri']);
104
			$objects[] = new Outbox($this->config, $this->principalInfo['uri']);
105
		}
106
107
		// We're adding a notifications node, if it's supported by the backend.
108
		if ($this->caldavBackend instanceof NotificationSupport) {
109
			$objects[] = new \Sabre\CalDAV\Notifications\Collection($this->caldavBackend, $this->principalInfo['uri']);
110
		}
111
112
		if ($this->caldavBackend instanceof CalDavBackend) {
113
			$objects[] = new TrashbinHome($this->caldavBackend, $this->principalInfo);
114
		}
115
116
		// If the backend supports subscriptions, we'll add those as well,
117
		if ($this->caldavBackend instanceof SubscriptionSupport) {
118
			foreach ($this->caldavBackend->getSubscriptionsForUser($this->principalInfo['uri']) as $subscription) {
119
				if ($this->returnCachedSubscriptions) {
120
					$objects[] = new CachedSubscription($this->caldavBackend, $subscription);
121
				} else {
122
					$objects[] = new Subscription($this->caldavBackend, $subscription);
123
				}
124
			}
125
		}
126
127
		foreach ($this->pluginManager->getCalendarPlugins() as $calendarPlugin) {
128
			/** @var ICalendarProvider $calendarPlugin */
129
			$calendars = $calendarPlugin->fetchAllForCalendarHome($this->principalInfo['uri']);
130
			foreach ($calendars as $calendar) {
131
				$objects[] = $calendar;
132
			}
133
		}
134
135
		return $objects;
136
	}
137
138
	/**
139
	 * @param string $name
140
	 *
141
	 * @return INode
142
	 */
143
	public function getChild($name) {
144
		// Special nodes
145
		if ($name === 'inbox' && $this->caldavBackend instanceof SchedulingSupport) {
146
			return new Inbox($this->caldavBackend, $this->principalInfo['uri']);
147
		}
148
		if ($name === 'outbox' && $this->caldavBackend instanceof SchedulingSupport) {
149
			return new Outbox($this->config, $this->principalInfo['uri']);
150
		}
151
		if ($name === 'notifications' && $this->caldavBackend instanceof NotificationSupport) {
152
			return new \Sabre\CalDAV\Notifications\Collection($this->caldavBackend, $this->principalInfo['uri']);
153
		}
154
		if ($name === TrashbinHome::NAME && $this->caldavBackend instanceof CalDavBackend) {
155
			return new TrashbinHome($this->caldavBackend, $this->principalInfo);
156
		}
157
158
		// Calendars
159
		foreach ($this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']) as $calendar) {
160
			if ($calendar['uri'] === $name) {
161
				return new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config);
162
			}
163
		}
164
165
		if ($this->caldavBackend instanceof SubscriptionSupport) {
166
			foreach ($this->caldavBackend->getSubscriptionsForUser($this->principalInfo['uri']) as $subscription) {
167
				if ($subscription['uri'] === $name) {
168
					if ($this->returnCachedSubscriptions) {
169
						return new CachedSubscription($this->caldavBackend, $subscription);
170
					}
171
172
					return new Subscription($this->caldavBackend, $subscription);
173
				}
174
			}
175
		}
176
177
		if (ExternalCalendar::isAppGeneratedCalendar($name)) {
178
			[$appId, $calendarUri] = ExternalCalendar::splitAppGeneratedCalendarUri($name);
179
180
			foreach ($this->pluginManager->getCalendarPlugins() as $calendarPlugin) {
181
				/** @var ICalendarProvider $calendarPlugin */
182
				if ($calendarPlugin->getAppId() !== $appId) {
183
					continue;
184
				}
185
186
				if ($calendarPlugin->hasCalendarInCalendarHome($this->principalInfo['uri'], $calendarUri)) {
187
					return $calendarPlugin->getCalendarInCalendarHome($this->principalInfo['uri'], $calendarUri);
188
				}
189
			}
190
		}
191
192
		throw new NotFound('Node with name \'' . $name . '\' could not be found');
193
	}
194
195
	/**
196
	 * @param array $filters
197
	 * @param integer|null $limit
198
	 * @param integer|null $offset
199
	 */
200
	public function calendarSearch(array $filters, $limit = null, $offset = null) {
201
		$principalUri = $this->principalInfo['uri'];
202
		return $this->caldavBackend->calendarSearch($principalUri, $filters, $limit, $offset);
0 ignored issues
show
Bug introduced by
The method calendarSearch() does not exist on Sabre\CalDAV\Backend\BackendInterface. Did you maybe mean calendarQuery()? ( Ignorable by Annotation )

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

202
		return $this->caldavBackend->/** @scrutinizer ignore-call */ calendarSearch($principalUri, $filters, $limit, $offset);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
203
	}
204
205
206
	public function enableCachedSubscriptionsForThisRequest() {
207
		$this->returnCachedSubscriptions = true;
208
	}
209
}
210