Passed
Pull Request — master (#686)
by Vitor
04:21
created

Factory::getPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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