Completed
Push — master ( 8024bc...f8f0d2 )
by Vitor
14s
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 5
	#[\Override]
51
	public function getSettings(): Settings {
52 5
		if ($this->settings !== null) {
53 5
			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 3
	#[\Override]
64
	public function remove(?Settings $settings = null): void {
65 3
		if (!is_object($settings)) {
66 1
			$settings = $this->getSettings();
67
		} else {
68 2
			$this->settings = $settings;
69
		}
70 3
		foreach ($settings->fields as $field) {
71 3
			$method = 'delete' . $this->toCamel($field->field);
72 3
			$this->{$method}();
73
		}
74
	}
75
76 3
	protected function toCamel(string $field): string {
77 3
		return str_replace(' ', '', ucwords(str_replace('_', ' ', $field)));
78
	}
79
80 4
	#[\Override]
81
	public function getProviderId(): string {
82 4
		$id = self::deriveIdFromFqcn(static::class);
83 4
		if ($id === null) {
84
			throw new \LogicException('Cannot derive gateway id from FQCN: ' . static::class);
85
		}
86 4
		return $id;
87
	}
88
89 4
	private static function deriveIdFromFqcn(string $fqcn): ?string {
90 4
		$prefix = 'OCA\\TwoFactorGateway\\Provider\\Channel\\';
91 4
		if (strncmp($fqcn, $prefix, strlen($prefix)) !== 0) {
92
			return null;
93
		}
94 4
		$rest = substr($fqcn, strlen($prefix));
95 4
		$sep = strpos($rest, '\\');
96 4
		if ($sep === false || substr($rest, $sep + 1) !== 'Gateway') {
97
			return null;
98
		}
99 4
		$type = substr($rest, 0, $sep);
100 4
		return $type !== '' ? strtolower($type) : null;
101
	}
102
}
103