Completed
Push — master ( cbce23...a2678d )
by Morris
14:21
created

Search::search()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Andrew Brown <[email protected]>
6
 * @author Bart Visscher <[email protected]>
7
 * @author Jörn Friedrich Dreyer <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Robin Appelman <[email protected]>
10
 *
11
 * @license AGPL-3.0
12
 *
13
 * This code is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License, version 3,
15
 * as published by the Free Software Foundation.
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, version 3,
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
24
 *
25
 */
26
27
namespace OC;
28
use OCP\Search\PagedProvider;
29
use OCP\Search\Provider;
30
use OCP\ISearch;
31
32
/**
33
 * Provide an interface to all search providers
34
 */
35
class Search implements ISearch {
36
37
	private $providers = array();
38
	private $registeredProviders = array();
39
40
	/**
41
	 * Search all providers for $query
42
	 * @param string $query
43
	 * @param string[] $inApps optionally limit results to the given apps
44
	 * @param int $page pages start at page 1
45
	 * @param int $size, 0 = all
0 ignored issues
show
Bug introduced by
There is no parameter named $size,. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
46
	 * @return array An array of OC\Search\Result's
47
	 */
48
	public function searchPaged($query, array $inApps = array(), $page = 1, $size = 30) {
49
		$this->initProviders();
50
		$results = array();
51
		foreach($this->providers as $provider) {
52
			/** @var $provider Provider */
53
			if ( ! $provider->providesResultsFor($inApps) ) {
54
				continue;
55
			}
56
			if ($provider instanceof PagedProvider) {
57
				$results = array_merge($results, $provider->searchPaged($query, $page, $size));
58
			} else if ($provider instanceof Provider) {
59
				$providerResults = $provider->search($query);
60
				if ($size > 0) {
61
					$slicedResults = array_slice($providerResults, ($page - 1) * $size, $size);
62
					$results = array_merge($results, $slicedResults);
63
				} else {
64
					$results = array_merge($results, $providerResults);
65
				}
66
			} else {
67
				\OC::$server->getLogger()->warning('Ignoring Unknown search provider', array('provider' => $provider));
68
			}
69
		}
70
		return $results;
71
	}
72
73
	/**
74
	 * Remove all registered search providers
75
	 */
76
	public function clearProviders() {
77
		$this->providers = array();
78
		$this->registeredProviders = array();
79
	}
80
81
	/**
82
	 * Remove one existing search provider
83
	 * @param string $provider class name of a OC\Search\Provider
84
	 */
85
	public function removeProvider($provider) {
86
		$this->registeredProviders = array_filter(
87
			$this->registeredProviders,
88
			function ($element) use ($provider) {
89
				return ($element['class'] != $provider);
90
			}
91
		);
92
		// force regeneration of providers on next search
93
		$this->providers = array();
94
	}
95
96
	/**
97
	 * Register a new search provider to search with
98
	 * @param string $class class name of a OC\Search\Provider
99
	 * @param array $options optional
100
	 */
101
	public function registerProvider($class, array $options = array()) {
102
		$this->registeredProviders[] = array('class' => $class, 'options' => $options);
103
	}
104
105
	/**
106
	 * Create instances of all the registered search providers
107
	 */
108
	private function initProviders() {
109
		if( ! empty($this->providers) ) {
110
			return;
111
		}
112
		foreach($this->registeredProviders as $provider) {
113
			$class = $provider['class'];
114
			$options = $provider['options'];
115
			$this->providers[] = new $class($options);
116
		}
117
	}
118
119
}
120