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

Factory::isValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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;
11
12
use OCA\TwoFactorGateway\Exception\InvalidProviderException;
13
use OCA\TwoFactorGateway\Provider\AFactory;
14
use OCA\TwoFactorGateway\Provider\Channel\SMS\Provider\AProvider;
15
16
/** @extends AFactory<AProvider> */
17
class Factory extends AFactory {
18
	/** @var array<AProvider> */
19
	private array $instancesByFqcn = [];
20 2
	#[\Override]
21
	protected function getPrefix(): string {
22 2
		return 'OCA\\TwoFactorGateway\\Provider\\Channel\\SMS\\Provider\\Drivers\\';
23
	}
24
25 2
	#[\Override]
26
	protected function getSuffix(): string {
27 2
		return '';
28
	}
29
30 2
	#[\Override]
31
	protected function getBaseClass(): string {
32 2
		return AProvider::class;
33
	}
34
35 2
	#[\Override]
36
	public function get(string $name): object {
37 2
		if (isset($this->instancesByFqcn[$name])) {
38
			return $this->instancesByFqcn[$name];
39
		}
40 2
		if (isset($this->instances[$name])) {
41
			return $this->instances[$name];
42
		}
43 2
		foreach ($this->getFqcnList() as $fqcn) {
44 2
			$instance = \OCP\Server::get($fqcn);
45 2
			$settings = $instance->getSettings();
46 2
			if ($fqcn === $name || $settings->id === $name) {
47 2
				$instance->setAppConfig(\OCP\Server::get(\OCP\IAppConfig::class));
48 2
				$this->instances[$settings->id] = $instance;
49 2
				$this->instancesByFqcn[$fqcn] = $instance;
50 2
				return $instance;
51
			}
52
		}
53
		throw new InvalidProviderException("Provider <$name> does not exist");
54
	}
55
}
56