Completed
Push — master ( 8177fd...1ce867 )
by Björn
12:48 queued 10s
created

LookupPlugin::search()   B

Complexity

Conditions 9
Paths 33

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 33
nop 4
dl 0
loc 54
rs 7.448
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Arthur Schiwon <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OC\Collaboration\Collaborators;
25
26
27
use OCP\Collaboration\Collaborators\ISearchPlugin;
28
use OCP\Collaboration\Collaborators\ISearchResult;
29
use OCP\Collaboration\Collaborators\SearchResultType;
30
use OCP\Federation\ICloudIdManager;
31
use OCP\Http\Client\IClientService;
32
use OCP\IConfig;
33
use OCP\ILogger;
34
use OCP\IUserSession;
35
use OCP\Share;
36
37
class LookupPlugin implements ISearchPlugin {
38
39
	/** @var IConfig */
40
	private $config;
41
	/** @var IClientService */
42
	private $clientService;
43
	/** @var string remote part of the current user's cloud id */
44
	private $currentUserRemote;
45
	/** @var ICloudIdManager */
46
	private $cloudIdManager;
47
	/** @var ILogger */
48
	private $logger;
49
50
	public function __construct(IConfig $config,
51
								IClientService $clientService,
52
								IUserSession $userSession,
53
								ICloudIdManager $cloudIdManager,
54
								ILogger $logger) {
55
		$this->config = $config;
56
		$this->clientService = $clientService;
57
		$this->cloudIdManager = $cloudIdManager;
58
		$currentUserCloudId = $userSession->getUser()->getCloudId();
59
		$this->currentUserRemote = $cloudIdManager->resolveCloudId($currentUserCloudId)->getRemote();
60
		$this->logger = $logger;
61
	}
62
63
	public function search($search, $limit, $offset, ISearchResult $searchResult) {
64
		$isGlobalScaleEnabled = $this->config->getSystemValue('gs.enabled', false);
65
		$isLookupServerEnabled = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'no') === 'yes';
66
		// if case of Global Scale we always search the lookup server
67
		if (!$isLookupServerEnabled && !$isGlobalScaleEnabled) {
68
			return false;
69
		}
70
71
		$lookupServerUrl = $this->config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com');
72
		$lookupServerUrl = rtrim($lookupServerUrl, '/');
73
		$result = [];
74
75
		try {
76
			$client = $this->clientService->newClient();
77
			$response = $client->get(
78
				$lookupServerUrl . '/users?search=' . urlencode($search),
79
				[
80
					'timeout' => 10,
81
					'connect_timeout' => 3,
82
				]
83
			);
84
85
			$body = json_decode($response->getBody(), true);
86
87
			foreach ($body as $lookup) {
88
				try {
89
					$remote = $this->cloudIdManager->resolveCloudId($lookup['federationId'])->getRemote();
90
				} catch (\Exception $e) {
91
					$this->logger->error('Can not parse federated cloud ID "' .  $lookup['federationId'] . '"');
92
					$this->logger->error($e->getMessage());
93
					continue;
94
				}
95
				if ($this->currentUserRemote === $remote) {
96
					continue;
97
				}
98
				$name = isset($lookup['name']['value']) ? $lookup['name']['value'] : '';
99
				$label = empty($name) ? $lookup['federationId'] : $name . ' (' . $lookup['federationId'] . ')';
100
				$result[] = [
101
					'label' => $label,
102
					'value' => [
103
						'shareType' => Share::SHARE_TYPE_REMOTE,
104
						'shareWith' => $lookup['federationId'],
105
					],
106
					'extra' => $lookup,
107
				];
108
			}
109
		} catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
110
		}
111
112
		$type = new SearchResultType('lookup');
113
		$searchResult->addResultSet($type, $result, []);
114
115
		return false;
116
	}
117
}
118