Passed
Pull Request — master (#669)
by
unknown
01:56
created

TConfigurable::keyFromFieldName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 2
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 8
	public function __call(string $name, array $args) {
22 8
		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 7
		$field = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $matches['field']));
26 7
		$key = $this->keyFromField($field);
27
28
		switch ($matches['operation']) {
29
			case 'get':
30
				$val = (string)$this->getAppConfig()->getValueString(Application::APP_ID, $key, '');
31
				if ($val === '') {
32
					throw new ConfigurationException('No value set for ' . $field);
33
				}
34
				return $val;
35
36
			case 'set':
37
				$this->getAppConfig()->setValueString(Application::APP_ID, $key, (string)($args[0] ?? ''));
38
				return $this;
39
40
			case 'delete':
41
				$this->getAppConfig()->deleteKey(Application::APP_ID, $key);
42
				return $this;
43
		}
44
		throw new ConfigurationException('Invalid operation ' . $matches['operation']);
45
	}
46
47
	/**
48
	 * @return string
49
	 */
50 8
	private static function keyFromFieldName(string $id, string $fieldName):string
51
	{
52 8
		return $id . '_' . $fieldName;
53
	}
54
55
	/**
56
	 * @throws ConfigurationException
57
	 */
58 7
	private function keyFromField(string $fieldName, ?Settings $settings = null): string {
0 ignored issues
show
Bug introduced by
The type OCA\TwoFactorGateway\Provider\Gateway\Settings 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...
59 7
		if (!is_object($settings)) {
60 7
			$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

60
			/** @scrutinizer ignore-call */ 
61
   $settings = $this->getSettings();
Loading history...
61
		}
62 7
		$providerId = $settings->id ?? $this->getProviderId();
0 ignored issues
show
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

62
		$providerId = $settings->id ?? $this->/** @scrutinizer ignore-call */ getProviderId();
Loading history...
63 7
		$fields = $settings->fields;
64 7
		foreach ($fields as $field) {
65 7
			if ($field->field === $fieldName) {
66 6
				static::keyFromFieldName($providerId, $fieldName);
0 ignored issues
show
Bug introduced by
It seems like $providerId can also be of type OCA\TwoFactorGateway\Pro...r\Gateway\TConfigurable; however, parameter $id of OCA\TwoFactorGateway\Pro...ble::keyFromFieldName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

66
				static::keyFromFieldName(/** @scrutinizer ignore-type */ $providerId, $fieldName);
Loading history...
67
			}
68
		}
69 7
		throw new ConfigurationException('Invalid configuration field: ' . $fieldName . ', check SCHEMA at ' . static::class);
70
	}
71
72
	/**
73
	 * @throws ConfigurationException
74
	 */
75
	private function getAppConfig(): IAppConfig {
76
		if (!isset($this->appConfig)) {
77
			throw new ConfigurationException('No app config set');
78
		}
79
		return $this->appConfig;
80
	}
81
}
82