Completed
Push — master ( aa1898...1342f9 )
by
unknown
04:03
created

ContactController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 91.11%

Importance

Changes 0
Metric Value
dl 0
loc 102
rs 10
c 0
b 0
f 0
ccs 41
cts 45
cp 0.9111
wmc 13
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B searchLocation() 0 25 5
B searchAttendee() 0 24 5
A getNameFromContact() 0 8 2
1
<?php
2
/**
3
 * Calendar App
4
 *
5
 * @author Georg Ehrke
6
 * @copyright 2016 Georg Ehrke <[email protected]>
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
10
 * License as published by the Free Software Foundation; either
11
 * version 3 of the License, or any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public
19
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
namespace OCA\Calendar\Controller;
23
24
use OCP\AppFramework\Controller;
25
use OCP\AppFramework\Http\JSONResponse;
26
use OCP\Contacts\IManager;
27
use OCP\IRequest;
28
29
class ContactController extends Controller {
30
31
	/**
32
	 * API for contacts api
33
	 * @var IManager
34
	 */
35
	private $contacts;
36
37
38
	/**
39
	 * @param string $appName
40
	 * @param IRequest $request an instance of the request
41
	 * @param IManager $contacts
42
	 */
43 2
	public function __construct($appName, IRequest $request, IManager $contacts) {
44 2
		parent::__construct($appName, $request);
45 2
		$this->contacts = $contacts;
46 2
	}
47
48
49
	/**
50
	 * @param string $location
51
	 * @return JSONResponse
52
	 *
53
	 * @NoAdminRequired
54
	 * @NoCSRFRequired
55
	 */
56 1
	public function searchLocation($location) {
57 1
		$result = $this->contacts->search($location, ['FN', 'ADR']);
58
59 1
		$contacts = [];
60 1
		foreach ($result as $r) {
61 1
			if (!isset($r['ADR'])) {
62 1
				continue;
63
			}
64
65 1
			$name = $this->getNameFromContact($r);
66 1
			if (is_string($r['ADR'])) {
67
				$r['ADR'] = [$r['ADR']];
68
			}
69
70 1
			foreach ($r['ADR'] as $address) {
71 1
				$address = trim(preg_replace("/\n+/", "\n", str_replace(';', "\n", $address)));
72 1
				$contacts[] = [
73 1
					'label' => $address,
74
					'name' => $name
75 1
				];
76 1
			}
77 1
		}
78
79 1
		return new JSONResponse($contacts);
80
	}
81
82
83
	/**
84
	 * @param string $search
85
	 * @return JSONResponse
86
	 *
87
	 * @NoAdminRequired
88
	 * @NoCSRFRequired
89
	 */
90 1
	public function searchAttendee($search) {
91 1
		$result = $this->contacts->search($search, ['FN', 'EMAIL']);
92
93 1
		$contacts = [];
94 1
		foreach ($result as $r) {
95 1
			if (!isset($r['EMAIL'])) {
96 1
				continue;
97
			}
98
99 1
			$name = $this->getNameFromContact($r);
100 1
			if (is_string($r['EMAIL'])) {
101
				$r['EMAIL'] = [$r['EMAIL']];
102
			}
103
104 1
			foreach ($r['EMAIL'] as $email) {
105 1
				$contacts[] = [
106 1
					'email' => $email,
107
					'name' => $name
108 1
				];
109 1
			}
110 1
		}
111
112 1
		return new JSONResponse($contacts);
113
	}
114
115
116
	/**
117
	 * Extract name from an array containing a contact's information
118
	 *
119
	 * @param array $r
120
	 * @return string
121
	 */
122 2
	private function getNameFromContact(array $r) {
123 2
		$name = '';
124 2
		if (isset($r['FN'])) {
125 2
			$name = $r['FN'];
126 2
		}
127
128 2
		return $name;
129
	}
130
}
131