Passed
Push — master ( 5c7e77...f5ca5a )
by Thomas
03:03
created

ServiceContainer::offsetUnset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace MadWizard\WebAuthn\Builder;
4
5
use ArrayAccess;
6
use MadWizard\WebAuthn\Exception\WebAuthnException;
7
use Psr\Log\LoggerAwareInterface;
8
use Psr\Log\LoggerInterface;
9
use RuntimeException;
10
11
/**
12
 * @implements ArrayAccess<string, object>
13
 */
14
class ServiceContainer implements ArrayAccess
15
{
16
    /**
17
     * @phpstan-var array<class-string, object>
18
     *
19
     * @var object[]
20
     */
21
    private $serviceMap = [];
22
23
    /**
24
     * @phpstan-var array<class-string, callable(self): object>
25
     *
26
     * @var callable[]
27
     */
28
    private $instantiators = [];
29
30
    /**
31
     * @param string $offset
32
     * @param string $offset
33
     * @phpstan-param class-string $offset
34
     *
35
     * @return bool
36
     */
37
    public function offsetExists($offset)
38
    {
39
        return isset($this->serviceMap[$offset]) || isset($this->instantiators[$offset]);
40
    }
41
42
    /**
43
     * @template T of object
44
     *
45
     * @param string $service
46
     * @phpstan-param class-string<T> $service
47
     * @phpstan-return T
48
     */
49 18
    public function offsetGet($service)
50
    {
51
        /**
52
         * @phpstan-var T
53
         */
54 18
        $service = ($this->serviceMap[$service] ?? $this->instantiate($service));
55 18
        return $service;
56
    }
57
58
    /**
59
     * @param string $offset
60
     */
61
    public function offsetUnset($offset)
62
    {
63
        throw new RuntimeException('Unset operation is not supported.');
64
    }
65
66
    /**
67
     * @param string   $offset
68
     * @param callable $value
69
     * @phpstan-param class-string $offset
70
     * @phpstan-param callable(self): object $value
71
     */
72 18
    public function offsetSet($offset, $value)
73
    {
74 18
        $this->instantiators[$offset] = $value;
75 18
    }
76
77
    /**
78
     * @template T of object
79
     * @phpstan-param class-string<T> $offset
80
     * @phpstan-return T of object
81
     */
82 18
    private function instantiate(string $offset): object
83
    {
84 18
        $instantiator = $this->instantiators[$offset] ?? null;
85 18
        if ($instantiator === null) {
86
            throw new WebAuthnException(sprintf('Missing service %s.', $offset));
87
        }
88
        /**
89
         * @phpstan-var T
90
         */
91 18
        $service = $instantiator($this);
92 18
        $this->serviceMap[$offset] = $service;
93
94 18
        if ($service instanceof LoggerAwareInterface) {
95 18
            $service->setLogger($this[LoggerInterface::class]);
96
        }
97
        // Free instantatior resources:
98 18
        unset($this->instantiators[$offset]);
99 18
        return $service;
100
    }
101
}
102