Passed
Push — add-locator-sigleton-static-fu... ( 89c92e )
by Chema
04:15
created

Locator::addSingleton()   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\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 1
    public static function addSingleton(string $key, mixed $value): void
34
    {
35 1
        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 2
    public static function getSingleton(string $className)
46
    {
47 2
        return self::getInstance()->get($className);
48
    }
49
50 1
    public function add(string $key, mixed $value): self
51
    {
52 1
        $this->instanceCache[$key] = $value;
53
54 1
        return $this;
55
    }
56
57 5
    public static function getInstance(): self
58
    {
59 5
        if (self::$instance === null) {
60 5
            self::$instance = new self();
61
        }
62
63 5
        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...
64
    }
65
66
    /**
67
     * @template T
68
     *
69
     * @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...
70
     *
71
     * @return T|null
72
     */
73 5
    public function get(string $className)
74
    {
75 5
        if (isset($this->instanceCache[$className])) {
76
            /** @var T $instance */
77 2
            $instance = $this->instanceCache[$className];
78
79 2
            return $instance;
80
        }
81
82
        /** @var T|null $locatedInstance */
83 4
        $locatedInstance = AnonymousGlobal::getByClassName($className)
84 4
            ?? GacelaContainer::create($className);
85
86
        /** @psalm-suppress MixedAssignment */
87 4
        $this->instanceCache[$className] = $locatedInstance;
88
89 4
        return $locatedInstance;
90
    }
91
}
92