Completed
Pull Request — master (#2)
by James Ekow Abaka
01:19
created

Bindings::to()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2
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
     *
14
     * @var array
15
     */
16
    private $bindings = [];
17
    
18
    /**
19
     * Key of the binding that would be altered with subsequent calls to a bindings object.
20
     *
21
     * @var string 
22
     */
23
    private $activeKey;
24
25
    /**
26
     * Set the key of the binding to be modified or accessed on subsequent calls.
27
     *
28
     * @param $activeKey
29
     * @return self
30
     */
31 10
    public function setActiveKey(string $activeKey) : self
32
    {
33 10
        $this->activeKey = $activeKey;
34 10
        return $this;
35
    }
36
37
    /**
38
     * Registers a method to be called when an instance is created for the current active binding.
39
     * This method could be a setter or some other optional initialization method. Parameters for methods registered
40
     * with call that have type hints are also injected.
41
     *
42
     * @param string $method
43
     * @param array $parameters
44
     * @return self
45
     */
46 3
    public function call(string $method, array $parameters = []) : self
47
    {
48 3
        $this->bindings[$this->activeKey]['calls'][] = [$method, $parameters];
49 3
        return $this;
50
    }
51
52
    /**
53
     * Provides a concrete class or factory function to which the currently selected binding should be linked.
54
     *
55
     * @param mixed $value
56
     * @return self
57
     */
58 12
    public function to($value)
59
    {
60 12
        if(isset($this->bindings[$this->activeKey])) {
61 1
            $this->bindings[$this->activeKey]['binding'] = $value;
62
        } else {
63 12
            $this->bindings[$this->activeKey] = ['binding' => $value, 'calls' => [], 'properties' => []];
64
        }
65 12
        return $this;
66
    }
67
68
    /**
69
     * Get the configuration of a binding.
70
     *
71
     * @param string $key
72
     * @return mixed
73
     */
74 12
    public function get(string $key)
75
    {
76 12
        return $this->bindings[$key];
77
    }
78
79
    /**
80
     * Checks if a binding exists.
81
     *
82
     * @param string $key
83
     * @return bool
84
     */
85 19
    public function has($key)
86
    {
87 19
        return isset($this->bindings[$key]);
88
    }
89
90
    /**
91
     * Register the current active binding as a singleton.
92
     *
93
     * @param bool $singleton
94
     */
95 3
    public function asSingleton($singleton = true)
96
    {
97 3
        $this->bindings[$this->activeKey]['singleton'] = $singleton;
98 3
    }
99
100
    /**
101
     * Setup bindings from an array.
102
     *
103
     * @param array $binding
104
     */
105 2
    private function setArrayBinding(array $binding)
106
    {
107 2
        if(isset($binding[0])) {
108 2
            $this->to($binding[0]);
109
        }
110
        
111 2
        if(isset($binding['calls'])){
112 1
            foreach($binding['calls'] as $call => $parameters) {
113 1
                if(is_numeric($call)) {
114 1
                    $call = $parameters;
115 1
                    $parameters = [];
116
                } 
117 1
                $this->call($call, $parameters);
118
            }
119
        } else {
120 1
            $this->bindings[$this->activeKey]['calls'] = [];
121
        }
122
123 2
        $this->asSingleton($binding['singleton'] ?? false);
124 2
    }
125
126
    /**
127
     * Merge current bindings with another configuration overwriting conflicting values with the new bindings.
128
     *
129
     * @param array $bindings
130
     */
131 5
    public function merge(array $bindings)
132
    {
133 5
        foreach($bindings as $key => $binding) {
134 5
            $this->activeKey = $key;
135 5
            if(is_array($binding)) {
136 2
                $this->setArrayBinding($binding);
137
            } else {
138 5
                $this->bindings[$key] = ['binding' => $binding, 'calls' => [], 'properties' => []];
139
            }
140
        }
141 5
    }
142
143
}
144