DispatchController   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 48
ccs 13
cts 13
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A dispatch() 0 17 2
1
<?php
2
3
namespace Mosaic\Routing\Dispatchers;
4
5
use Mosaic\Container\Container;
6
use Mosaic\Routing\Exceptions\NotFoundHttpException;
7
use Mosaic\Routing\MethodParameterResolver;
8
use Mosaic\Routing\Route;
9
use ReflectionMethod;
10
11
class DispatchController implements Dispatcher
12
{
13
    /**
14
     * @var Container
15
     */
16
    private $container;
17
18
    /**
19
     * @var MethodParameterResolver
20
     */
21
    private $resolver;
22
23
    /**
24
     * @param Container               $container
25
     * @param MethodParameterResolver $resolver
26
     */
27 3
    public function __construct(Container $container, MethodParameterResolver $resolver)
28
    {
29 3
        $this->container = $container;
30 3
        $this->resolver  = $resolver;
31 3
    }
32
33
    /**
34
     * Dispatch the request
35
     *
36
     * @param  Route                 $route
37
     * @param  callable              $next
38
     * @throws NotFoundHttpException
39
     * @return mixed
40
     */
41 3
    public function dispatch(Route $route, callable $next)
42
    {
43 3
        $action = $route->action();
44
45 3
        list($class, $method) = explode('@', $action['uses']);
46
47 3
        if (!method_exists($instance = $this->container->make($class), $method)) {
48 1
            throw new NotFoundHttpException;
49
        }
50
51 1
        $parameters = $this->resolver->resolve(
52 1
            new ReflectionMethod($instance, $method),
53 1
            $route->parameters()
54
        );
55
56 1
        return $this->container->call([$instance, $method], $parameters);
57
    }
58
}
59