Passed
Push — feature/migration ( e715ab...e225e7 )
by Mathieu
22:09 queued 12:08
created

Container::getKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Suricate;
6
7
use InvalidArgumentException;
8
9
class Container implements \ArrayAccess
10
{
11
    private $content;
12
    private $warehouse = [];
13
14
    public function __construct(array $values = [])
15
    {
16
        $this->content = $values;
17
    }
18
19
    /**
20
     * \ArrayAccess offsetExists implementation
21
     *
22
     * @param mixed $offset offset to check
23
     * @return bool
24
     */
25
    public function offsetExists(mixed $offset): bool
26
    {
27
        return isset($this->content[$offset]);
28
    }
29
30
    /**
31
     * \ArrayAccess offsetGet implementation
32
     *
33
     * @param mixed $offset offset to get
34
     * @throws InvalidArgumentException
35
     * @return bool
36
     */
37
    public function offsetGet(mixed $offset): mixed
38
    {
39
        if (isset($this->content[$offset])) {
40
            return $this->content[$offset];
41
        }
42
43
        // Instantiate from warehouse if available
44
        if (isset($this->warehouse[$offset])) {
45
            $this->content[$offset] = new $this->warehouse[$offset]();
46
            return $this->content[$offset];
47
        }
48
49
        throw new InvalidArgumentException('Unknown service ' . $offset);
50
    }
51
52
    /**
53
     * \ArrayAccess offsetSet implementation
54
     *
55
     * @param mixed $offset offset to set
56
     * @param mixed $value  value to set
57
     * @return void
58
     */
59
    public function offsetSet(mixed $offset, mixed $value): void
60
    {
61
    }
62
63
    /**
64
     * \ArrayAccess offsetUnset implementation
65
     *
66
     * @param mixed $offset offset to unset
67
     * @return void
68
     */
69
    public function offsetUnset(mixed $offset): void
70
    {
71
        if (isset($this->content[$offset])) {
72
            unset($this->content[$offset]);
73
        }
74
    }
75
76
    /**
77
     * Set warehouse array
78
     *
79
     * @param array $serviceList warehouse content
80
     * @return Container
81
     */
82
    public function setWarehouse(array $serviceList)
83
    {
84
        $this->warehouse = $serviceList;
85
86
        return $this;
87
    }
88
89
    /**
90
     * Add Service to warehouse array
91
     *
92
     * @param string $serviceName
93
     * @param string $serviceClass
94
     * @return self
95
     */
96
    public function addToWarehouse(string $serviceName, string $serviceClass): self
97
    {
98
        $this->warehouse[$serviceName] = $serviceClass;
99
100
        return $this;
101
    }
102
103
    public function getKeys(): array
104
    {
105
        return array_keys($this->warehouse);
106
    }
107
}
108