Completed
Push — master ( edd944...d89c76 )
by Lukas
146:32 queued 124:11
created

ContactsStore::contactArrayToEntry()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 14
nc 16
nop 1
dl 0
loc 28
rs 6.7272
c 0
b 0
f 0
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 IUser $user
64
	 * @param integer $shareType
65
	 * @param string $shareWith
66
	 * @return IEntry|null
67
	 */
68
	public function findOne(IUser $user, $shareType, $shareWith) {
69
		switch($shareType) {
70
			case 0:
71
			case 6:
72
				$filter = ['UID'];
73
				break;
74
			case 4:
75
				$filter = ['EMAIL'];
76
				break;
77
			default:
78
				return null;
79
		}
80
81
		$userId = $user->getUID();
82
		$allContacts = $this->contactsManager->search($shareWith, $filter);
83
		$contacts = array_filter($allContacts, function($contact) use ($userId) {
84
			return $contact['UID'] !== $userId;
85
		});
86
		$match = null;
87
88
		foreach ($contacts as $contact) {
89
			if ($shareType === 4 && isset($contact['EMAIL'])) {
90
				if (in_array($shareWith, $contact['EMAIL'])) {
91
					$match = $contact;
92
					break;
93
				}
94
			}
95
			if ($shareType === 0 || $shareType === 6) {
96
				if ($contact['UID'] === $shareWith && $contact['isLocalSystemBook'] === true) {
97
					$match = $contact;
98
					break;
99
				}
100
			}
101
		}
102
103
		return $match ? $this->contactArrayToEntry($match) : null;
104
	}
105
106
	/**
107
	 * @param array $contact
108
	 * @return Entry
109
	 */
110
	private function contactArrayToEntry(array $contact) {
111
		$entry = new Entry();
112
113
		if (isset($contact['id'])) {
114
			$entry->setId($contact['id']);
115
		}
116
117
		if (isset($contact['FN'])) {
118
			$entry->setFullName($contact['FN']);
119
		}
120
121
		$avatarPrefix = "VALUE=uri:";
122
		if (isset($contact['PHOTO']) && strpos($contact['PHOTO'], $avatarPrefix) === 0) {
123
			$entry->setAvatar(substr($contact['PHOTO'], strlen($avatarPrefix)));
124
		}
125
126
		if (isset($contact['EMAIL'])) {
127
			foreach ($contact['EMAIL'] as $email) {
128
				$entry->addEMailAddress($email);
129
			}
130
		}
131
132
		// Attach all other properties to the entry too because some
133
		// providers might make use of it.
134
		$entry->setProperties($contact);
135
136
		return $entry;
137
	}
138
139
}
140