Completed
Pull Request — master (#3233)
by Jan-Christoph
12:25
created

ContactsStore::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * @copyright 2017 Christoph Wurst <[email protected]>
5
 *
6
 * @author 2017 Christoph Wurst <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
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
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OC\Contacts\ContactsMenu;
26
27
use OCP\Contacts\ContactsMenu\IEntry;
28
use OCP\Contacts\IManager;
29
use OCP\IUser;
30
31
class ContactsStore {
32
33
	/** @var IManager */
34
	private $contactsManager;
35
36
	/**
37
	 * @param IManager $contactsManager
38
	 */
39
	public function __construct(IManager $contactsManager) {
40
		$this->contactsManager = $contactsManager;
41
	}
42
43
	/**
44
	 * @param IUser $user
45
	 * @param string|null $filter
46
	 * @return IEntry[]
47
	 */
48
	public function getContacts(IUser $user, $filter) {
49
		$allContacts = $this->contactsManager->search($filter ?: '', [
50
			'FN',
51
		]);
52
53
		$self = $user->getUID();
54
		$entries = array_map(function(array $contact) {
55
			return $this->contactArrayToEntry($contact);
56
		}, $allContacts);
57
		return array_filter($entries, function(IEntry $entry) use ($self) {
58
			return $entry->getProperty('UID') !== $self;
59
		});
60
	}
61
62
	/**
63
	 * @param array $contact
64
	 * @return Entry
65
	 */
66
	private function contactArrayToEntry(array $contact) {
67
		$entry = new Entry();
68
69
		if (isset($contact['id'])) {
70
			$entry->setId($contact['id']);
71
		}
72
73
		if (isset($contact['FN'])) {
74
			$entry->setFullName($contact['FN']);
75
		}
76
77
		$avatarPrefix = "VALUE=uri:";
78
		if (isset($contact['PHOTO']) && strpos($contact['PHOTO'], $avatarPrefix) === 0) {
79
			$entry->setAvatar(substr($contact['PHOTO'], strlen($avatarPrefix)));
80
		}
81
82
		if (isset($contact['EMAIL'])) {
83
			foreach ($contact['EMAIL'] as $email) {
84
				$entry->addEMailAddress($email);
85
			}
86
		}
87
88
		// Attach all other properties to the entry too because some
89
		// providers might make use of it.
90
		$entry->setProperties($contact);
91
92
		return $entry;
93
	}
94
95
}
96