Container::offsetSet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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