Passed
Push — master ( 2b3684...9979b6 )
by Morris
13:31 queued 10s
created

Search   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 75
rs 10
c 2
b 0
f 0
wmc 16

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A registerPlugin() 0 6 2
C search() 0 48 13
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Arthur Schiwon <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Christoph Wurst <[email protected]>
7
 * @author onehappycat <[email protected]>
8
 * @author Robin Appelman <[email protected]>
9
 *
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 OC\Collaboration\Collaborators;
28
29
use OCP\Collaboration\Collaborators\ISearch;
30
use OCP\Collaboration\Collaborators\ISearchPlugin;
31
use OCP\Collaboration\Collaborators\ISearchResult;
32
use OCP\Collaboration\Collaborators\SearchResultType;
33
use OCP\IContainer;
34
use OCP\Share;
35
36
class Search implements ISearch {
37
	/** @var IContainer */
38
	private $c;
39
40
	protected $pluginList = [];
41
42
	public function __construct(IContainer $c) {
43
		$this->c = $c;
44
	}
45
46
	/**
47
	 * @param string $search
48
	 * @param array $shareTypes
49
	 * @param bool $lookup
50
	 * @param int|null $limit
51
	 * @param int|null $offset
52
	 * @return array
53
	 * @throws \OCP\AppFramework\QueryException
54
	 */
55
	public function search($search, array $shareTypes, $lookup, $limit, $offset) {
56
		$hasMoreResults = false;
57
58
		// Trim leading and trailing whitespace characters, e.g. when query is copy-pasted
59
		$search = trim($search);
60
61
		/** @var ISearchResult $searchResult */
62
		$searchResult = $this->c->resolve(SearchResult::class);
63
64
		foreach ($shareTypes as $type) {
65
			if (!isset($this->pluginList[$type])) {
66
				continue;
67
			}
68
			foreach ($this->pluginList[$type] as $plugin) {
69
				/** @var ISearchPlugin $searchPlugin */
70
				$searchPlugin = $this->c->resolve($plugin);
71
				$hasMoreResults = $searchPlugin->search($search, $limit, $offset, $searchResult) || $hasMoreResults;
72
			}
73
		}
74
75
		// Get from lookup server, not a separate share type
76
		if ($lookup) {
77
			$searchPlugin = $this->c->resolve(LookupPlugin::class);
78
			$hasMoreResults = $searchPlugin->search($search, $limit, $offset, $searchResult) || $hasMoreResults;
0 ignored issues
show
Bug introduced by
The method search() does not exist on stdClass. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
			$hasMoreResults = $searchPlugin->/** @scrutinizer ignore-call */ search($search, $limit, $offset, $searchResult) || $hasMoreResults;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
79
		}
80
81
		// sanitizing, could go into the plugins as well
82
83
		// if we have a exact match, either for the federated cloud id or for the
84
		// email address we only return the exact match. It is highly unlikely
85
		// that the exact same email address and federated cloud id exists
86
		$emailType = new SearchResultType('emails');
87
		$remoteType = new SearchResultType('remotes');
88
		if ($searchResult->hasExactIdMatch($emailType) && !$searchResult->hasExactIdMatch($remoteType)) {
89
			$searchResult->unsetResult($remoteType);
90
		} elseif (!$searchResult->hasExactIdMatch($emailType) && $searchResult->hasExactIdMatch($remoteType)) {
91
			$searchResult->unsetResult($emailType);
92
		}
93
94
		// if we have an exact local user match with an email-a-like query,
95
		// there is no need to show the remote and email matches.
96
		$userType = new SearchResultType('users');
97
		if (strpos($search, '@') !== false && $searchResult->hasExactIdMatch($userType)) {
98
			$searchResult->unsetResult($remoteType);
99
			$searchResult->unsetResult($emailType);
100
		}
101
102
		return [$searchResult->asArray(), (bool)$hasMoreResults];
103
	}
104
105
	public function registerPlugin(array $pluginInfo) {
106
		$shareType = constant(Share::class . '::' . $pluginInfo['shareType']);
107
		if ($shareType === null) {
108
			throw new \InvalidArgumentException('Provided ShareType is invalid');
109
		}
110
		$this->pluginList[$shareType][] = $pluginInfo['class'];
111
	}
112
}
113