Completed
Push — stable10 ( 782c31...5fae9e )
by Roeland
33:52 queued 20:31
created

AddressBookImpl::vCard2Array()   C

Complexity

Conditions 11
Paths 16

Size

Total Lines 49
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 30
c 1
b 0
f 0
nc 16
nop 2
dl 0
loc 49
rs 5.2653

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Björn Schießle <[email protected]>
6
 * @author Georg Ehrke <[email protected]>
7
 * @author Thomas Müller <[email protected]>
8
 *
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OCA\DAV\CardDAV;
26
27
use OCP\Constants;
28
use OCP\IAddressBook;
29
use OCP\IURLGenerator;
30
use Sabre\VObject\Component\VCard;
31
use Sabre\VObject\Property;
32
use Sabre\VObject\Property\Text;
33
use Sabre\VObject\Reader;
34
use Sabre\VObject\UUIDUtil;
35
36
class AddressBookImpl implements IAddressBook {
37
38
	/** @var CardDavBackend */
39
	private $backend;
40
41
	/** @var array */
42
	private $addressBookInfo;
43
44
	/** @var AddressBook */
45
	private $addressBook;
46
47
	/** @var IURLGenerator */
48
	private $urlGenerator;
49
50
	/**
51
	 * AddressBookImpl constructor.
52
	 *
53
	 * @param AddressBook $addressBook
54
	 * @param array $addressBookInfo
55
	 * @param CardDavBackend $backend
56
	 * @param IUrlGenerator $urlGenerator
57
	 */
58
	public function __construct(
59
			AddressBook $addressBook,
60
			array $addressBookInfo,
61
			CardDavBackend $backend,
62
			IURLGenerator $urlGenerator) {
63
64
		$this->addressBook = $addressBook;
65
		$this->addressBookInfo = $addressBookInfo;
66
		$this->backend = $backend;
67
		$this->urlGenerator = $urlGenerator;
68
	}
69
70
	/**
71
	 * @return string defining the technical unique key
72
	 * @since 5.0.0
73
	 */
74
	public function getKey() {
75
		return $this->addressBookInfo['id'];
76
	}
77
78
	/**
79
	 * In comparison to getKey() this function returns a human readable (maybe translated) name
80
	 *
81
	 * @return mixed
82
	 * @since 5.0.0
83
	 */
84
	public function getDisplayName() {
85
		return $this->addressBookInfo['{DAV:}displayname'];
86
	}
87
88
	/**
89
	 * @param string $pattern which should match within the $searchProperties
90
	 * @param array $searchProperties defines the properties within the query pattern should match
91
	 * @param array $options - for future use. One should always have options!
92
	 * @return array an array of contacts which are arrays of key-value-pairs
93
	 * @since 5.0.0
94
	 */
95
	public function search($pattern, $searchProperties, $options) {
96
		$results = $this->backend->search($this->getKey(), $pattern, $searchProperties);
97
98
		$vCards = [];
99
		foreach ($results as $result) {
100
			$vCards[] = $this->vCard2Array($result['uri'], $this->readCard($result['carddata']));
101
		}
102
103
		return $vCards;
104
	}
105
106
	/**
107
	 * @param array $properties this array if key-value-pairs defines a contact
108
	 * @return array an array representing the contact just created or updated
109
	 * @since 5.0.0
110
	 */
111
	public function createOrUpdate($properties) {
112
		$update = false;
113
		if (!isset($properties['URI'])) { // create a new contact
114
			$uid = $this->createUid();
115
			$uri = $uid . '.vcf';
116
			$vCard = $this->createEmptyVCard($uid);
117
		} else { // update existing contact
118
			$uri = $properties['URI'];
119
			$vCardData = $this->backend->getCard($this->getKey(), $uri);
120
			$vCard = $this->readCard($vCardData['carddata']);
121
			$update = true;
122
		}
123
124
		foreach ($properties as $key => $value) {
125
			$vCard->$key = $vCard->createProperty($key, $value);
126
		}
127
128
		if ($update) {
129
			$this->backend->updateCard($this->getKey(), $uri, $vCard->serialize());
130
		} else {
131
			$this->backend->createCard($this->getKey(), $uri, $vCard->serialize());
132
		}
133
134
		return $this->vCard2Array($uri, $vCard);
135
136
	}
137
138
	/**
139
	 * @return mixed
140
	 * @since 5.0.0
141
	 */
142
	public function getPermissions() {
143
		$permissions = $this->addressBook->getACL();
144
		$result = 0;
145
		foreach ($permissions as $permission) {
146
			switch($permission['privilege']) {
147
				case '{DAV:}read':
148
					$result |= Constants::PERMISSION_READ;
149
					break;
150
				case '{DAV:}write':
151
					$result |= Constants::PERMISSION_CREATE;
152
					$result |= Constants::PERMISSION_UPDATE;
153
					break;
154
				case '{DAV:}all':
155
					$result |= Constants::PERMISSION_ALL;
156
					break;
157
			}
158
		}
159
160
		return $result;
161
	}
162
163
	/**
164
	 * @param object $id the unique identifier to a contact
165
	 * @return bool successful or not
166
	 * @since 5.0.0
167
	 */
168
	public function delete($id) {
169
		$uri = $this->backend->getCardUri($id);
170
		return $this->backend->deleteCard($this->addressBookInfo['id'], $uri);
171
	}
172
173
	/**
174
	 * read vCard data into a vCard object
175
	 *
176
	 * @param string $cardData
177
	 * @return VCard
178
	 */
179
	protected function readCard($cardData) {
180
		return  Reader::read($cardData);
181
	}
182
183
	/**
184
	 * create UID for contact
185
	 *
186
	 * @return string
187
	 */
188
	protected function createUid() {
189
		do {
190
			$uid = $this->getUid();
191
			$contact = $this->backend->getContact($this->getKey(), $uid . '.vcf');
192
		} while (!empty($contact));
193
194
		return $uid;
195
	}
196
197
	/**
198
	 * getUid is only there for testing, use createUid instead
199
	 */
200
	protected function getUid() {
201
		return UUIDUtil::getUUID();
202
	}
203
204
	/**
205
	 * create empty vcard
206
	 *
207
	 * @param string $uid
208
	 * @return VCard
209
	 */
210
	protected function createEmptyVCard($uid) {
211
		$vCard = new VCard();
212
		$vCard->add(new Text($vCard, 'UID', $uid));
213
		return $vCard;
214
	}
215
216
	/**
217
	 * create array with all vCard properties
218
	 *
219
	 * @param string $uri
220
	 * @param VCard $vCard
221
	 * @return array
222
	 */
223
	protected function vCard2Array($uri, VCard $vCard) {
224
		$result = [
225
			'URI' => $uri,
226
		];
227
228
		foreach ($vCard->children as $property) {
229
			/** @var \Sabre\VObject\Property\Unknown $property */
230
			if ($property->name === 'PHOTO' && $property->getValueType() === 'BINARY') {
231
				$url = $this->urlGenerator->getAbsoluteURL(
232
					$this->urlGenerator->linkTo('', 'remote.php') . '/dav/');
233
				$url .= implode('/', [
234
					'addressbooks',
235
					substr($this->addressBookInfo['principaluri'], 11), //cut off 'principals/'
236
					$this->addressBookInfo['uri'],
237
					$uri
238
				]) . '?photo';
239
240
				$result['PHOTO'] = 'VALUE=uri:' . $url;
241
242
			} else if ($property->name === 'X-SOCIALPROFILE') {
243
				$type = $this->getTypeFromProperty($property);
244
245
				// Type is the social network, when it's empty we don't need this.
246
				if ($type !== null) {
247
					if (!isset($result[$property->name])) {
248
						$result[$property->name] = [];
249
					}
250
					$result[$property->name][$type] = $property->getValue();
251
				}
252
253
			// The following properties can be set multiple times
254
			} else if (in_array($property->name, ['CLOUD', 'EMAIL', 'IMPP', 'TEL', 'URL'])) {
255
				if (!isset($result[$property->name])) {
256
					$result[$property->name] = [];
257
				}
258
259
				$result[$property->name][] = $property->getValue();
260
261
			} else {
262
				$result[$property->name] = $property->getValue();
263
			}
264
		}
265
266
		if ($this->addressBookInfo['principaluri'] === 'principals/system/system' &&
267
			$this->addressBookInfo['uri'] === 'system') {
268
			$result['isLocalSystemBook'] = true;
269
		}
270
		return $result;
271
	}
272
273
	/**
274
	 * Get the type of the current property
275
	 *
276
	 * @param Property $property
277
	 * @return null|string
278
	 */
279
	protected function getTypeFromProperty(Property $property) {
280
		$parameters = $property->parameters();
281
		// Type is the social network, when it's empty we don't need this.
282
		if (isset($parameters['TYPE'])) {
283
			/** @var \Sabre\VObject\Parameter $type */
284
			$type = $parameters['TYPE'];
285
			return $type->getValue();
286
		}
287
288
		return null;
289
	}
290
}
291