DispatchClosure::dispatch()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Mosaic\Routing\Dispatchers;
4
5
use Mosaic\Routing\MethodParameterResolver;
6
use Mosaic\Routing\Route;
7
use ReflectionFunction;
8
9
class DispatchClosure implements Dispatcher
10
{
11
    /**
12
     * @var MethodParameterResolver
13
     */
14
    private $resolver;
15
16
    /**
17
     * @param MethodParameterResolver $resolver
18
     */
19 2
    public function __construct(MethodParameterResolver $resolver)
20
    {
21 2
        $this->resolver = $resolver;
22 2
    }
23
24
    /**
25
     * Dispatch the request
26
     *
27
     * @param  Route    $route
28
     * @param  callable $next
29
     * @return mixed
30
     */
31 2
    public function dispatch(Route $route, callable $next)
32
    {
33 2
        if (!$this->isSatisfiedBy($route)) {
34 1
            return $next($route);
35
        }
36
37 1
        $action   = $route->action();
38 1
        $callback = $action['uses'];
39
40 1
        $parameters = $this->resolver->resolve(
41 1
            new ReflectionFunction($callback),
42 1
            $route->parameters()
43
        );
44
45 1
        return call_user_func_array($callback, $parameters);
46
    }
47
48
    /**
49
     * @param Route $route
50
     *
51
     * @return bool
52
     */
53 2
    private function isSatisfiedBy(Route $route)
54
    {
55 2
        $action = $route->action();
56
57 2
        return is_callable($action['uses']);
58
    }
59
}
60