Passed
Push — master ( 902adb...3cf321 )
by Christoph
10:44 queued 11s
created

AddressBook::getOwner()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright 2020 Christoph Wurst <[email protected]>
7
 *
8
 * @author 2020 Christoph Wurst <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 */
25
26
namespace OCA\ContactsInteraction;
27
28
use Exception;
29
use OCA\ContactsInteraction\AppInfo\Application;
30
use OCA\ContactsInteraction\Db\RecentContact;
31
use OCA\ContactsInteraction\Db\RecentContactMapper;
32
use OCA\DAV\CardDAV\Integration\ExternalAddressBook;
33
use OCA\DAV\DAV\Sharing\Plugin;
34
use OCP\AppFramework\Db\DoesNotExistException;
35
use OCP\IL10N;
36
use Sabre\DAV\Exception\NotFound;
37
use Sabre\DAV\Exception\NotImplemented;
38
use Sabre\DAV\PropPatch;
39
use Sabre\DAVACL\ACLTrait;
40
use Sabre\DAVACL\IACL;
41
42
class AddressBook extends ExternalAddressBook implements IACL {
43
44
	public const URI = 'recent';
45
46
	use ACLTrait;
47
48
	/** @var RecentContactMapper */
49
	private $mapper;
50
51
	/** @var IL10N */
52
	private $l10n;
53
54
	/** @var string */
55
	private $principalUri;
56
57
	public function __construct(RecentContactMapper $mapper,
58
								IL10N $l10n,
59
								string $principalUri) {
60
		parent::__construct(Application::APP_ID, self::URI);
61
62
		$this->mapper = $mapper;
63
		$this->l10n = $l10n;
64
		$this->principalUri = $principalUri;
65
	}
66
67
	/**
68
	 * @inheritDoc
69
	 */
70
	public function delete(): void {
71
		throw new Exception("This addressbook is immutable");
72
	}
73
74
	/**
75
	 * @inheritDoc
76
	 */
77
	function createFile($name, $data = 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...
78
		throw new Exception("This addressbook is immutable");
79
	}
80
81
	/**
82
	 * @inheritDoc
83
	 * @throws NotFound
84
	 */
85
	public function getChild($name) {
86
		try {
87
			return new Card(
88
				$this->mapper->find(
89
					$this->getUid(),
90
					(int)$name
91
				),
92
				$this->principalUri,
93
				$this->getACL()
94
			);
95
		} catch (DoesNotExistException $ex) {
96
			throw new NotFound("Contact does not exist: " . $ex->getMessage(), 0, $ex);
97
		}
98
	}
99
100
	/**
101
	 * @inheritDoc
102
	 */
103
	public function getChildren(): array {
104
		return array_map(
105
			function (RecentContact $contact) {
106
				return new Card(
107
					$contact,
108
					$this->principalUri,
109
					$this->getACL()
110
				);
111
			},
112
			$this->mapper->findAll($this->getUid())
113
		);
114
	}
115
116
	/**
117
	 * @inheritDoc
118
	 */
119
	public function childExists($name) {
120
		try {
121
			$this->mapper->find(
122
				$this->getUid(),
123
				(int)$name
124
			);
125
			return true;
126
		} catch (DoesNotExistException $e) {
127
			return false;
128
		}
129
	}
130
131
	/**
132
	 * @inheritDoc
133
	 */
134
	public function getLastModified() {
135
		throw new NotImplemented();
136
	}
137
138
	/**
139
	 * @inheritDoc
140
	 */
141
	public function propPatch(PropPatch $propPatch) {
142
		throw new Exception("This addressbook is immutable");
143
	}
144
145
	/**
146
	 * @inheritDoc
147
	 */
148
	public function getProperties($properties) {
149
		return [
150
			'principaluri' => $this->principalUri,
151
			'{DAV:}displayname' => $this->l10n->t('Recently contacted'),
152
			'{' . Plugin::NS_OWNCLOUD . '}read-only' => true,
153
		];
154
	}
155
156
	public function getOwner(): string {
157
		return $this->principalUri;
158
	}
159
160
	/**
161
	 * @inheritDoc
162
	 */
163
	public function getACL() {
164
		return [
165
			[
166
				'privilege' => '{DAV:}read',
167
				'principal' => $this->getOwner(),
168
				'protected' => true,
169
			],
170
		];
171
	}
172
173
	private function getUid(): string {
174
		list(, $uid) = \Sabre\Uri\split($this->principalUri);
175
		return $uid;
176
	}
177
178
}
179