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

AGateway::getConfiguration()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 15
ccs 12
cts 12
cp 1
rs 9.9
cc 3
nc 4
nop 1
crap 3
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\MessageTransmissionException;
14
use OCA\TwoFactorGateway\Provider\Settings;
15
use OCP\IAppConfig;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
abstract class AGateway implements IGateway {
20
	use TConfigurable;
0 ignored issues
show
introduced by
The trait OCA\TwoFactorGateway\Pro...r\Gateway\TConfigurable requires some properties which are not provided by OCA\TwoFactorGateway\Provider\Gateway\AGateway: $fields, $field
Loading history...
21
	public const SCHEMA = [];
22
	protected ?Settings $settings = null;
23
24 1
	public function __construct(
25
		public IAppConfig $appConfig,
26
	) {
27 1
	}
28
29
	/**
30
	 * @throws MessageTransmissionException
31
	 */
32
	#[\Override]
33
	abstract public function send(string $identifier, string $message, array $extra = []): void;
34
35 2
	#[\Override]
36
	public function isComplete(?Settings $settings = null): bool {
37 2
		if (!is_object($settings)) {
38 2
			$settings = $this->getSettings();
39
		}
40 2
		$savedKeys = $this->appConfig->getKeys(Application::APP_ID);
41 2
		$providerId = $settings->id ?? $this->getProviderId();
42 2
		$fields = [];
43 2
		foreach ($settings->fields as $field) {
44 2
			$fields[] = self::keyFromFieldName($providerId, $field->field);
45
		}
46 2
		$intersect = array_intersect($fields, $savedKeys);
47 2
		return count($intersect) === count($fields);
48
	}
49
50 1
	#[\Override]
51
	public function getConfiguration(?Settings $settings = null): array {
52 1
		if (!is_object($settings)) {
53 1
			$settings = $this->getSettings();
54
		}
55 1
		$providerId = $this->getProviderId();
56 1
		$config = [];
57 1
		foreach ($settings->fields as $field) {
58 1
			$config[$f['field']] = $this->appConfig->getValueString(
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $f seems to be never defined.
Loading history...
59 1
				Application::APP_ID,
60 1
				self::keyFromFieldName($providerId, $field->field),
61 1
				$field->default,
62 1
			);
63
		}
64 1
		return $config;
65
	}
66
67 5
	#[\Override]
68
	public function getSettings(): Settings {
69 5
		if ($this->settings !== null) {
70 5
			return $this->settings;
71
		}
72 2
		$this->settings = $this->createSettings();
73 2
		$this->settings->id = $this->settings->id ?? $this->getProviderId();
74 2
		return $this->settings;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->settings returns the type null which is incompatible with the type-hinted return OCA\TwoFactorGateway\Provider\Settings.
Loading history...
75
	}
76
77
	#[\Override]
78
	abstract public function cliConfigure(InputInterface $input, OutputInterface $output): int;
79
80 3
	#[\Override]
81
	public function remove(?Settings $settings = null): void {
82 3
		if (!is_object($settings)) {
83 1
			$settings = $this->getSettings();
84
		} else {
85 2
			$this->settings = $settings;
86
		}
87 3
		foreach ($settings->fields as $field) {
88 3
			$method = 'delete' . $this->toCamel($field->field);
89 3
			$this->{$method}();
90
		}
91
	}
92
93 4
	protected function toCamel(string $field): string {
94 4
		return str_replace(' ', '', ucwords(str_replace('_', ' ', $field)));
95
	}
96
97 6
	#[\Override]
98
	public function getProviderId(): string {
99 6
		$id = self::deriveIdFromFqcn(static::class);
100 6
		if ($id === null) {
101
			throw new \LogicException('Cannot derive gateway id from FQCN: ' . static::class);
102
		}
103 6
		return $id;
104
	}
105
106 6
	private static function deriveIdFromFqcn(string $fqcn): ?string {
107 6
		$prefix = 'OCA\\TwoFactorGateway\\Provider\\Channel\\';
108 6
		if (strncmp($fqcn, $prefix, strlen($prefix)) !== 0) {
109
			return null;
110
		}
111 6
		$rest = substr($fqcn, strlen($prefix));
112 6
		$sep = strpos($rest, '\\');
113 6
		if ($sep === false || substr($rest, $sep + 1) !== 'Gateway') {
114
			return null;
115
		}
116 6
		$type = substr($rest, 0, $sep);
117 6
		return $type !== '' ? strtolower($type) : null;
118
	}
119
}
120