Passed
Pull Request — master (#640)
by Vitor
03:36
created

Factory::getBaseClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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