Completed
Pull Request — master (#2)
by Mathieu
05:06 queued 01:16
created

Container::offsetGet()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

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