Passed
Push — master ( 588b6f...2aa625 )
by Morris
19:50 queued 09:11
created

AppSearch::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * @copyright Copyright (c) 2020 Joas Schilling <[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 OCA\Settings\Search;
25
26
use OCP\IL10N;
27
use OCP\INavigationManager;
28
use OCP\IUser;
29
use OCP\Search\IProvider;
30
use OCP\Search\ISearchQuery;
31
use OCP\Search\SearchResult;
32
use OCP\Search\SearchResultEntry;
33
34
class AppSearch implements IProvider {
35
36
	/** @var INavigationManager */
37
	protected $navigationManager;
38
39
	/** @var IL10N */
40
	protected $l;
41
42
	public function __construct(INavigationManager $navigationManager,
43
								IL10N $l) {
44
		$this->navigationManager = $navigationManager;
45
		$this->l = $l;
46
	}
47
48
	/**
49
	 * @inheritDoc
50
	 */
51
	public function getId(): string {
52
		return 'settings_apps';
53
	}
54
55
	/**
56
	 * @inheritDoc
57
	 */
58
	public function getName(): string {
59
		return $this->l->t('Apps');
60
	}
61
62
	/**
63
	 * @inheritDoc
64
	 */
65
	public function getOrder(string $route, array $routeParameters): int {
66
		return -50;
67
	}
68
69
	/**
70
	 * @inheritDoc
71
	 */
72
	public function search(IUser $user, ISearchQuery $query): SearchResult {
73
		/** @var  $entries */
74
		$entries = $this->navigationManager->getAll('all');
75
76
		$result = [];
77
		foreach ($entries as $entry) {
78
			if (
79
				stripos($entry['name'], $query->getTerm()) === false &&
80
				stripos($entry['id'], $query->getTerm()) === false
81
			) {
82
				continue;
83
			}
84
85
			if (strpos($query->getRoute(), $entry['id'] . '.') === 0) {
86
				// Skip the current app, unlikely this is intended
87
				continue;
88
			}
89
90
			if ($entry['href'] === '') {
91
				// Nothing we can open, so ignore
92
				continue;
93
			}
94
95
			$result[] = new SearchResultEntry(
96
				'',
97
				$entry['name'],
98
				'',
99
				$entry['href'],
100
				'icon-confirm'
101
			);
102
		}
103
104
		return SearchResult::complete(
105
			$this->l->t('Apps'),
106
			$result
107
		);
108
	}
109
}
110