Completed
Push — master ( 8024bc...f8f0d2 )
by Vitor
14s
created

AProvider::getSettings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
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 OCA\TwoFactorGateway\Provider\Settings;
15
use OCP\IAppConfig;
16
17
abstract class AProvider implements IProvider {
18
	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\Pro...\SMS\Provider\AProvider: $fields, $field, $id
Loading history...
19
	public IAppConfig $appConfig;
20
	protected ?Settings $settings = null;
21
22
	/**
23
	 * @throws MessageTransmissionException
24
	 */
25
	#[\Override]
26
	abstract public function send(string $identifier, string $message);
27
28 8
	#[\Override]
29
	public function setAppConfig(IAppConfig $appConfig): void {
30 8
		$this->appConfig = $appConfig;
31
	}
32
33 7
	#[\Override]
34
	public function getSettings(): Settings {
35 7
		if ($this->settings !== null) {
36 5
			return $this->settings;
37
		}
38 6
		return $this->settings = $this->createSettings();
0 ignored issues
show
Bug introduced by
The method createSettings() does not exist on OCA\TwoFactorGateway\Pro...\SMS\Provider\AProvider. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
		return $this->settings = $this->/** @scrutinizer ignore-call */ createSettings();
Loading history...
Bug Best Practice introduced by
The expression return $this->settings = $this->createSettings() returns the type OCA\TwoFactorGateway\Pro...\SMS\Provider\AProvider which is incompatible with the type-hinted return OCA\TwoFactorGateway\Provider\Settings.
Loading history...
39
	}
40
41
	#[\Override]
42
	public static function idOverride(): ?string {
43
		return null;
44
	}
45
46
	#[\Override]
47
	public function getProviderId(): string {
48
		$settings = $this->getSettings();
49
		if (!empty($settings->id)) {
50
			return $settings->id;
51
		}
52
		$id = self::getIdFromProviderFqcn(static::class);
53
		if ($id === null) {
54
			throw new \LogicException('Cannot derive gateway id from FQCN: ' . static::class);
55
		}
56
		return $id;
57
	}
58
59
	private static function getIdFromProviderFqcn(string $fqcn): ?string {
60
		$prefix = 'OCA\\TwoFactorGateway\\Provider\\Channel\\SMS\\Provider\\Drivers\\';
61
		if (strncmp($fqcn, $prefix, strlen($prefix)) !== 0) {
62
			return null;
63
		}
64
		$type = substr($fqcn, strlen($prefix));
65
		if (strpos($type, '\\') !== false) {
66
			return null;
67
		}
68
		return $type !== '' ? strtolower($type) : null;
69
	}
70
}
71