Passed
Pull Request — master (#799)
by Vitor
19:42 queued 15:16
created

AProvider::isComplete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 8
c 1
b 1
f 0
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
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\AppInfo\Application;
13
use OCA\TwoFactorGateway\Exception\MessageTransmissionException;
14
use OCA\TwoFactorGateway\Provider\Gateway\TConfigurable;
15
use OCA\TwoFactorGateway\Provider\Settings;
16
use OCP\IAppConfig;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
abstract class AProvider implements IProvider {
21
	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...
22
	public IAppConfig $appConfig;
23
	protected ?Settings $settings = null;
24
25
	/**
26
	 * @throws MessageTransmissionException
27
	 */
28
	#[\Override]
29
	abstract public function send(string $identifier, string $message);
30
31 3
	#[\Override]
32
	public function setAppConfig(IAppConfig $appConfig): void {
33 3
		$this->appConfig = $appConfig;
34
	}
35
36 6
	#[\Override]
37
	public function getSettings(): Settings {
38 6
		if ($this->settings !== null) {
39 6
			return $this->settings;
40
		}
41 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

41
		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...
42
	}
43
44
	#[\Override]
45
	public static function idOverride(): ?string {
46
		return null;
47
	}
48
49
	#[\Override]
50
	public function getProviderId(): string {
51
		$settings = $this->getSettings();
52
		if (!empty($settings->id)) {
53
			return $settings->id;
54
		}
55
		$id = self::getIdFromProviderFqcn(static::class);
56
		if ($id === null) {
57
			throw new \LogicException('Cannot derive gateway id from FQCN: ' . static::class);
58
		}
59
		return $id;
60
	}
61
62
	#[\Override]
63
	abstract public function cliConfigure(InputInterface $input, OutputInterface $output): int;
64
65
	public function isComplete(): bool {
66
		$settings = $this->getSettings();
67
		$savedKeys = $this->appConfig->getKeys(Application::APP_ID);
68
		$providerId = $settings->id ?? $this->getProviderId();
69
		$fields = [];
70
		foreach ($settings->fields as $field) {
71
			$fields[] = self::keyFromFieldName($providerId, $field->field);
72
		}
73
		$intersect = array_intersect($fields, $savedKeys);
74
		return count($intersect) === count($fields);
75
	}
76
77
	private static function getIdFromProviderFqcn(string $fqcn): ?string {
78
		$prefix = 'OCA\\TwoFactorGateway\\Provider\\Channel\\Telegram\\Provider\\Drivers\\';
79
		if (strncmp($fqcn, $prefix, strlen($prefix)) !== 0) {
80
			return null;
81
		}
82
		$type = substr($fqcn, strlen($prefix));
83
		if (strpos($type, '\\') !== false) {
84
			return null;
85
		}
86
		return $type !== '' ? strtolower($type) : null;
87
	}
88
}
89