Passed
Pull Request — master (#25)
by Mathieu
04:23 queued 01:40
created

ServiceContainer::offsetGet()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 14
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Suricate;
6
7
use InvalidArgumentException;
8
9
class ServiceContainer extends Container
10
{
11
    /**
12
     * @var array $warehouse Services warehouse
13
     */
14
    private $warehouse = [];
15
16
    /**
17
     * Set warehouse array
18
     *
19
     * @param array $serviceList warehouse content
20
     * @return ServiceContainer
21
     */
22
    public function setWarehouse(array $serviceList)
23
    {
24
        $this->warehouse = $serviceList;
25
26
        return $this;
27
    }
28
29
    public function addToWarehouse(string $serviceName, string $serviceClass)
30
    {
31
        $this->warehouse[$serviceName] = $serviceClass;
32
    }
33
34
    public function offsetExists($offset): bool
35
    {
36
        return isset($this->warehouse[$offset]);
37
    }
38
39
    /**
40
     * ArrayAccess offsetGet implementation
41
     *
42
     * @param mixed $offset offset to get
43
     * @throws InvalidArgumentException
44
     * @return mixed
45
     */
46
    public function offsetGet($offset)
47
    {
48
        // Service has already been inited, returning it
49
        if (isset($this->content[$offset])) {
0 ignored issues
show
Bug introduced by
The property content is declared private in Suricate\Container and cannot be accessed from this context.
Loading history...
50
            return $this->content[$offset];
51
        }
52
53
        // Instantiate a new copy from warehouse if available
54
        if (isset($this->warehouse[$offset])) {
55
            $this->content[$offset] = new $this->warehouse[$offset]();
56
            return $this->content[$offset];
57
        }
58
59
        throw new InvalidArgumentException('Unknown service ' . $offset);
60
    }
61
}
62