Passed
Push — master ( 5f61fa...b1d4c3 )
by John
14:30
created

ContactsManager::getUserAddressBooks()   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
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bart Visscher <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Morris Jobke <[email protected]>
8
 * @author Robin McCorkell <[email protected]>
9
 * @author Thomas Müller <[email protected]>
10
 * @author Tobia De Koninck <[email protected]>
11
 *
12
 * @license AGPL-3.0
13
 *
14
 * This code is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License, version 3,
16
 * as published by the Free Software Foundation.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License, version 3,
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
25
 *
26
 */
27
28
namespace OC {
29
30
	class ContactsManager implements \OCP\Contacts\IManager {
31
32
		/**
33
		 * This function is used to search and find contacts within the users address books.
34
		 * In case $pattern is empty all contacts will be returned.
35
		 *
36
		 * @param string $pattern which should match within the $searchProperties
37
		 * @param array $searchProperties defines the properties within the query pattern should match
38
		 * @param array $options - for future use. One should always have options!
39
		 * @return array an array of contacts which are arrays of key-value-pairs
40
		 */
41
		public function search($pattern, $searchProperties = array(), $options = array()) {
42
			$this->loadAddressBooks();
43
			$result = array();
44
			foreach($this->addressBooks as $addressBook) {
45
				$r = $addressBook->search($pattern, $searchProperties, $options);
46
				$contacts = array();
47
				foreach($r as $c){
48
					$c['addressbook-key'] = $addressBook->getKey();
49
					$contacts[] = $c;
50
				}
51
				$result = array_merge($result, $contacts);
52
			}
53
54
			return $result;
55
		}
56
57
		/**
58
		 * This function can be used to delete the contact identified by the given id
59
		 *
60
		 * @param object $id the unique identifier to a contact
61
		 * @param string $addressBookKey identifier of the address book in which the contact shall be deleted
62
		 * @return bool successful or not
63
		 */
64
		public function delete($id, $addressBookKey) {
65
			$addressBook = $this->getAddressBook($addressBookKey);
66
			if (!$addressBook) {
0 ignored issues
show
introduced by
$addressBook is of type OCP\IAddressBook, thus it always evaluated to true.
Loading history...
67
				return null;
68
			}
69
70
			if ($addressBook->getPermissions() & \OCP\Constants::PERMISSION_DELETE) {
71
				return $addressBook->delete($id);
72
			}
73
74
			return null;
75
		}
76
77
		/**
78
		 * This function is used to create a new contact if 'id' is not given or not present.
79
		 * Otherwise the contact will be updated by replacing the entire data set.
80
		 *
81
		 * @param array $properties this array if key-value-pairs defines a contact
82
		 * @param string $addressBookKey identifier of the address book in which the contact shall be created or updated
83
		 * @return array representing the contact just created or updated
84
		 */
85
		public function createOrUpdate($properties, $addressBookKey) {
86
			$addressBook = $this->getAddressBook($addressBookKey);
87
			if (!$addressBook) {
0 ignored issues
show
introduced by
$addressBook is of type OCP\IAddressBook, thus it always evaluated to true.
Loading history...
88
				return null;
89
			}
90
91
			if ($addressBook->getPermissions() & \OCP\Constants::PERMISSION_CREATE) {
92
				return $addressBook->createOrUpdate($properties);
93
			}
94
95
			return null;
96
		}
97
98
		/**
99
		 * Check if contacts are available (e.g. contacts app enabled)
100
		 *
101
		 * @return bool true if enabled, false if not
102
		 */
103
		public function isEnabled() {
104
			return !empty($this->addressBooks) || !empty($this->addressBookLoaders);
105
		}
106
107
		/**
108
		 * @param \OCP\IAddressBook $addressBook
109
		 */
110
		public function registerAddressBook(\OCP\IAddressBook $addressBook) {
111
			$this->addressBooks[$addressBook->getKey()] = $addressBook;
112
		}
113
114
		/**
115
		 * @param \OCP\IAddressBook $addressBook
116
		 */
117
		public function unregisterAddressBook(\OCP\IAddressBook $addressBook) {
118
			unset($this->addressBooks[$addressBook->getKey()]);
119
		}
120
121
		/**
122
		 * Return a list of the user's addressbooks display names
123
		 * ! The addressBook displayName are not unique, please use getUserAddressBooks
124
		 * 
125
		 * @return array
126
		 * @since 6.0.0
127
		 * @deprecated 16.0.0 - Use `$this->getUserAddressBooks()` instead
128
		 */
129
		public function getAddressBooks() {
130
			$this->loadAddressBooks();
131
			$result = array();
132
			foreach($this->addressBooks as $addressBook) {
133
				$result[$addressBook->getKey()] = $addressBook->getDisplayName();
134
			}
135
136
			return $result;
137
		}
138
139
		/**
140
		 * Return a list of the user's addressbooks
141
		 * 
142
		 * @return IAddressBook[]
143
		 * @since 16.0.0
144
		 */
145
		public function getUserAddressBooks(): Array {
146
			$this->loadAddressBooks();
147
			return $this->addressBooks;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->addressBooks returns the type OCP\IAddressBook[] which is incompatible with the documented return type OC\IAddressBook[].
Loading history...
148
		}
149
150
		/**
151
		 * removes all registered address book instances
152
		 */
153
		public function clear() {
154
			$this->addressBooks = array();
155
			$this->addressBookLoaders = array();
156
		}
157
158
		/**
159
		 * @var \OCP\IAddressBook[] which holds all registered address books
160
		 */
161
		private $addressBooks = array();
162
163
		/**
164
		 * @var \Closure[] to call to load/register address books
165
		 */
166
		private $addressBookLoaders = array();
167
168
		/**
169
		 * In order to improve lazy loading a closure can be registered which will be called in case
170
		 * address books are actually requested
171
		 *
172
		 * @param \Closure $callable
173
		 */
174
		public function register(\Closure $callable)
175
		{
176
			$this->addressBookLoaders[] = $callable;
177
		}
178
179
		/**
180
		 * Get (and load when needed) the address book for $key
181
		 *
182
		 * @param string $addressBookKey
183
		 * @return \OCP\IAddressBook
184
		 */
185
		protected function getAddressBook($addressBookKey)
186
		{
187
			$this->loadAddressBooks();
188
			if (!array_key_exists($addressBookKey, $this->addressBooks)) {
189
				return null;
190
			}
191
192
			return $this->addressBooks[$addressBookKey];
193
		}
194
195
		/**
196
		 * Load all address books registered with 'register'
197
		 */
198
		protected function loadAddressBooks()
199
		{
200
			foreach($this->addressBookLoaders as $callable) {
201
				$callable($this);
202
			}
203
			$this->addressBookLoaders = array();
204
		}
205
	}
206
}
207