Passed
Push — add-pre-plugins ( a890af...421a8c )
by Chema
02:48
created

Locator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 18
c 1
b 0
f 1
dl 0
loc 80
ccs 23
cts 23
cp 1
rs 10
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A __clone() 0 2 1
A addSingleton() 0 3 1
A resetInstance() 0 3 1
A getSingleton() 0 3 1
A add() 0 5 1
A getInstance() 0 7 2
A get() 0 17 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Container;
6
7
use Gacela\Container\Container as GacelaContainer;
8
use Gacela\Framework\ClassResolver\GlobalInstance\AnonymousGlobal;
9
10
final class Locator
11
{
12
    private static ?Locator $instance = null;
13
14
    /** @var array<string, mixed> */
15
    private array $instanceCache = [];
16
17 5
    private function __construct()
18
    {
19 5
    }
20
21
    /**
22
     * @codeCoverageIgnore
23
     */
24
    private function __clone()
25
    {
26
    }
27
28 4
    public static function resetInstance(): void
29
    {
30 4
        self::$instance = null;
31
    }
32
33 2
    public static function addSingleton(string $key, mixed $value): void
34
    {
35 2
        self::getInstance()->add($key, $value);
36
    }
37
38
    /**
39
     * @template T
40
     *
41
     * @param class-string<T> $className
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...
42
     *
43
     * @return T|null
44
     */
45 3
    public static function getSingleton(string $className)
46
    {
47 3
        return self::getInstance()->get($className);
48
    }
49
50 6
    public static function getInstance(): self
51
    {
52 6
        if (self::$instance === null) {
53 5
            self::$instance = new self();
54
        }
55
56 6
        return self::$instance;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::instance could return the type null which is incompatible with the type-hinted return Gacela\Framework\Container\Locator. Consider adding an additional type-check to rule them out.
Loading history...
57
    }
58
59
    /**
60
     * @template T
61
     *
62
     * @param class-string<T> $className
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...
63
     *
64
     * @return T|null
65
     */
66 6
    public function get(string $className)
67
    {
68 6
        if (isset($this->instanceCache[$className])) {
69
            /** @var T $instance */
70 3
            $instance = $this->instanceCache[$className];
71
72 3
            return $instance;
73
        }
74
75
        /** @var T|null $locatedInstance */
76 4
        $locatedInstance = AnonymousGlobal::getByClassName($className)
77 4
            ?? GacelaContainer::create($className);
78
79
        /** @psalm-suppress MixedAssignment */
80 4
        $this->instanceCache[$className] = $locatedInstance;
81
82 4
        return $locatedInstance;
83
    }
84
85 2
    private function add(string $key, mixed $value): self
86
    {
87 2
        $this->instanceCache[$key] = $value;
88
89 2
        return $this;
90
    }
91
}
92