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

Factory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 20
dl 0
loc 37
rs 10
c 0
b 0
f 0

5 Methods

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