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

ExternalAddressBook::setName()   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 1
dl 0
loc 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace OCA\DAV\CardDAV\Integration;
6
7
use Sabre\CardDAV\IAddressBook;
8
use Sabre\DAV;
9
10
/**
11
 * @since 19.0.0
12
 */
13
abstract class ExternalAddressBook implements IAddressBook, DAV\IProperties {
14
15
	/** @var string */
16
	private const PREFIX = 'app-generated';
17
18
	/**
19
	 * @var string
20
	 *
21
	 * Double dash is a valid delimiter,
22
	 * because it will always split the URIs correctly:
23
	 * - our prefix contains only one dash and won't be split
24
	 * - appIds are not allowed to contain dashes as per spec:
25
	 * > must contain only lowercase ASCII characters and underscore
26
	 * - explode has a limit of three, so even if the app-generated
27
	 *   URI has double dashes, it won't be split
28
	 */
29
	private const DELIMITER = '--';
30
31
	/** @var string */
32
	private $appId;
33
34
	/** @var string */
35
	private $uri;
36
37
	/**
38
	 * @param string $appId
39
	 * @param string $uri
40
	 */
41
	public function __construct(string $appId, string $uri) {
42
		$this->appId = $appId;
43
		$this->uri = $uri;
44
	}
45
46
	/**
47
	 * @inheritDoc
48
	 */
49
	final public function getName() {
50
		return implode(self::DELIMITER, [
51
			self::PREFIX,
52
			$this->appId,
53
			$this->uri,
54
		]);
55
	}
56
57
	/**
58
	 * @inheritDoc
59
	 */
60
	final public function setName($name) {
61
		throw new DAV\Exception\MethodNotAllowed('Renaming address books is not yet supported');
62
	}
63
64
	/**
65
	 * @inheritDoc
66
	 */
67
	final public function createDirectory($name) {
68
		throw new DAV\Exception\MethodNotAllowed('Creating collections in address book objects is not allowed');
69
70
	}
71
72
	/**
73
	 * Checks whether the address book uri is app-generated
74
	 *
75
	 * @param string $uri
76
	 *
77
	 * @return bool
78
	 */
79
	public static function isAppGeneratedAddressBook(string $uri): bool {
80
		return strpos($uri, self::PREFIX) === 0 && substr_count($uri, self::DELIMITER) >= 2;
81
	}
82
83
	/**
84
	 * Splits an app-generated uri into appId and uri
85
	 *
86
	 * @param string $uri
87
	 *
88
	 * @return array
89
	 */
90
	public static function splitAppGeneratedAddressBookUri(string $uri): array {
91
		$array = array_slice(explode(self::DELIMITER, $uri, 3), 1);
92
		// Check the array has expected amount of elements
93
		// and none of them is an empty string
94
		if (\count($array) !== 2 || \in_array('', $array, true)) {
95
			throw new \InvalidArgumentException('Provided address book uri was not app-generated');
96
		}
97
98
		return $array;
99
	}
100
101
	/**
102
	 * Checks whether a address book name the user wants to create violates
103
	 * the reserved name for URIs
104
	 *
105
	 * @param string $uri
106
	 *
107
	 * @return bool
108
	 */
109
	public static function doesViolateReservedName(string $uri): bool {
110
		return strpos($uri, self::PREFIX) === 0;
111
	}
112
113
}
114