Completed
Push — master ( 97a159...fc6227 )
by Maxence
29s
created

Contacts::generateDataArray()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 21
Code Lines 12

Duplication

Lines 3
Ratio 14.29 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 3
loc 21
rs 9.0534
cc 4
eloc 12
nc 8
nop 1
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Circles\Search;
28
29
use OCA\Circles\ISearch;
30
use OCA\Circles\Model\Member;
31
use OCA\Circles\Model\SearchResult;
32
33
class Contacts implements ISearch {
34
35
	/**
36
	 * {@inheritdoc}
37
	 */
38
	public function search($search) {
39
40
		$result = [];
41
		$contactManager = \OC::$server->getContactsManager();
42
43
		// Add 'ADR' to search also in the address
44
		$contacts = $contactManager->search($search, ['FN', 'ORG', 'EMAIL']);
45
		foreach ($contacts as $contact) {
46
			if (key_exists('isLocalSystemBook', $contact)
47
				&& $contact['isLocalSystemBook'] === true) {
48
				continue;
49
			}
50
51
			$data = $this->generateDataArray($contact);
52
			$result[] = new SearchResult($contact['UID'], Member::TYPE_CONTACT, $data);
53
		}
54
55
		return $result;
56
	}
57
58
59
	/**
60
	 * @param array $contact
61
	 *
62
	 * @return array
63
	 */
64
	private function generateDataArray($contact) {
65
		$data = [
66
			'display'      => '',
67
			'email'        => '',
68
			'organization' => ''
69
		];
70
71 View Code Duplication
		if (key_exists('EMAIL', $contact)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
			$data['display'] = $data['email'] = $contact['EMAIL'];
73
		}
74
75
		if (key_exists('FN', $contact)) {
76
			$data['display'] = $contact['FN'];
77
		}
78
79
		if (key_exists('ORG', $contact)) {
80
			$data['organization'] = $contact['ORG'];
81
		}
82
83
		return $data;
84
	}
85
}
86
87