Passed
Pull Request — master (#686)
by Vitor
08:35 queued 03:41
created

AProvider   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 11
eloc 27
dl 0
loc 54
ccs 0
cts 27
cp 0
rs 10
c 1
b 0
f 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getSettings() 0 6 2
A getProviderId() 0 10 3
A getIdFromProviderFqcn() 0 10 4
A setAppConfig() 0 3 1
A idOverride() 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
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
	#[\Override]
31
	public function setAppConfig(IAppConfig $appConfig): void {
32
		$this->appConfig = $appConfig;
33
	}
34
35
	#[\Override]
36
	public function getSettings(): Settings {
37
		if ($this->settings !== null) {
38
			return $this->settings;
39
		}
40
		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
		if (!empty($this->settings->id)) {
51
			return $this->settings->id;
52
		}
53
		$id = self::getIdFromProviderFqcn(static::class);
54
		if ($id === null) {
55
			throw new \LogicException('Cannot derive gateway id from FQCN: ' . static::class);
56
		}
57
		return $id;
58
	}
59
60
	#[\Override]
61
	abstract public function cliConfigure(InputInterface $input, OutputInterface $output): int;
62
63
	private static function getIdFromProviderFqcn(string $fqcn): ?string {
64
		$prefix = 'OCA\\TwoFactorGateway\\Provider\\Channel\\Telegram\\Provider\\Drivers\\';
65
		if (strncmp($fqcn, $prefix, strlen($prefix)) !== 0) {
66
			return null;
67
		}
68
		$type = substr($fqcn, strlen($prefix));
69
		if (strpos($type, '\\') !== false) {
70
			return null;
71
		}
72
		return $type !== '' ? strtolower($type) : null;
73
	}
74
}
75