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

Factory::get()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

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