Completed
Push — master ( eed05d...5ff1a2 )
by James Ekow Abaka
01:21
created

Bindings   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 44.83%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 70
ccs 13
cts 29
cp 0.4483
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setActiveKey() 0 5 1
A call() 0 4 1
A setProperty() 0 4 1
A to() 0 5 1
A get() 0 4 1
A has() 0 4 1
A asSingleton() 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
    public function call($method, $parameters = [])
30
    {
31
        $this->bindings[$this->activeKey]['calls'][] = ['setter' => $method, 'parameters' => $parameters];
32
    }
33
34
    public function setProperty($property, $binding)
35
    {
36
        $this->bindings[$this->activeKey]['sets'][] = ['property' => $property, 'binding' => $binding];
37
    }
38
39 5
    public function to($value)
40
    {
41 5
        $this->bindings[$this->activeKey] = ['binding' => $value, 'calls'=>[], 'properties' => []];
42 5
        return $this;
43
    }
44
45 5
    public function get($key)
46
    {
47 5
        return $this->bindings[$key];
48
    }
49
50 7
    public function has($key)
51
    {
52 7
        return isset($this->bindings[$key]);
53
    }
54
55 1
    public function asSingleton()
56
    {
57 1
        $this->bindings[$this->activeKey]['singleton'] = true;
58 1
    }    
59
60
    public function merge($bindings, $replace = true)
61
    {
62
        foreach($bindings as $key => $binding) {
63
            if(isset($this->bindings[$key]) && !$replace) {
64
                continue;
65
            }
66
            if(is_array($binding)) {
67
                $this->bindings[$key] = ['binding' => $binding[0]];
68
                if(isset($binding['singleton'])) {
69
                    $this->bindings[$key]['singleton'] = $binding['singleton'];
70
                }
71
            } else {
72
                $this->bindings[$key] = ['binding' => $binding];
73
            }
74
        }
75
    }
76
77
}
78