Passed
Push — master ( 3d306b...237a49 )
by Morris
12:39 queued 02:23
created

Plugin::scheduleReply()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, Roeland Jago Douma <[email protected]>
4
 * @copyright Copyright (c) 2016, Joas Schilling <[email protected]>
5
 *
6
 * @author Joas Schilling <[email protected]>
7
 * @author Roeland Jago Douma <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
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
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
namespace OCA\DAV\CalDAV\Schedule;
26
27
use OCA\DAV\CalDAV\CalDavBackend;
28
use OCA\DAV\CalDAV\CalendarHome;
29
use Sabre\DAV\INode;
30
use Sabre\DAV\PropFind;
31
use Sabre\DAV\Server;
32
use Sabre\DAV\Xml\Property\LocalHref;
33
use Sabre\DAVACL\IPrincipal;
34
35
class Plugin extends \Sabre\CalDAV\Schedule\Plugin {
36
37
	/**
38
	 * Initializes the plugin
39
	 *
40
	 * @param Server $server
41
	 * @return void
42
	 */
43
	function initialize(Server $server) {
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...
44
		parent::initialize($server);
45
		$server->on('propFind', [$this, 'propFindDefaultCalendarUrl'], 90);
46
	}
47
48
	/**
49
	 * This method handler is invoked during fetching of properties.
50
	 *
51
	 * We use this event to add calendar-auto-schedule-specific properties.
52
	 *
53
	 * @param PropFind $propFind
54
	 * @param INode $node
55
	 * @return void
56
	 */
57
	function propFind(PropFind $propFind, INode $node) {
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...
58
		// overwrite Sabre/Dav's implementation
59
		$propFind->handle('{' . self::NS_CALDAV . '}calendar-user-type', function() use ($node) {
60
			$calendarUserType = '{' . self::NS_CALDAV . '}calendar-user-type';
61
			$props = $node->getProperties([$calendarUserType]);
0 ignored issues
show
Bug introduced by
The method getProperties() does not exist on Sabre\DAV\INode. It seems like you code against a sub-type of Sabre\DAV\INode such as Sabre\DAV\IProperties or OCA\DAV\Comments\CommentNode or OCA\DAV\Provisioning\Apple\AppleProvisioningNode or Sabre\DAVACL\Principal or Sabre\CalDAV\Subscriptions\Subscription or Sabre\CardDAV\AddressBook or Sabre\CalDAV\Principal\User or Sabre\CalDAV\Subscriptions\ISubscription or OCA\DAV\Comments\EntityCollection or Sabre\CalDAV\Calendar or Sabre\CardDAV\AddressBook or Sabre\CalDAV\Subscriptions\Subscription or Sabre\CardDAV\AddressBook or Sabre\CalDAV\Calendar or Sabre\CardDAV\AddressBook or Sabre\CardDAV\AddressBook or Sabre\CalDAV\Calendar or Sabre\CalDAV\SharedCalendar or Sabre\CalDAV\Subscriptions\Subscription or Sabre\DAVACL\Principal or Sabre\CardDAV\AddressBook or Sabre\CalDAV\Calendar or Sabre\DAVACL\Principal or OCA\DAV\CalDAV\Calendar or OCA\DAV\CardDAV\AddressBook. ( Ignorable by Annotation )

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

61
			/** @scrutinizer ignore-call */ 
62
   $props = $node->getProperties([$calendarUserType]);
Loading history...
62
63
			if (isset($props[$calendarUserType])) {
64
				return $props[$calendarUserType];
65
			}
66
67
			return 'INDIVIDUAL';
68
		});
69
70
		parent::propFind($propFind, $node);
71
	}
72
73
	/**
74
	 * Returns a list of addresses that are associated with a principal.
75
	 *
76
	 * @param string $principal
77
	 * @return array
78
	 */
79
	protected function getAddressesForPrincipal($principal) {
80
		$result = parent::getAddressesForPrincipal($principal);
81
82
		if ($result === null) {
0 ignored issues
show
introduced by
The condition $result === null is always false.
Loading history...
83
			$result = [];
84
		}
85
86
		return $result;
87
	}
88
89
	/**
90
	 * Always use the personal calendar as target for scheduled events
91
	 *
92
	 * @param PropFind $propFind
93
	 * @param INode $node
94
	 * @return void
95
	 */
96
	function propFindDefaultCalendarUrl(PropFind $propFind, INode $node) {
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...
97
		if ($node instanceof IPrincipal) {
98
			$propFind->handle('{' . self::NS_CALDAV . '}schedule-default-calendar-URL', function() use ($node) {
99
				/** @var \OCA\DAV\CalDAV\Plugin $caldavPlugin */
100
				$caldavPlugin = $this->server->getPlugin('caldav');
101
				$principalUrl = $node->getPrincipalUrl();
102
103
				$calendarHomePath = $caldavPlugin->getCalendarHomeForPrincipal($principalUrl);
104
				if (!$calendarHomePath) {
105
					return null;
106
				}
107
108
				if (strpos($principalUrl, 'principals/users') === 0) {
109
					$uri = CalDavBackend::PERSONAL_CALENDAR_URI;
110
					$displayname = CalDavBackend::PERSONAL_CALENDAR_NAME;
111
				} elseif (strpos($principalUrl, 'principals/calendar-resources') === 0 ||
112
						  strpos($principalUrl, 'principals/calendar-rooms') === 0) {
113
					$uri = CalDavBackend::RESOURCE_BOOKING_CALENDAR_URI;
114
					$displayname = CalDavBackend::RESOURCE_BOOKING_CALENDAR_NAME;
115
				} else {
116
					// How did we end up here?
117
					// TODO - throw exception or just ignore?
118
					return null;
119
				}
120
121
				/** @var CalendarHome $calendarHome */
122
				$calendarHome = $this->server->tree->getNodeForPath($calendarHomePath);
123
				if (!$calendarHome->childExists($uri)) {
124
					$calendarHome->getCalDAVBackend()->createCalendar($principalUrl, $uri, [
125
						'{DAV:}displayname' => $displayname,
126
					]);
127
				}
128
129
				$result = $this->server->getPropertiesForPath($calendarHomePath . '/' . $uri, [], 1);
0 ignored issues
show
Deprecated Code introduced by
The function Sabre\DAV\Server::getPropertiesForPath() has been deprecated: Use getPropertiesIteratorForPath() instead (as it's more memory efficient) ( Ignorable by Annotation )

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

129
				$result = /** @scrutinizer ignore-deprecated */ $this->server->getPropertiesForPath($calendarHomePath . '/' . $uri, [], 1);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
130
				if (empty($result)) {
131
					return null;
132
				}
133
134
				return new LocalHref($result[0]['href']);
135
			});
136
		}
137
	}
138
}
139