DispatchClosure   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 51
ccs 15
cts 15
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A dispatch() 0 16 2
A isSatisfiedBy() 0 6 1
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