Completed
Pull Request — master (#590)
by
unknown
32:12 queued 12:16
created

getPreferredLanguageFromUidOrDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
eloc 2
nc 1
nop 2
crap 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\IConfig;
28
use OCP\IRequest;
29
use OCP\L10N\IFactory;
30
31
class ContactController extends Controller {
32
33
	/**
34
	 * API for contacts api
35
	 * @var IManager
36
	 */
37
	private $contacts;
38
39
	/**
40
	 * @var IConfig
41
	 */
42
	private $config;
43 2
44 2
	/**
45 2
	 * @var IFactory
46 2
	 */
47
	private $l10nFactory;
48
49
50
	/**
51
	 * @param string $appName
52
	 * @param IRequest $request an instance of the request
53
	 * @param IManager $contacts
54
	 * @param IConfig $config
55 1
	 * @param IFactory $l10nFactory
56 1
	 */
57
	public function __construct($appName, IRequest $request, IManager $contacts, IConfig $config, IFactory $l10nFactory) {
58 1
		parent::__construct($appName, $request);
59 1
		$this->contacts = $contacts;
60 1
		$this->config = $config;
61 1
		$this->l10nFactory = $l10nFactory;
62
	}
63
64 1
65 1
	/**
66
	 * @param string $location
67
	 * @return JSONResponse
68
	 *
69 1
	 * @NoAdminRequired
70 1
	 */
71 1
	public function searchLocation($location) {
72 1
		$result = $this->contacts->search($location, ['FN', 'ADR']);
73 1
74
		$contacts = [];
75
		foreach ($result as $r) {
76
			if (!isset($r['ADR'])) {
77
				continue;
78 1
			}
79
80
			$name = $this->getNameFromContact($r);
81
			if (is_string($r['ADR'])) {
82
				$r['ADR'] = [$r['ADR']];
83
			}
84
85
			foreach ($r['ADR'] as $address) {
86
				$address = trim(preg_replace("/\n+/", "\n", str_replace(';', "\n", $address)));
87
				$contacts[] = [
88 1
					'label' => $address,
89 1
					'name' => $name
90
				];
91 1
			}
92 1
		}
93 1
94 1
		return new JSONResponse($contacts);
95
	}
96
97 1
98 1
	/**
99
	 * @param string $search
100
	 * @return JSONResponse
101
	 *
102 1
	 * @NoAdminRequired
103 1
	 */
104 1
	public function searchAttendee($search) {
105
		$result = $this->contacts->search($search, ['FN', 'EMAIL']);
106
107
		$defaultLang = $this->l10nFactory->findLanguage();
108 1
		$contacts = [];
109
		foreach ($result as $r) {
110
			if (!isset($r['EMAIL'])) {
111
				continue;
112
			}
113
114
			$name = $this->getNameFromContact($r);
115
			if (is_string($r['EMAIL'])) {
116
				$r['EMAIL'] = [$r['EMAIL']];
117
			}
118 2
119 2
			$lang = $this->getPreferredLanguageFromUidOrDefault($r['UID'], $defaultLang);
120 2
			$isLocalUser = isset($r['isLocalSystemBook']) && $r['isLocalSystemBook'];
0 ignored issues
show
Unused Code introduced by
$isLocalUser is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
121 2
122
			$contacts[] = [
123
					'email' => $r['EMAIL'],
124 2
					'name' => $name,
125
					'lang' => $lang
126
			];
127
		}
128
129
		return new JSONResponse($contacts);
130
	}
131
132
133
	/**
134
	 * Extract name from an array containing a contact's information
135
	 *
136
	 * @param array $r
137
	 * @return string
138
	 */
139
	private function getNameFromContact(array $r) {
140
		$name = '';
141
		if (isset($r['FN'])) {
142
			$name = $r['FN'];
143
		}
144
145
		return $name;
146
	}
147
148
	/**
149
	 * Returns the preferred language of a given user uid or $default instead
150
	 *
151
	 * @param string $uid
152
	 * @param string $default
153
	 * @return string
154
	 */
155
	private function getPreferredLanguageFromUidOrDefault($uid, $default) {
156
		return $this->config->getUserValue($uid, 'core', 'lang', $default);
157
	}
158
}
159