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

Factory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 89.47%

Importance

Changes 0
Metric Value
wmc 9
eloc 22
dl 0
loc 37
ccs 17
cts 19
cp 0.8947
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getPrefix() 0 3 1
A getBaseClass() 0 3 1
A getSuffix() 0 3 1
A get() 0 19 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\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