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

AProvider   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 25%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 11
eloc 28
dl 0
loc 55
ccs 6
cts 24
cp 0.25
rs 10
c 2
b 1
f 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A idOverride() 0 3 1
A getProviderId() 0 11 3
A getIdFromProviderFqcn() 0 10 4
A getSettings() 0 6 2
A setAppConfig() 0 3 1
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\Telegram\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
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
abstract class AProvider implements IProvider {
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\Pro...gram\Provider\AProvider: $fields, $field, $id
Loading history...
21
	public IAppConfig $appConfig;
22
	protected ?Settings $settings = null;
23
24
	/**
25
	 * @throws MessageTransmissionException
26
	 */
27
	#[\Override]
28
	abstract public function send(string $identifier, string $message);
29
30 1
	#[\Override]
31
	public function setAppConfig(IAppConfig $appConfig): void {
32 1
		$this->appConfig = $appConfig;
33
	}
34
35 1
	#[\Override]
36
	public function getSettings(): Settings {
37 1
		if ($this->settings !== null) {
38 1
			return $this->settings;
39
		}
40 1
		return $this->settings = $this->createSettings();
0 ignored issues
show
Bug introduced by
The method createSettings() does not exist on OCA\TwoFactorGateway\Pro...gram\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

40
		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...gram\Provider\AProvider which is incompatible with the type-hinted return OCA\TwoFactorGateway\Provider\Settings.
Loading history...
41
	}
42
43
	#[\Override]
44
	public static function idOverride(): ?string {
45
		return null;
46
	}
47
48
	#[\Override]
49
	public function getProviderId(): string {
50
		$settings = $this->getSettings();
51
		if (!empty($settings->id)) {
52
			return $settings->id;
53
		}
54
		$id = self::getIdFromProviderFqcn(static::class);
55
		if ($id === null) {
56
			throw new \LogicException('Cannot derive gateway id from FQCN: ' . static::class);
57
		}
58
		return $id;
59
	}
60
61
	#[\Override]
62
	abstract public function cliConfigure(InputInterface $input, OutputInterface $output): int;
63
64
	private static function getIdFromProviderFqcn(string $fqcn): ?string {
65
		$prefix = 'OCA\\TwoFactorGateway\\Provider\\Channel\\Telegram\\Provider\\Drivers\\';
66
		if (strncmp($fqcn, $prefix, strlen($prefix)) !== 0) {
67
			return null;
68
		}
69
		$type = substr($fqcn, strlen($prefix));
70
		if (strpos($type, '\\') !== false) {
71
			return null;
72
		}
73
		return $type !== '' ? strtolower($type) : null;
74
	}
75
}
76