Passed
Push — master ( 0c3f71...c8d0a7 )
by Phillip
02:28
created

Container::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Deployee\Components\Container;
4
5
class Container implements ContainerInterface
6
{
7
    /**
8
     * @var \Pimple\Container
9
     */
10
    private $container;
11
12
    /**
13
     * @param array $values
14
     * @throws ContainerException
15
     */
16 3
    public function __construct(array $values = array())
17
    {
18 3
        $this->container = new \Pimple\Container();
19 3
        foreach ($values as $id => $value){
20 3
            $this->set($id, $value);
21
        }
22 3
    }
23
24
    /**
25
     * @param string $id
26
     * @return mixed
27
     */
28 2
    public function get(string $id)
29
    {
30 2
        return $this->container[$id];
31
    }
32
33
    /**
34
     * @param string $id
35
     * @param mixed $value
36
     * @return void
37
     * @throws ContainerException
38
     */
39 3
    public function set(string $id, $value)
40
    {
41 3
        if(isset($this->container[$id])){
42 1
            throw new ContainerException(sprintf(
43 1
                'Element with id %s already exists. You must use "extend" to modify the value',
44 1
                $id
45
            ));
46
        }
47
48 3
        if(is_callable($value)){
49 1
            $me = $this;
50 1
            $result = function() use($value, $me){
51 1
                return $value($me);
52 1
            };
53
54 1
            $value = $result;
55
        }
56
57 3
        $this->container[$id] = $value;
58 3
    }
59
60
    /**
61
     * @param string $id
62
     * @param callable $callable
63
     */
64 1
    public function extend(string $id, callable $callable)
65
    {
66 1
        $me = $this;
67 1
        $value = $me->get($id);
68 1
        $this->container[$id] = function() use($callable, $me, $value){
69 1
            return $callable($value, $me);
70
        };
71
    }
72
}