Completed
Push — master ( aedd1f...4456fa )
by Lukas
06:45
created

Plugin::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
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
	 * Returns a list of addresses that are associated with a principal.
50
	 *
51
	 * @param string $principal
52
	 * @return array
53
	 */
54
	protected function getAddressesForPrincipal($principal) {
55
		$result = parent::getAddressesForPrincipal($principal);
56
57
		if ($result === null) {
58
			$result = [];
59
		}
60
61
		return $result;
62
	}
63
64
	/**
65
	 * Always use the personal calendar as target for scheduled events
66
	 *
67
	 * @param PropFind $propFind
68
	 * @param INode $node
69
	 * @return void
70
	 */
71
	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...
72
		if ($node instanceof IPrincipal) {
0 ignored issues
show
Bug introduced by
The class Sabre\DAVACL\IPrincipal does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
73
			$propFind->handle('{' . self::NS_CALDAV . '}schedule-default-calendar-URL', function() use ($node) {
74
				/** @var \OCA\DAV\CalDAV\Plugin $caldavPlugin */
75
				$caldavPlugin = $this->server->getPlugin('caldav');
76
				$principalUrl = $node->getPrincipalUrl();
77
78
				$calendarHomePath = $caldavPlugin->getCalendarHomeForPrincipal($principalUrl);
79
80
				if (!$calendarHomePath) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $calendarHomePath of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
81
					return null;
82
				}
83
84
				/** @var CalendarHome $calendarHome */
85
				$calendarHome = $this->server->tree->getNodeForPath($calendarHomePath);
86
				if (!$calendarHome->childExists(CalDavBackend::PERSONAL_CALENDAR_URI)) {
87
					$calendarHome->getCalDAVBackend()->createCalendar($principalUrl, CalDavBackend::PERSONAL_CALENDAR_URI, [
88
						'{DAV:}displayname' => CalDavBackend::PERSONAL_CALENDAR_NAME,
89
					]);
90
				}
91
92
				$result = $this->server->getPropertiesForPath($calendarHomePath . '/' . CalDavBackend::PERSONAL_CALENDAR_URI, [], 1);
93
				if (empty($result)) {
94
					return null;
95
				}
96
97
				return new LocalHref($result[0]['href']);
98
			});
99
		}
100
	}
101
}
102