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

Factory::getBaseClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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