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

Bindings   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 38.46%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 65
ccs 10
cts 26
cp 0.3846
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setActiveKey() 0 5 1
A to() 0 5 1
A get() 0 4 1
A has() 0 4 1
A asSingleton() 0 4 1
A remove() 0 4 1
B merge() 0 16 6
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