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

AGateway::getProviderId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

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