Passed
Pull Request — master (#686)
by Vitor
04:54
created

TConfigurable::getSchemaFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
7
 * SPDX-License-Identifier: AGPL-3.0-or-later
8
 */
9
10
namespace OCA\TwoFactorGateway\Provider\Gateway;
11
12
use OCA\TwoFactorGateway\AppInfo\Application;
13
use OCA\TwoFactorGateway\Exception\ConfigurationException;
14
use OCP\IAppConfig;
15
16
trait TConfigurable {
17
	/**
18
	 * @return string|static
19
	 * @throws ConfigurationException
20
	 */
21 6
	public function __call(string $name, array $args) {
22 6
		if (!preg_match('/^(?<operation>get|set|delete)(?<field>[A-Z][A-Za-z0-9_]*)$/', $name, $matches)) {
23 1
			throw new ConfigurationException('Invalid method ' . $name);
24
		}
25 5
		$field = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $matches['field']));
26 5
		$key = $this->keyFromField($field);
27
28 4
		switch ($matches['operation']) {
29 4
			case 'get':
30 4
				$val = (string)$this->getAppConfig()->getValueString(Application::APP_ID, $key, '');
31 4
				if ($val === '') {
32 2
					throw new ConfigurationException('No value set for ' . $field);
33
				}
34 3
				return $val;
35
36 3
			case 'set':
37 3
				$this->getAppConfig()->setValueString(Application::APP_ID, $key, (string)($args[0] ?? ''));
38 3
				return $this;
39
40 1
			case 'delete':
41 1
				$this->getAppConfig()->deleteKey(Application::APP_ID, $key);
42 1
				return $this;
43
		}
44
		throw new ConfigurationException('Invalid operation ' . $matches['operation']);
45
	}
46
47
	/**
48
	 * @throws ConfigurationException
49
	 */
50 5
	private function keyFromField(string $fieldName): string {
51 5
		$settings = $this->getSettings();
0 ignored issues
show
Bug introduced by
The method getSettings() does not exist on OCA\TwoFactorGateway\Pro...r\Gateway\TConfigurable. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
		/** @scrutinizer ignore-call */ 
52
  $settings = $this->getSettings();
Loading history...
52 5
		$fields = $settings->fields;
53 5
		foreach ($fields as $field) {
54 5
			if ($field->field === $fieldName) {
55 4
				return $this->getProviderId() . '_' . $fieldName;
0 ignored issues
show
Bug introduced by
Are you sure $this->getProviderId() of type OCA\TwoFactorGateway\Pro...ay\TConfigurable|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
				return /** @scrutinizer ignore-type */ $this->getProviderId() . '_' . $fieldName;
Loading history...
Bug introduced by
The method getProviderId() does not exist on OCA\TwoFactorGateway\Pro...r\Gateway\TConfigurable. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
				return $this->/** @scrutinizer ignore-call */ getProviderId() . '_' . $fieldName;
Loading history...
56
			}
57
		}
58 1
		throw new ConfigurationException('Invalid configuration field: ' . $field->field . ', check SCHEMA at ' . static::class);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $field seems to be defined by a foreach iteration on line 53. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
59
	}
60
61
	/**
62
	 * @throws ConfigurationException
63
	 */
64 4
	private function getAppConfig(): IAppConfig {
65 4
		if (!isset($this->appConfig)) {
66
			throw new ConfigurationException('No app config set');
67
		}
68 4
		return $this->appConfig;
69
	}
70
}
71