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

AProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 26.32%

Importance

Changes 0
Metric Value
wmc 9
eloc 21
dl 0
loc 42
ccs 5
cts 19
cp 0.2632
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A idOverride() 0 3 1
A setAppConfig() 0 3 1
A getProviderId() 0 10 3
A getIdFromProviderFqcn() 0 10 4
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