Passed
Pull Request — master (#41)
by Chema
07:01
created

AbstractClassResolver::addGlobal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\ClassResolver;
6
7
use Gacela\Framework\ClassResolver\ClassNameFinder\ClassNameFinderInterface;
8
9
abstract class AbstractClassResolver
10
{
11
    /** @var array<string,mixed|object> */
12
    protected static array $cachedInstances = [];
13
    protected static ?ClassNameFinderInterface $classNameFinder = null;
14
15
    /** @var array<string,object> */
16
    private static array $globalResolvedClasses = [];
17
18
    abstract public function resolve(object $callerClass): ?object;
19
20 1
    public static function addGlobal(string $key, object $resolvedClass): void
21
    {
22 1
        self::$globalResolvedClasses[$key] = $resolvedClass;
23 1
    }
24
25
    /**
26
     * @return null|mixed
27
     */
28 13
    public function doResolve(object $callerClass)
29
    {
30 13
        $classInfo = new ClassInfo($callerClass);
31 13
        $cacheKey = $this->getCacheKey($classInfo);
32
33 13
        if (isset(self::$cachedInstances[$cacheKey])) {
34
            return self::$cachedInstances[$cacheKey];
35
        }
36
37 13
        $resolvedClassName = $this->findClassName($classInfo);
38
39 13
        if (null === $resolvedClassName) {
40 4
            return $this->resolveGlobal($cacheKey);
41
        }
42
43 11
        self::$cachedInstances[$cacheKey] = $this->createInstance($resolvedClassName);
44
45 11
        return self::$cachedInstances[$cacheKey];
46
    }
47
48
    abstract protected function getResolvableType(): string;
49
50
    /**
51
     * @return null|mixed
52
     */
53 4
    private function resolveGlobal(string $cacheKey)
54
    {
55 4
        $resolvedClass = self::$globalResolvedClasses[$cacheKey] ?? null;
56
57 4
        if (null === $resolvedClass) {
58 3
            return null;
59
        }
60
61 1
        self::$cachedInstances[$cacheKey] = $resolvedClass;
62
63 1
        return self::$cachedInstances[$cacheKey];
64
    }
65
66 13
    private function getCacheKey(ClassInfo $classInfo): string
67
    {
68 13
        return $classInfo->getCacheKey($this->getResolvableType());
69
    }
70
71 13
    private function findClassName(ClassInfo $classInfo): ?string
72
    {
73 13
        return $this->getClassNameFinder()->findClassName(
74 13
            $classInfo,
75 13
            $this->getResolvableType()
76
        );
77
    }
78
79 13
    private function getClassNameFinder(): ClassNameFinderInterface
80
    {
81 13
        if (null === self::$classNameFinder) {
82 1
            $classResolverFactory = new ClassResolverFactory();
83 1
            self::$classNameFinder = $classResolverFactory->createClassNameFinder();
84
        }
85
86 13
        return self::$classNameFinder;
87
    }
88
89
    /**
90
     * @return null|object
91
     */
92 11
    private function createInstance(string $resolvedClassName)
93
    {
94 11
        if (class_exists($resolvedClassName)) {
95
            /** @psalm-suppress MixedMethodCall */
96 11
            return new $resolvedClassName();
97
        }
98
99
        return null;
100
    }
101
}
102