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

AGateway::deriveIdFromFqcn()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.2

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 12
ccs 8
cts 10
cp 0.8
rs 9.6111
cc 5
nc 4
nop 1
crap 5.2
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 1
	#[\Override]
36
	public function isComplete(?Settings $settings = null): bool {
37 1
		if (!is_object($settings)) {
38 1
			$settings = $this->getSettings();
39
		}
40 1
		$savedKeys = $this->appConfig->getKeys(Application::APP_ID);
41 1
		$providerId = $settings->id ?? $this->getProviderId();
42 1
		$fields = [];
43 1
		foreach ($settings->fields as $field) {
44 1
			$fields[] = $providerId . '_' . $field->field;
45
		}
46 1
		$intersect = array_intersect($fields, $savedKeys);
47 1
		return count($intersect) === count($fields);
48
	}
49
50 1
	#[\Override]
51
	public function getSettings(): Settings {
52 1
		if ($this->settings !== null) {
53 1
			return $this->settings;
54
		}
55 1
		return $this->settings = $this->createSettings();
56
	}
57
58
	#[\Override]
59
	abstract public function cliConfigure(InputInterface $input, OutputInterface $output): int;
60
61
	#[\Override]
62
	public function remove(?Settings $settings = null): void {
63
		if (!is_object($settings)) {
64
			$settings = $this->getSettings();
65
		}
66
		foreach ($settings->fields as $field) {
67
			$method = 'delete' . $this->toCamel($field->field);
68
			$this->{$method}();
69
		}
70
	}
71
72
	protected function toCamel(string $field): string {
73
		return str_replace(' ', '', ucwords(str_replace('_', ' ', $field)));
74
	}
75
76 1
	#[\Override]
77
	public static function getProviderId(): string {
78 1
		$id = self::deriveIdFromFqcn(static::class);
79 1
		if ($id === null) {
80
			throw new \LogicException('Cannot derive gateway id from FQCN: ' . static::class);
81
		}
82 1
		return $id;
83
	}
84
85 1
	private static function deriveIdFromFqcn(string $fqcn): ?string {
86 1
		$prefix = 'OCA\\TwoFactorGateway\\Provider\\Channel\\';
87 1
		if (strncmp($fqcn, $prefix, strlen($prefix)) !== 0) {
88
			return null;
89
		}
90 1
		$rest = substr($fqcn, strlen($prefix));
91 1
		$sep = strpos($rest, '\\');
92 1
		if ($sep === false || substr($rest, $sep + 1) !== 'Gateway') {
93
			return null;
94
		}
95 1
		$type = substr($rest, 0, $sep);
96 1
		return $type !== '' ? strtolower($type) : null;
97
	}
98
}
99