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

ServiceContainer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 86
ccs 15
cts 20
cp 0.75
rs 10
c 1
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetSet() 0 3 1
A offsetUnset() 0 3 1
A offsetGet() 0 7 1
A offsetExists() 0 3 2
A instantiate() 0 18 3
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