Completed
Push — master ( f2de0d...859793 )
by Damien
02:37
created

DispatcherLayer::in()   C

Complexity

Conditions 10
Paths 20

Size

Total Lines 59
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 59
rs 6.5919
cc 10
eloc 30
nc 20
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Veto.
4
 * PHP Microframework.
5
 *
6
 * @author Damien Walsh <[email protected]>
7
 * @copyright Damien Walsh 2013-2014
8
 * @version 0.1
9
 * @package veto
10
 */
11
namespace Veto\Layer\Dispatcher;
12
13
use Veto\DI\AbstractContainerAccessor;
14
use Veto\DI\Container;
15
use Veto\HTTP\Request;
16
use Veto\HTTP\Response;
17
use Veto\Layer\Dispatcher\Exception\DispatcherException;
18
use Veto\Layer\InboundLayerInterface;
19
20
/**
21
 * DispatcherLayer
22
 *
23
 * Dispatches a Request to a controller, obtaining a Response.
24
 */
25
class DispatcherLayer extends AbstractContainerAccessor implements InboundLayerInterface
26
{
27
    /**
28
     * Set the service container.
29
     *
30
     * @param Container $container
31
     */
32
    public function __construct(Container $container)
33
    {
34
        $this->container = $container;
35
    }
36
37
    /**
38
     * @param Request $request
39
     * @return mixed
40
     * @throws DispatcherException
41
     */
42
    public function in(Request $request)
43
    {
44
        // Get the controller
45
        $controllerSpec = $request->getParameter('_controller');
46
47
        if (!$controllerSpec) {
48
            throw DispatcherException::notTagged(
49
                $request->getMethod(),
50
                $request->getUri() ? $request->getUri()->getPath() : ''
51
            );
52
        }
53
54
        $controller = $this->container->get($controllerSpec['class']);
55
56
        if (!method_exists($controller, $controllerSpec['method'])) {
57
            throw DispatcherException::controllerActionDoesNotExist(
58
                $controllerSpec['method'],
59
                $controllerSpec['class']
60
            );
61
        }
62
63
        // Prepare to run the action method
64
        $actionMethod = new \ReflectionMethod($controller, $controllerSpec['method']);
65
        $parameters = $actionMethod->getParameters();
66
        $passedArgs = array();
67
68
        foreach ($parameters as $parameter) {
69
70
            $hintedClass = $parameter->getClass();
71
            $parameterName = $parameter->getName();
0 ignored issues
show
Bug introduced by
Consider using $parameter->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
72
73
            if ($hintedClass) {
74
                $hintedClass = $hintedClass->getName();
0 ignored issues
show
Bug introduced by
Consider using $hintedClass->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
75
            }
76
77
            // Special case - should the Request object be passed here?
78
            if ($parameterName == 'request' && $hintedClass == 'Veto\HTTP\Request') {
79
                $passedArgs[] = $request;
80
            }
81
82
            // Should a request parameter be passed here?
83
            if ($request->hasParameter($parameterName)) {
84
                $passedArgs[] = $request->getParameter($parameterName);
85
            }
86
        }
87
88
        $response = $actionMethod->invokeArgs($controller, $passedArgs);
89
90
        // By the end of the inbound layer list, a response should have been obtained
91
        if (!$response instanceof Response) {
92
            throw DispatcherException::controllerActionDidNotReturnResponse(
93
                $controllerSpec['method'],
94
                $controllerSpec['class'],
95
                gettype($response)
96
            );
97
        }
98
99
        return $response;
100
    }
101
}
102