Passed
Pull Request — master (#686)
by Vitor
05:00 queued 15s
created

AProvider::getProviderId()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.679

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 10
ccs 3
cts 7
cp 0.4286
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 4.679
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * SPDX-FileCopyrightText: 2024 Christoph Wurst <[email protected]>
7
 * SPDX-License-Identifier: AGPL-3.0-or-later
8
 */
9
10
namespace OCA\TwoFactorGateway\Provider\Channel\SMS\Provider;
11
12
use OCA\TwoFactorGateway\Exception\MessageTransmissionException;
13
use OCA\TwoFactorGateway\Provider\Gateway\TConfigurable;
14
use OCP\IAppConfig;
15
16
abstract class AProvider implements IProvider {
17
	use TConfigurable;
18
	public IAppConfig $appConfig;
19
20
	/**
21
	 * @throws MessageTransmissionException
22
	 */
23
	#[\Override]
24
	abstract public function send(string $identifier, string $message);
25
26 6
	#[\Override]
27
	public function setAppConfig(IAppConfig $appConfig): void {
28 6
		$this->appConfig = $appConfig;
29
	}
30
31
	#[\Override]
32
	public static function idOverride(): ?string {
33
		return null;
34
	}
35
36 4
	#[\Override]
37
	public static function getProviderId(): string {
38 4
		if (static::SCHEMA['id'] ?? null) {
39 4
			return static::SCHEMA['id'];
40
		}
41
		$id = self::getIdFromProviderFqcn(static::class);
42
		if ($id === null) {
43
			throw new \LogicException('Cannot derive gateway id from FQCN: ' . static::class);
44
		}
45
		return $id;
46
	}
47
48
	private static function getIdFromProviderFqcn(string $fqcn): ?string {
49
		$prefix = 'OCA\\TwoFactorGateway\\Provider\\Channel\\SMS\\Provider\\Drivers\\';
50
		if (strncmp($fqcn, $prefix, strlen($prefix)) !== 0) {
51
			return null;
52
		}
53
		$type = substr($fqcn, strlen($prefix));
54
		if (strpos($type, '\\') !== false) {
55
			return null;
56
		}
57
		return $type !== '' ? strtolower($type) : null;
58
	}
59
}
60