Passed
Push — master ( 4765f5...78c7e6 )
by Roeland
11:41 queued 14s
created

Manager::getPersonalSettings()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Arthur Schiwon <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Christoph Wurst <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author John Molakvoæ (skjnldsv) <[email protected]>
9
 * @author Julius Härtl <[email protected]>
10
 * @author Lukas Reschke <[email protected]>
11
 * @author Morris Jobke <[email protected]>
12
 * @author Robin Appelman <[email protected]>
13
 * @author Roeland Jago Douma <[email protected]>
14
 * @author sualko <[email protected]>
15
 *
16
 * @license GNU AGPL version 3 or any later version
17
 *
18
 * This program is free software: you can redistribute it and/or modify
19
 * it under the terms of the GNU Affero General Public License as
20
 * published by the Free Software Foundation, either version 3 of the
21
 * License, or (at your option) any later version.
22
 *
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 * GNU Affero General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU Affero General Public License
29
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30
 *
31
 */
32
33
namespace OC\Settings;
34
35
use Closure;
36
use OC\Settings\Personal\PersonalInfo;
0 ignored issues
show
Bug introduced by
The type OC\Settings\Personal\PersonalInfo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
37
use OCP\AppFramework\QueryException;
38
use OCP\IL10N;
39
use OCP\ILogger;
40
use OCP\IServerContainer;
41
use OCP\IURLGenerator;
42
use OCP\L10N\IFactory;
43
use OCP\Settings\IManager;
44
use OCP\Settings\ISection;
45
use OCP\Settings\ISettings;
46
use OCP\Settings\ISubAdminSettings;
47
48
class Manager implements IManager {
49
50
	/** @var ILogger */
51
	private $log;
52
53
	/** @var IL10N */
54
	private $l;
55
56
	/** @var IFactory */
57
	private $l10nFactory;
58
59
	/** @var IURLGenerator */
60
	private $url;
61
62
	/** @var IServerContainer */
63
	private $container;
64
65
	public function __construct(
66
		ILogger $log,
67
		IFactory $l10nFactory,
68
		IURLGenerator $url,
69
		IServerContainer $container
70
	) {
71
		$this->log = $log;
72
		$this->l10nFactory = $l10nFactory;
73
		$this->url = $url;
74
		$this->container = $container;
75
	}
76
77
	/** @var array */
78
	protected $sectionClasses = [];
79
80
	/** @var array */
81
	protected $sections = [];
82
83
	/**
84
	 * @param string $type 'admin' or 'personal'
85
	 * @param string $section Class must implement OCP\Settings\ISection
86
	 *
87
	 * @return void
88
	 */
89
	public function registerSection(string $type, string $section) {
90
		if (!isset($this->sectionClasses[$type])) {
91
			$this->sectionClasses[$type] = [];
92
		}
93
94
		$this->sectionClasses[$type][] = $section;
95
	}
96
97
	/**
98
	 * @param string $type 'admin' or 'personal'
99
	 *
100
	 * @return ISection[]
101
	 */
102
	protected function getSections(string $type): array {
103
		if (!isset($this->sections[$type])) {
104
			$this->sections[$type] = [];
105
		}
106
107
		if (!isset($this->sectionClasses[$type])) {
108
			return $this->sections[$type];
109
		}
110
111
		foreach ($this->sectionClasses[$type] as $index => $class) {
112
			try {
113
				/** @var ISection $section */
114
				$section = \OC::$server->query($class);
115
			} catch (QueryException $e) {
116
				$this->log->logException($e, ['level' => ILogger::INFO]);
117
				continue;
118
			}
119
120
			if (!$section instanceof ISection) {
121
				$this->log->logException(new \InvalidArgumentException('Invalid settings section registered'), ['level' => ILogger::INFO]);
122
				continue;
123
			}
124
125
			$sectionID = $section->getID();
126
127
			if (isset($this->sections[$type][$sectionID])) {
128
				$this->log->logException(new \InvalidArgumentException('Section with the same ID already registered'), ['level' => ILogger::INFO]);
129
				continue;
130
			}
131
132
			$this->sections[$type][$sectionID] = $section;
133
134
			unset($this->sectionClasses[$type][$index]);
135
		}
136
137
		return $this->sections[$type];
138
	}
139
140
	/** @var array */
141
	protected $settingClasses = [];
142
143
	/** @var array */
144
	protected $settings = [];
145
146
	/**
147
	 * @param string $type 'admin' or 'personal'
148
	 * @param string $setting Class must implement OCP\Settings\ISetting
149
	 *
150
	 * @return void
151
	 */
152
	public function registerSetting(string $type, string $setting) {
153
		$this->settingClasses[$setting] = $type;
154
	}
155
156
	/**
157
	 * @param string $type 'admin' or 'personal'
158
	 * @param string $section
159
	 * @param Closure $filter optional filter to apply on all loaded ISettings
160
	 *
161
	 * @return ISettings[]
162
	 */
163
	protected function getSettings(string $type, string $section, Closure $filter = null): array {
164
		if (!isset($this->settings[$type])) {
165
			$this->settings[$type] = [];
166
		}
167
		if (!isset($this->settings[$type][$section])) {
168
			$this->settings[$type][$section] = [];
169
		}
170
171
		foreach ($this->settingClasses as $class => $settingsType) {
172
			if ($type !== $settingsType) {
173
				continue;
174
			}
175
176
			try {
177
				/** @var ISettings $setting */
178
				$setting = $this->container->query($class);
179
			} catch (QueryException $e) {
180
				$this->log->logException($e, ['level' => ILogger::INFO]);
181
				continue;
182
			}
183
184
			if (!$setting instanceof ISettings) {
185
				$this->log->logException(new \InvalidArgumentException('Invalid settings setting registered (' . $class . ')'), ['level' => ILogger::INFO]);
186
				continue;
187
			}
188
189
			if ($filter !== null && !$filter($setting)) {
190
				continue;
191
			}
192
			if ($setting->getSection() === null) {
193
				continue;
194
			}
195
196
			if (!isset($this->settings[$settingsType][$setting->getSection()])) {
197
				$this->settings[$settingsType][$setting->getSection()] = [];
198
			}
199
			$this->settings[$settingsType][$setting->getSection()][] = $setting;
200
201
			unset($this->settingClasses[$class]);
202
		}
203
204
		return $this->settings[$type][$section];
205
	}
206
207
	/**
208
	 * @inheritdoc
209
	 */
210
	public function getAdminSections(): array {
211
		// built-in sections
212
		$sections = [];
213
214
		$appSections = $this->getSections('admin');
215
216
		foreach ($appSections as $section) {
217
			/** @var ISection $section */
218
			if (!isset($sections[$section->getPriority()])) {
219
				$sections[$section->getPriority()] = [];
220
			}
221
222
			$sections[$section->getPriority()][] = $section;
223
		}
224
225
		ksort($sections);
226
227
		return $sections;
228
	}
229
230
	/**
231
	 * @param string $section
232
	 * @param Closure $filter
233
	 *
234
	 * @return ISection[]
235
	 */
236
	private function getBuiltInAdminSettings($section, Closure $filter = null): array {
237
		$forms = [];
238
239
		if ($section === 'overview') {
240
			/** @var ISettings $form */
241
			$form = $this->container->query(\OCA\Settings\Admin\Overview::class);
242
			if ($filter === null || $filter($form)) {
243
				$forms[$form->getPriority()] = [$form];
244
			}
245
		}
246
		if ($section === 'server') {
247
			/** @var ISettings $form */
248
			$form = $this->container->query(\OCA\Settings\Admin\Server::class);
249
			if ($filter === null || $filter($form)) {
250
				$forms[$form->getPriority()] = [$form];
251
			}
252
			$form = $this->container->query(\OCA\Settings\Admin\Mail::class);
253
			if ($filter === null || $filter($form)) {
254
				$forms[$form->getPriority()] = [$form];
255
			}
256
		}
257
		if ($section === 'security') {
258
			/** @var ISettings $form */
259
			$form = $this->container->query(\OCA\Settings\Admin\Security::class);
260
			if ($filter === null || $filter($form)) {
261
				$forms[$form->getPriority()] = [$form];
262
			}
263
		}
264
		if ($section === 'sharing') {
265
			/** @var ISettings $form */
266
			$form = $this->container->query(\OCA\Settings\Admin\Sharing::class);
267
			if ($filter === null || $filter($form)) {
268
				$forms[$form->getPriority()] = [$form];
269
			}
270
		}
271
272
		return $forms;
273
	}
274
275
	/**
276
	 * @inheritdoc
277
	 */
278
	public function getAdminSettings($section, bool $subAdminOnly = false): array {
279
		if ($subAdminOnly) {
280
			$subAdminSettingsFilter = function(ISettings $settings) {
281
				return $settings instanceof ISubAdminSettings;
282
			};
283
			$settings = $this->getBuiltInAdminSettings($section, $subAdminSettingsFilter);
284
			$appSettings = $this->getSettings('admin', $section, $subAdminSettingsFilter);
285
		} else {
286
			$settings = $this->getBuiltInAdminSettings($section);
287
			$appSettings = $this->getSettings('admin', $section);
288
		}
289
290
		foreach ($appSettings as $setting) {
291
			if (!isset($settings[$setting->getPriority()])) {
292
				$settings[$setting->getPriority()] = [];
293
			}
294
			$settings[$setting->getPriority()][] = $setting;
295
		}
296
297
		ksort($settings);
298
		return $settings;
299
	}
300
301
	/**
302
	 * @inheritdoc
303
	 */
304
	public function getPersonalSections(): array {
305
		if ($this->l === null) {
306
			$this->l = $this->l10nFactory->get('lib');
307
		}
308
309
		$sections = [];
310
311
		$legacyForms = \OC_App::getForms('personal');
312
		if (!empty($legacyForms) && $this->hasLegacyPersonalSettingsToRender($legacyForms)) {
313
			$sections[98] = [new Section('additional', $this->l->t('Additional settings'), 0, $this->url->imagePath('core', 'actions/settings-dark.svg'))];
314
		}
315
316
		$appSections = $this->getSections('personal');
317
318
		foreach ($appSections as $section) {
319
			/** @var ISection $section */
320
			if (!isset($sections[$section->getPriority()])) {
321
				$sections[$section->getPriority()] = [];
322
			}
323
324
			$sections[$section->getPriority()][] = $section;
325
		}
326
327
		ksort($sections);
328
329
		return $sections;
330
	}
331
332
	/**
333
	 * @param string[] $forms
334
	 *
335
	 * @return bool
336
	 */
337
	private function hasLegacyPersonalSettingsToRender(array $forms): bool {
338
		foreach ($forms as $form) {
339
			if (trim($form) !== '') {
340
				return true;
341
			}
342
		}
343
		return false;
344
	}
345
346
	/**
347
	 * @inheritdoc
348
	 */
349
	public function getPersonalSettings($section): array {
350
		$settings = [];
351
		$appSettings = $this->getSettings('personal', $section);
352
353
		foreach ($appSettings as $setting) {
354
			if (!isset($settings[$setting->getPriority()])) {
355
				$settings[$setting->getPriority()] = [];
356
			}
357
			$settings[$setting->getPriority()][] = $setting;
358
		}
359
360
		ksort($settings);
361
		return $settings;
362
	}
363
}
364