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

AFactory::get()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.5923

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 20
ccs 8
cts 12
cp 0.6667
rs 9.8333
c 1
b 0
f 0
cc 4
nc 4
nop 1
crap 4.5923
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
6
 * SPDX-License-Identifier: AGPL-3.0-or-later
7
 */
8
9
namespace OCA\TwoFactorGateway\Provider;
10
11
/**
12
 * @template T of object
13
 */
14
abstract class AFactory {
15
	/** @var array<string, T> */
16
	protected array $instances = [];
17
	/** @var array<string> */
18
	protected array $fqcn = [];
19
20
	abstract protected function getPrefix(): string;
21
22
	abstract protected function getSuffix(): string;
23
24
	/** @return class-string<T> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T>.
Loading history...
25 1
	abstract protected function getBaseClass(): string;
26 1
27
	/**
28
	 * @param class-string<T> $name
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T>.
Loading history...
29
	 * @return T
30 1
	 */
31 1
	public function get(string $name): object {
32 1
		$needle = match(str_contains($name, '\\')) {
33
			true => $this->typeFrom($name),
34
			false => strtolower($name),
35
		};
36 1
		if (isset($this->instances[$needle])) {
37 1
			return $this->instances[$needle];
38 1
		}
39 1
40
		foreach ($this->getFqcnList() as $fqcn) {
41
			$type = $this->typeFrom($fqcn);
42
			if ($type !== $needle) {
43
				continue;
44
			}
45
			$instance = \OCP\Server::get($fqcn);
46 1
			$this->instances[$type] = $instance;
47
			return $instance;
48
		}
49
50 2
		throw new \InvalidArgumentException("Invalid type <$name>");
51 2
	}
52
53
	/** @return array<string> */
54
	public function getFqcnList(): array {
55 2
		if (!empty($this->fqcn)) {
56 2
			return $this->fqcn;
57 2
		}
58 2
59 2
		$loader = require __DIR__ . '/../../vendor/autoload.php';
60
		foreach ($loader->getClassMap() as $fqcn => $_) {
61 2
			$type = $this->typeFrom($fqcn);
62
			if ($type === null) {
63
				continue;
64 2
			}
65
			if (!is_subclass_of($fqcn, $this->getBaseClass(), true)) {
66
				continue;
67 2
			}
68
			$this->fqcn[] = $fqcn;
69 2
		}
70
		return $this->fqcn;
71
	}
72 2
73 2
	protected function typeFrom(string $fqcn): ?string {
74 2
		$prefix = $this->getPrefix();
75 2
		if (strncmp($fqcn, $prefix, strlen($prefix)) !== 0) {
76
			return null;
77 2
		}
78 2
		$suffix = $this->getSuffix();
79 1
		if (!str_ends_with($fqcn, $suffix)) {
80
			return null;
81 2
		}
82 2
		$type = substr($fqcn, strlen($prefix));
83 2
		$type = substr($type, 0, -strlen('\\' . $suffix));
84
		if (strpos($type, '\\') !== false || $type === '') {
85
			return null;
86 2
		}
87
		return strtolower($type);
88
	}
89
}
90