Factory::get()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.128

Importance

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