Passed
Push — master ( 25fe32...0fb74a )
by Morris
13:02 queued 10s
created

CalendarHome   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 124
rs 10
c 0
b 0
f 0
wmc 26

7 Methods

Rating   Name   Duplication   Size   Complexity  
A calendarSearch() 0 3 1
A createExtendedCollection() 0 8 2
A getCalDAVBackend() 0 2 1
C getChild() 0 33 13
A enableCachedSubscriptionsForThisRequest() 0 2 1
A __construct() 0 4 1
B getChildren() 0 29 7
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Georg Ehrke <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Lukas Reschke <[email protected]>
8
 * @author Thomas Müller <[email protected]>
9
 *
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
namespace OCA\DAV\CalDAV;
26
27
use Sabre\CalDAV\Backend\BackendInterface;
28
use Sabre\CalDAV\Backend\NotificationSupport;
29
use Sabre\CalDAV\Backend\SchedulingSupport;
30
use Sabre\CalDAV\Backend\SubscriptionSupport;
31
use Sabre\CalDAV\Schedule\Inbox;
32
use Sabre\CalDAV\Subscriptions\Subscription;
33
use Sabre\DAV\Exception\NotFound;
34
use Sabre\DAV\Exception\MethodNotAllowed;
35
use Sabre\DAV\MkCol;
36
37
class CalendarHome extends \Sabre\CalDAV\CalendarHome {
38
39
	/** @var \OCP\IL10N */
40
	private $l10n;
41
42
	/** @var \OCP\IConfig */
43
	private $config;
44
45
	/** @var bool */
46
	private $returnCachedSubscriptions=false;
47
48
	public function __construct(BackendInterface $caldavBackend, $principalInfo) {
49
		parent::__construct($caldavBackend, $principalInfo);
50
		$this->l10n = \OC::$server->getL10N('dav');
51
		$this->config = \OC::$server->getConfig();
52
	}
53
54
	/**
55
	 * @return BackendInterface
56
	 */
57
	public function getCalDAVBackend() {
58
		return $this->caldavBackend;
59
	}
60
61
	/**
62
	 * @inheritdoc
63
	 */
64
	function createExtendedCollection($name, MkCol $mkCol) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
65
		$reservedNames = [BirthdayService::BIRTHDAY_CALENDAR_URI];
66
67
		if (in_array($name, $reservedNames)) {
68
			throw new MethodNotAllowed('The resource you tried to create has a reserved name');
69
		}
70
71
		parent::createExtendedCollection($name, $mkCol);
72
	}
73
74
	/**
75
	 * @inheritdoc
76
	 */
77
	function getChildren() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
78
		$calendars = $this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']);
79
		$objects = [];
80
		foreach ($calendars as $calendar) {
81
			$objects[] = new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config);
82
		}
83
84
		if ($this->caldavBackend instanceof SchedulingSupport) {
85
			$objects[] = new Inbox($this->caldavBackend, $this->principalInfo['uri']);
86
			$objects[] = new Outbox($this->config, $this->principalInfo['uri']);
87
		}
88
89
		// We're adding a notifications node, if it's supported by the backend.
90
		if ($this->caldavBackend instanceof NotificationSupport) {
91
			$objects[] = new \Sabre\CalDAV\Notifications\Collection($this->caldavBackend, $this->principalInfo['uri']);
92
		}
93
94
		// If the backend supports subscriptions, we'll add those as well,
95
		if ($this->caldavBackend instanceof SubscriptionSupport) {
96
			foreach ($this->caldavBackend->getSubscriptionsForUser($this->principalInfo['uri']) as $subscription) {
97
				if ($this->returnCachedSubscriptions) {
98
					$objects[] = new CachedSubscription($this->caldavBackend, $subscription);
99
				} else {
100
					$objects[] = new Subscription($this->caldavBackend, $subscription);
101
				}
102
			}
103
		}
104
105
		return $objects;
106
	}
107
108
	/**
109
	 * @inheritdoc
110
	 */
111
	function getChild($name) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
112
		// Special nodes
113
		if ($name === 'inbox' && $this->caldavBackend instanceof SchedulingSupport) {
114
			return new Inbox($this->caldavBackend, $this->principalInfo['uri']);
115
		}
116
		if ($name === 'outbox' && $this->caldavBackend instanceof SchedulingSupport) {
117
			return new Outbox($this->config, $this->principalInfo['uri']);
118
		}
119
		if ($name === 'notifications' && $this->caldavBackend instanceof NotificationSupport) {
120
			return new \Sabre\CalDAv\Notifications\Collection($this->caldavBackend, $this->principalInfo['uri']);
121
		}
122
123
		// Calendars
124
		foreach ($this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']) as $calendar) {
125
			if ($calendar['uri'] === $name) {
126
				return new Calendar($this->caldavBackend, $calendar, $this->l10n, $this->config);
127
			}
128
		}
129
130
		if ($this->caldavBackend instanceof SubscriptionSupport) {
131
			foreach ($this->caldavBackend->getSubscriptionsForUser($this->principalInfo['uri']) as $subscription) {
132
				if ($subscription['uri'] === $name) {
133
					if ($this->returnCachedSubscriptions) {
134
						return new CachedSubscription($this->caldavBackend, $subscription);
135
					}
136
137
					return new Subscription($this->caldavBackend, $subscription);
138
				}
139
			}
140
141
		}
142
143
		throw new NotFound('Node with name \'' . $name . '\' could not be found');
144
	}
145
146
	/**
147
	 * @param array $filters
148
	 * @param integer|null $limit
149
	 * @param integer|null $offset
150
	 */
151
	function calendarSearch(array $filters, $limit=null, $offset=null) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
152
		$principalUri = $this->principalInfo['uri'];
153
		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

153
		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...
154
	}
155
156
	/**
157
	 *
158
	 */
159
	public function enableCachedSubscriptionsForThisRequest() {
160
		$this->returnCachedSubscriptions = true;
161
	}
162
}
163