Completed
Push — master ( 2493d7...51beeb )
by Maxence
03:12 queued 12s
created

ContactService::getAddressBoxById()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
6
/**
7
 * Circles - Bring cloud-users closer together.
8
 *
9
 * This file is licensed under the Affero General Public License version 3 or
10
 * later. See the COPYING file.
11
 *
12
 * @author Maxence Lange <[email protected]>
13
 * @copyright 2021
14
 * @license GNU AGPL version 3 or any later version
15
 *
16
 * This program is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License as
18
 * published by the Free Software Foundation, either version 3 of the
19
 * License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
 *
29
 */
30
31
32
namespace OCA\Circles\Service;
33
34
35
use ArtificialOwl\MySmallPhpTools\Traits\Nextcloud\nc22\TNC22Logger;
36
use ArtificialOwl\MySmallPhpTools\Traits\TArrayTools;
37
use ArtificialOwl\MySmallPhpTools\Traits\TStringTools;
38
use OC;
39
use OCA\Circles\Exceptions\ContactAddressBookNotFoundException;
40
use OCA\Circles\Exceptions\ContactFormatException;
41
use OCA\Circles\Exceptions\ContactNotFoundException;
42
use OCA\DAV\CardDAV\ContactsManager;
43
use OCP\Contacts\IManager;
44
use OCP\IAddressBook;
45
use OCP\IURLGenerator;
46
47
48
/**
49
 * Class ContactService
50
 *
51
 * @package OCA\Circles\Service
52
 */
53
class ContactService {
54
55
56
	use TArrayTools;
57
	use TStringTools;
58
	use TNC22Logger;
59
60
61
	/** @var IURLGenerator */
62
	private $urlGenerator;
63
64
	/** @var ConfigService */
65
	private $configService;
66
67
68
	/**
69
	 * ContactService constructor.
70
	 *
71
	 * @param IURLGenerator $urlGenerator
72
	 * @param ConfigService $configService
73
	 */
74
	public function __construct(IURLGenerator $urlGenerator, ConfigService $configService) {
75
		$this->urlGenerator = $urlGenerator;
76
		$this->configService = $configService;
77
	}
78
79
80
	/**
81
	 * @param string $contactPath
82
	 *
83
	 * @return string
84
	 * @throws ContactAddressBookNotFoundException
85
	 * @throws ContactFormatException
86
	 * @throws ContactNotFoundException
87
	 */
88
	public function getDisplayName(string $contactPath): string {
89
		$contact = $this->getContact($contactPath);
90
91
		if ($this->get('FN', $contact) !== '') {
92
			return $this->get('FN', $contact);
93
		}
94
95
		if ($this->get('EMAIL', $contact) !== '') {
96
			return $this->get('EMAIL', $contact);
97
		}
98
99
		if (!empty($this->getArray('EMAIL', $contact))) {
100
			return $this->getArray('EMAIL', $contact)[0];
101
		}
102
103
		// TODO: no idea if this situation might exists or if displaying the full contactPath is safe, so md5()
104
		return md5($contactPath);
105
	}
106
107
108
	/**
109
	 * @param string $contactPath
110
	 *
111
	 * @return array
112
	 * @throws ContactAddressBookNotFoundException
113
	 * @throws ContactFormatException
114
	 * @throws ContactNotFoundException
115
	 */
116
	public function getMailAddresses(string $contactPath): array {
117
		$c = $this->getContact($contactPath);
118
119
		return ($this->get('EMAIL', $c) === '') ? [$this->get('EMAIL', $c)] : $this->getArray('EMAIL', $c);
120
	}
121
122
123
	/**
124
	 * @throws ContactFormatException
125
	 * @throws ContactNotFoundException
126
	 * @throws ContactAddressBookNotFoundException
127
	 */
128
	private function getContact(string $contactPath): array {
129
		[$userId, $addressBookUri, $contactId] = explode('/', $contactPath, 3);
0 ignored issues
show
Bug introduced by
The variable $userId does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $addressBookUri does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $contactId does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
130
131
		if ($userId === ''
132
			|| $contactId === ''
133
			|| $addressBookUri === ''
134
			|| is_null($contactId)
135
		) {
136
			throw new ContactFormatException('issue with contact format USERID/ADDRESSBOOK/CONTACTID');
137
		}
138
139
		$contactsManager = OC::$server->get(ContactsManager::class);
140
		$cm = OC::$server->get(IManager::class);
141
		$contactsManager->setupContactsProvider($cm, $userId, $this->urlGenerator);
142
143
		$addressBook = $this->getAddressBook($cm, $addressBookUri);
144
		$contacts = $addressBook->search(
145
			$contactId, ['UID'],
146
			[
147
				'types'             => false,
148
				'escape_like_param' => false
149
			]
150
		);
151
152
		if (sizeof($contacts) !== 1) {
153
			throw new ContactNotFoundException();
154
		}
155
156
		return $contacts[0];
157
	}
158
159
160
	/**
161
	 * @param IManager $cm
162
	 * @param string $addressBookUri
163
	 *
164
	 * @return IAddressBook
165
	 * @throws ContactAddressBookNotFoundException
166
	 */
167
	private function getAddressBook(IManager $cm, string $addressBookUri): IAddressBook {
168
		foreach ($cm->getUserAddressBooks() as $addressBook) {
169
			if ($addressBook->getUri() === $addressBookUri) {
170
				return $addressBook;
171
			}
172
		}
173
174
		throw new ContactAddressBookNotFoundException();
175
	}
176
177
178
	/**
179
	 * @param IManager $cm
180
	 * @param string $addressBookKey
181
	 *
182
	 * @return IAddressBook
183
	 * @throws ContactAddressBookNotFoundException
184
	 */
185
	public function getAddressBoxById(IManager $cm, string $addressBookKey): IAddressBook {
186
		foreach ($cm->getUserAddressBooks() as $addressBook) {
187
			if ($addressBook->getKey() === $addressBookKey) {
188
				return $addressBook;
189
			}
190
		}
191
192
		throw new ContactAddressBookNotFoundException();
193
	}
194
195
}
196
197