Completed
Push — master ( ea0713...bd13d1 )
by Vitor
15s
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\Telegram\Provider\IProvider;
15
16
class Factory extends AFactory {
17
	/** @var array<string,IProvider> */
18
	protected array $instances = [];
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 IProvider::class;
32
	}
33
34
	#[\Override]
35
	public function isValid(string $fqcn): bool {
36
		return defined("$fqcn::SCHEMA")
37
			&& is_array($fqcn::SCHEMA);
38
	}
39
40
	#[\Override]
41
	public function get(string $name): IProvider {
42
		if (isset($this->instances[$name])) {
43
			return $this->instances[$name];
44
		}
45
		foreach ($this->getFqcnList() as $fqcn) {
46
			if ($fqcn::getProviderId() === $name) {
47
				$instance = \OCP\Server::get($fqcn);
48
				$instance->setAppConfig(\OCP\Server::get(\OCP\IAppConfig::class));
49
				$this->instances[$name] = $instance;
50
				return $instance;
51
			}
52
		}
53
		throw new InvalidProviderException("Provider <$name> does not exist");
54
	}
55
}
56