Invoker::invoke()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 14
cts 14
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 13
nc 4
nop 2
crap 4
1
<?php
2
3
namespace mindplay\walkway;
4
5
use Closure;
6
use ReflectionFunction;
7
8
/**
9
 * This is the default invoker implementation, which is only capable of resolving
10
 * parameters directly provided.
11
 */
12
class Invoker implements InvokerInterface
13
{
14 1
    public function invoke(Closure $func, array $params)
15
    {
16 1
        $func_ref = new ReflectionFunction($func);
17 1
        $param_refs = $func_ref->getParameters();
18
19 1
        $args = array();
20
21 1
        foreach ($param_refs as $param_ref) {
22 1
            if (array_key_exists($param_ref->name, $params)) {
23 1
                $args[] = $params[$param_ref->name];
24
25 1
                continue;
26
            }
27
28 1
            if ($param_ref->isDefaultValueAvailable()) {
29 1
                $args[] = $param_ref->getDefaultValue();
30
31 1
                continue;
32
            }
33
34 1
            throw new InvocationException("missing parameter: \${$param_ref->name}", $func);
35 1
        }
36
37 1
        return call_user_func_array($func, $args);
38
    }
39
}
40