Passed
Push — bugfix/support-windows ( 0e99e7...b5ecf8 )
by Jesús
06:33 queued 02:55
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\Framework\ClassResolver\GlobalInstance\AnonymousGlobal;
8
9
/**
10
 * @internal
11
 */
12
final class Locator implements LocatorInterface
13
{
14
    private static ?Locator $instance = null;
15
16
    /** @var array<string, mixed> */
17
    private array $instanceCache = [];
18
19
    private ContainerInterface $container;
20
21 7
    private function __construct(
22
        ?ContainerInterface $container = null,
23
    ) {
24 7
        $this->container = $container ?? new Container();
25
    }
26
27
    /**
28
     * @codeCoverageIgnore
29
     */
30
    private function __clone()
31
    {
32
    }
33
34 51
    public static function resetInstance(): void
35
    {
36 51
        self::$instance = null;
37
    }
38
39
    /**
40
     * @template T
41
     *
42
     * @param class-string<T> $key
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...
43
     * @param T $value
44
     */
45 1
    public static function addSingleton(string $key, mixed $value): void
46
    {
47 1
        self::getInstance()->add($key, $value);
48
    }
49
50
    /**
51
     * @template T
52
     *
53
     * @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...
54
     *
55
     * @return T|null
56
     */
57 9
    public static function getSingleton(string $className, ?Container $container = null)
58
    {
59 9
        return self::getInstance($container)->get($className);
60
    }
61
62 11
    public static function getInstance(?Container $container = null): self
63
    {
64 11
        if (self::$instance === null) {
65 7
            self::$instance = new self($container);
66
        }
67
68 11
        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...
69
    }
70
71
    /**
72
     * @template T
73
     *
74
     * @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...
75
     *
76
     * @return T|null
77
     */
78 11
    public function get(string $className)
79
    {
80 11
        if (isset($this->instanceCache[$className])) {
81
            /** @var T $instance */
82 7
            $instance = $this->instanceCache[$className];
83
84 7
            return $instance;
85
        }
86
87
        /** @var T|null $locatedInstance */
88 6
        $locatedInstance = AnonymousGlobal::getByClassName($className)
89 6
            ?? $this->container->get($className);
90
91 6
        $this->add($className, $locatedInstance);
92
93 6
        return $locatedInstance;
94
    }
95
96
    /**
97
     * @template T
98
     *
99
     * @param class-string<T> $key
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...
100
     * @param T|null $value
101
     */
102 7
    private function add(string $key, mixed $value = null): self
103
    {
104 7
        $this->instanceCache[$key] = $value;
105
106 7
        return $this;
107
    }
108
}
109