Passed
Push — master ( 0eed71...c124b4 )
by John
12:29 queued 12s
created

SectionSearch::searchSections()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 16
c 1
b 0
f 0
nc 5
nop 4
dl 0
loc 26
rs 9.1111
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\IGroupManager;
27
use OCP\IL10N;
28
use OCP\IURLGenerator;
29
use OCP\IUser;
30
use OCP\Search\IProvider;
31
use OCP\Search\ISearchQuery;
32
use OCP\Search\SearchResult;
33
use OCP\Settings\IIconSection;
34
use OCP\Settings\ISection;
35
use OCP\Settings\IManager;
36
37
class SectionSearch implements IProvider {
38
39
	/** @var IManager */
40
	protected $settingsManager;
41
	/** @var IGroupManager */
42
	protected $groupManager;
43
	/** @var IURLGenerator */
44
	protected $urlGenerator;
45
	/** @var IL10N */
46
	protected $l;
47
48
	public function __construct(IManager $settingsManager,
49
								IGroupManager $groupManager,
50
								IURLGenerator $urlGenerator,
51
								IL10N $l) {
52
		$this->settingsManager = $settingsManager;
53
		$this->groupManager = $groupManager;
54
		$this->urlGenerator = $urlGenerator;
55
		$this->l = $l;
56
	}
57
58
	/**
59
	 * @inheritDoc
60
	 */
61
	public function getId(): string {
62
		return 'settings_sections';
63
	}
64
65
	/**
66
	 * @inheritDoc
67
	 */
68
	public function getName(): string {
69
		return $this->l->t('Settings');
70
	}
71
72
	/**
73
	 * @inheritDoc
74
	 */
75
	public function search(IUser $user, ISearchQuery $query): SearchResult {
76
		$isAdmin = $this->groupManager->isAdmin($user->getUID());
77
78
		$result = $this->searchSections(
79
			$query,
80
			$this->settingsManager->getPersonalSections(),
81
			$isAdmin ? $this->l->t('Personal') : '',
82
			'settings.PersonalSettings.index'
83
		);
84
85
		if ($this->groupManager->isAdmin($user->getUID())) {
86
			$result = array_merge($result, $this->searchSections(
87
				$query,
88
				$this->settingsManager->getAdminSections(),
89
				$this->l->t('Administration'),
90
				'settings.AdminSettings.index'
91
			));
92
		}
93
94
		return SearchResult::complete(
95
			$this->l->t('Settings'),
96
			$result
97
		);
98
	}
99
100
	/**
101
	 * @param ISearchQuery $query
102
	 * @param ISection[][] $sections
103
	 * @param string $subline
104
	 * @param string $routeName
105
	 * @return array
106
	 */
107
	public function searchSections(ISearchQuery $query, array $sections, string $subline, string $routeName): array {
108
		$result = [];
109
		foreach ($sections as $priority => $sectionsByPriority) {
110
			foreach ($sectionsByPriority as $section) {
111
				if (
112
					stripos($section->getName(), $query->getTerm()) === false &&
113
					stripos($section->getID(), $query->getTerm()) === false
114
				) {
115
					continue;
116
				}
117
118
				$iconUrl = '';
119
				if ($section instanceof IIconSection) {
120
					$iconUrl = $section->getIcon();
121
				}
122
123
				$result[] = new SectionResult(
124
					$iconUrl,
125
					$section->getName(),
126
					$subline,
127
					$this->urlGenerator->linkToRouteAbsolute($routeName, ['section' => $section->getID()])
128
				);
129
			}
130
		}
131
132
		return $result;
133
	}
134
}
135