Completed
Push — master ( b4ca26...e16bad )
by James Ekow Abaka
01:17
created

Bindings::merge()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 10
cp 0
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 10
nc 5
nop 2
crap 42
1
<?php
2
3
namespace ntentan\panie;
4
5
/**
6
 * Holds all the bindings for the dependency injection.
7
 */
8
class Bindings
9
{
10
11
    /**
12
     * An array of all the bindings.
13
     * @var array
14
     */
15
    private $bindings = [];
16
    
17
    /**
18
     * An active key that would be altered with subsequent calls to the bindings object.
19
     * @var string 
20
     */
21
    private $activeKey;
22
23 5
    public function setActiveKey($activeKey)
24
    {
25 5
        $this->activeKey = $activeKey;
26 5
        return $this;
27
    }
28
29 5
    public function to($value)
30
    {
31 5
        $this->bindings[$this->activeKey] = ['binding' => $value];
32 5
        return $this;
33
    }
34
35 5
    public function get($key)
36
    {
37 5
        return $this->bindings[$key];
38
    }
39
40 7
    public function has($key)
41
    {
42 7
        return isset($this->bindings[$key]);
43
    }
44
45
    public function asSingleton()
46
    {
47
        $this->bindings[$this->activeKey]['singleton'] = true;
48
    }
49
50
    public function remove($type)
51
    {
52
        unset($this->bindings[$type]);
53
    }
54
55
    public function merge($bindings, $replace)
56
    {
57
        foreach($bindings as $key => $binding) {
58
            if(isset($this->bindings[$key]) && !$replace) {
59
                continue;
60
            }
61
            if(is_array($binding)) {
62
                $this->bindings[$key] = ['binding' => $binding[0]];
63
                if(isset($binding['singleton'])) {
64
                    $this->bindings[$key]['singleton'] = $binding['singleton'];
65
                }
66
            } else {
67
                $this->bindings[$key] = ['binding' => $binding];
68
            }
69
        }
70
    }
71
72
}
73