Passed
Push — master ( 5a8c78...5b0dfd )
by Christoph
12:14 queued 11s
created

UserAddressBooks   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 78
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createExtendedCollection() 0 6 2
A getChildren() 0 20 5
A getACL() 0 12 2
A __construct() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2016, ownCloud, Inc.
7
 *
8
 * @author Joas Schilling <[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\CardDAV;
29
30
use OCA\DAV\AppInfo\PluginManager;
31
use OCA\DAV\CardDAV\Integration\ExternalAddressBook;
32
use OCP\IConfig;
33
use OCP\IL10N;
34
use Sabre\CardDAV\Backend;
35
use Sabre\DAV\Exception\MethodNotAllowed;
36
use Sabre\DAV\MkCol;
37
38
class UserAddressBooks extends \Sabre\CardDAV\AddressBookHome {
39
40
	/** @var IL10N */
41
	protected $l10n;
42
43
	/** @var IConfig */
44
	protected $config;
45
46
	/** @var PluginManager */
47
	private $pluginManager;
48
49
	public function __construct(Backend\BackendInterface $carddavBackend,
50
								string $principalUri,
51
								PluginManager $pluginManager) {
52
		parent::__construct($carddavBackend, $principalUri);
53
		$this->pluginManager = $pluginManager;
54
	}
55
56
	/**
57
	 * Returns a list of address books
58
	 *
59
	 * @return array
60
	 */
61
	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...
62
		if ($this->l10n === null) {
63
			$this->l10n = \OC::$server->getL10N('dav');
64
		}
65
		if ($this->config === null) {
66
			$this->config = \OC::$server->getConfig();
67
		}
68
69
		$addressBooks = $this->carddavBackend->getAddressBooksForUser($this->principalUri);
0 ignored issues
show
Bug introduced by
$this->principalUri of type array is incompatible with the type string expected by parameter $principalUri of Sabre\CardDAV\Backend\Ba...etAddressBooksForUser(). ( Ignorable by Annotation )

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

69
		$addressBooks = $this->carddavBackend->getAddressBooksForUser(/** @scrutinizer ignore-type */ $this->principalUri);
Loading history...
70
		$objects = array_map(function(array $addressBook) {
71
			if ($addressBook['principaluri'] === 'principals/system/system') {
72
				return new SystemAddressbook($this->carddavBackend, $addressBook, $this->l10n, $this->config);
73
			}
74
75
			return new AddressBook($this->carddavBackend, $addressBook, $this->l10n);
76
		}, $addressBooks);
77
		foreach ($this->pluginManager->getAddressBookPlugins() as $plugin) {
78
			$plugin->fetchAllForAddressBookHome($this->principalUri);
79
		}
80
		return $objects;
81
82
	}
83
84
	public function createExtendedCollection($name, MkCol $mkCol) {
85
		if (ExternalAddressBook::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
	 * Returns a list of ACE's for this node.
94
	 *
95
	 * Each ACE has the following properties:
96
	 *   * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
97
	 *     currently the only supported privileges
98
	 *   * 'principal', a url to the principal who owns the node
99
	 *   * 'protected' (optional), indicating that this ACE is not allowed to
100
	 *      be updated.
101
	 *
102
	 * @return array
103
	 */
104
	function getACL() {
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...
105
106
		$acl = parent::getACL();
107
		if ($this->principalUri === 'principals/system/system') {
0 ignored issues
show
introduced by
The condition $this->principalUri === ...incipals/system/system' is always false.
Loading history...
108
			$acl[] = [
109
				'privilege' => '{DAV:}read',
110
				'principal' => '{DAV:}authenticated',
111
				'protected' => true,
112
			];
113
		}
114
115
		return $acl;
116
	}
117
118
}
119