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

AGateway::getConfiguration()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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