Definition   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 257
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

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

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A addTag() 0 6 1
A hasTag() 0 4 1
A setAlias() 0 6 1
A getAlias() 0 4 1
A setShared() 0 6 1
A isShared() 0 4 1
A getConcrete() 0 4 1
A setConcrete() 0 6 1
A addMethodCalls() 0 8 2
A addArgument() 0 6 1
A addArguments() 0 8 2
A addMethodCall() 0 9 1
B resolve() 0 34 10
A resolveCallable() 0 6 1
A resolveClass() 0 7 1
A invokeMethods() 0 9 2
1
<?php declare(strict_types=1);
2
3
namespace League\Container\Definition;
4
5
use League\Container\Argument\{
6
    ArgumentResolverInterface, ArgumentResolverTrait, ClassNameInterface, RawArgumentInterface
7
};
8
use League\Container\ContainerAwareTrait;
9
use ReflectionClass;
10
11
class Definition implements ArgumentResolverInterface, DefinitionInterface
12
{
13
    use ArgumentResolverTrait;
14
    use ContainerAwareTrait;
15
16
    /**
17
     * @var string
18
     */
19
    protected $alias;
20
21
    /**
22
     * @var mixed
23
     */
24
    protected $concrete;
25
26
    /**
27
     * @var boolean
28
     */
29
    protected $shared = false;
30
31
    /**
32
     * @var array
33
     */
34
    protected $tags = [];
35
36
    /**
37
     * @var array
38
     */
39
    protected $arguments = [];
40
41
    /**
42
     * @var array
43
     */
44
    protected $methods = [];
45
46
    /**
47
     * @var mixed
48
     */
49
    protected $resolved;
50
51
    /**
52
     * Constructor.
53
     *
54
     * @param string $id
55
     * @param mixed  $concrete
56
     */
57 69
    public function __construct(string $id, $concrete = null)
58
    {
59 69
        $concrete = $concrete ?? $id;
60
61 69
        $this->alias    = $id;
62 69
        $this->concrete = $concrete;
63 69
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 6
    public function addTag(string $tag) : DefinitionInterface
69
    {
70 6
        $this->tags[] = $tag;
71
72 6
        return $this;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 6
    public function hasTag(string $tag) : bool
79
    {
80 6
        return in_array($tag, $this->tags);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 36
    public function setAlias(string $id) : DefinitionInterface
87
    {
88 36
        $this->alias = $id;
89
90 36
        return $this;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 33
    public function getAlias() : string
97
    {
98 33
        return $this->alias;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 39
    public function setShared(bool $shared = true) : DefinitionInterface
105
    {
106 39
        $this->shared = $shared;
107
108 39
        return $this;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 45
    public function isShared() : bool
115
    {
116 45
        return $this->shared;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 6
    public function getConcrete()
123
    {
124 6
        return $this->concrete;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 3
    public function setConcrete($concrete) : DefinitionInterface
131
    {
132 3
        $this->concrete = $concrete;
133
134 3
        return $this;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140 15
    public function addArgument($arg) : DefinitionInterface
141
    {
142 15
        $this->arguments[] = $arg;
143
144 15
        return $this;
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150 3
    public function addArguments(array $args) : DefinitionInterface
151
    {
152 3
        foreach ($args as $arg) {
153 3
            $this->addArgument($arg);
154
        }
155
156 3
        return $this;
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162 3
    public function addMethodCall(string $method, array $args = []) : DefinitionInterface
163
    {
164 3
        $this->methods[] = [
165 3
            'method'    => $method,
166 3
            'arguments' => $args
167
        ];
168
169 3
        return $this;
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175 3
    public function addMethodCalls(array $methods = []) : DefinitionInterface
176
    {
177 3
        foreach ($methods as $method => $args) {
178 3
            $this->addMethodCall($method, $args);
179
        }
180
181 3
        return $this;
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187 45
    public function resolve(bool $new = false)
188
    {
189 45
        $concrete = $this->concrete;
190
191 45
        if ($this->isShared() && ! is_null($this->resolved) && $new === false) {
192 9
            return $this->resolved;
193
        }
194
195 45
        if (is_callable($concrete)) {
196 12
            $concrete = $this->resolveCallable($concrete);
197
        }
198
199 45
        if ($concrete instanceof RawArgumentInterface) {
200 3
            $this->resolved = $concrete->getValue();
201
202 3
            return $concrete->getValue();
203
        }
204
205 42
        if ($concrete instanceof ClassNameInterface) {
206 6
            $concrete = $concrete->getValue();
207
        }
208
209 42
        if (is_string($concrete) && class_exists($concrete)) {
210 33
            $concrete = $this->resolveClass($concrete);
211
        }
212
213 42
        if (is_object($concrete)) {
214 39
            $concrete = $this->invokeMethods($concrete);
215
        }
216
217 42
        $this->resolved = $concrete;
218
219 42
        return $concrete;
220
    }
221
222
    /**
223
     * Resolve a callable.
224
     *
225
     * @param callable $concrete
226
     *
227
     * @return mixed
228
     */
229 12
    protected function resolveCallable(callable $concrete)
230
    {
231 12
        $resolved = $this->resolveArguments($this->arguments);
232
233 12
        return call_user_func_array($concrete, $resolved);
234
    }
235
236
    /**
237
     * Resolve a class.
238
     *
239
     * @param string $concrete
240
     *
241
     * @return object
242
     */
243 33
    protected function resolveClass(string $concrete)
244
    {
245 33
        $resolved   = $this->resolveArguments($this->arguments);
246 33
        $reflection = new ReflectionClass($concrete);
247
248 33
        return $reflection->newInstanceArgs($resolved);
249
    }
250
251
    /**
252
     * Invoke methods on resolved instance.
253
     *
254
     * @param object $instance
255
     *
256
     * @return object
257
     */
258 39
    protected function invokeMethods($instance)
259
    {
260 39
        foreach ($this->methods as $method) {
261 3
            $args = $this->resolveArguments($method['arguments']);
262 3
            call_user_func_array([$instance, $method['method']], $args);
263
        }
264
265 39
        return $instance;
266
    }
267
}
268