Completed
Push — master ( 5412c2...2d62f9 )
by Blizzz
45:23 queued 29:26
created

Search::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
use OCP\Collaboration\Collaborators\ISearch;
27
use OCP\Collaboration\Collaborators\ISearchPlugin;
28
use OCP\Collaboration\Collaborators\ISearchResult;
29
use OCP\Collaboration\Collaborators\SearchResultType;
30
use OCP\IContainer;
31
use OCP\Share;
32
33
class Search implements ISearch {
34
	/** @var IContainer */
35
	private $c;
36
37
	protected $pluginList = [];
38
39
	public function __construct(IContainer $c) {
40
		$this->c = $c;
41
	}
42
43
	public function search($search, array $shareTypes, $lookup, $limit, $offset) {
44
		$hasMoreResults = false;
45
46
		/** @var ISearchResult $searchResult */
47
		$searchResult = $this->c->resolve(SearchResult::class);
48
49
		foreach ($shareTypes as $type) {
50
			if(!isset($this->pluginList[$type])) {
51
				continue;
52
			}
53
			foreach ($this->pluginList[$type] as $plugin) {
54
				/** @var ISearchPlugin $searchPlugin */
55
				$searchPlugin = $this->c->resolve($plugin);
56
				$hasMoreResults |= $searchPlugin->search($search, $limit, $offset, $searchResult);
57
			}
58
		}
59
60
		// Get from lookup server, not a separate share type
61
		if ($lookup) {
62
			$searchPlugin = $this->c->resolve(LookupPlugin::class);
63
			$hasMoreResults |= $searchPlugin->search($search, $limit, $offset, $searchResult);
64
		}
65
66
		// sanitizing, could go into the plugins as well
67
68
		// if we have a exact match, either for the federated cloud id or for the
69
		// email address we only return the exact match. It is highly unlikely
70
		// that the exact same email address and federated cloud id exists
71
		$emailType = new SearchResultType('emails');
72
		$remoteType = new SearchResultType('remotes');
73
		if($searchResult->hasExactIdMatch($emailType) && !$searchResult->hasExactIdMatch($remoteType)) {
74
			$searchResult->unsetResult($remoteType);
75
		} elseif (!$searchResult->hasExactIdMatch($emailType) && $searchResult->hasExactIdMatch($remoteType)) {
76
			$searchResult->unsetResult($emailType);
77
		}
78
79
		return [$searchResult->asArray(), (bool)$hasMoreResults];
80
	}
81
82
	public function registerPlugin(array $pluginInfo) {
83
		$shareType = constant(Share::class . '::' . $pluginInfo['shareType']);
84
		if($shareType === null) {
85
			throw new \InvalidArgumentException('Provided ShareType is invalid');
86
		}
87
		$this->pluginList[$shareType][] = $pluginInfo['class'];
88
	}
89
}
90