Passed
Push — master ( a5ef88...f5262c )
by Mathieu
14:16
created

Container::setWarehouse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
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 13
    public function __construct(array $values = [])
15
    {
16 13
        $this->content = $values;
17 13
    }
18
19
    /**
20
     * \ArrayAccess offsetExists implementation
21
     *
22
     * @param mixed $offset offset to check
23
     * @return bool
24
     */
25 3
    public function offsetExists(mixed $offset): bool
26
    {
27 3
        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 22
    public function offsetGet(mixed $offset): mixed
38
    {
39 22
        if (isset($this->content[$offset])) {
40 20
            return $this->content[$offset];
41
        }
42
43
        // Instantiate from warehouse if available
44 12
        if (isset($this->warehouse[$offset])) {
45 11
            $this->content[$offset] = new $this->warehouse[$offset]();
46 11
            return $this->content[$offset];
47
        }
48
49 1
        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 1
    public function offsetSet(mixed $offset, mixed $value): void
60
    {
61 1
    }
62
63
    /**
64
     * \ArrayAccess offsetUnset implementation
65
     *
66
     * @param mixed $offset offset to unset
67
     * @return void
68
     */
69 1
    public function offsetUnset(mixed $offset): void
70
    {
71 1
        if (isset($this->content[$offset])) {
72 1
            unset($this->content[$offset]);
73
        }
74 1
    }
75
76
    /**
77
     * Set warehouse array
78
     *
79
     * @param array $serviceList warehouse content
80
     * @return Container
81
     */
82 10
    public function setWarehouse(array $serviceList)
83
    {
84 10
        $this->warehouse = $serviceList;
85
86 10
        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