Completed
Pull Request — master (#1)
by Woody
27:07 queued 01:07
created

DispatchHandler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 82
ccs 26
cts 26
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 22 2
A dispatcher() 0 10 2
A dispatch() 0 16 3
1
<?php
2
3
namespace Equip\Handler;
4
5
use FastRoute;
6
use FastRoute\Dispatcher;
7
use FastRoute\RouteCollector;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Equip\Directory;
11
use Equip\Exception\HttpException;
12
use Equip\Handler\ActionHandler;
13
14
class DispatchHandler
15
{
16
    /**
17
     * @var Directory
18
     */
19
    private $directory;
20
21
    /**
22
     * @param Directory $directory
23
     */
24 3
    public function __construct(Directory $directory)
25
    {
26 3
        $this->directory = $directory;
27 3
    }
28
29
    /**
30
     * @inheritDoc
31
     */
32 3
    public function __invoke(
33
        ServerRequestInterface $request,
34
        ResponseInterface $response,
35
        callable $next
36
    ) {
37
        /**
38
         * @var $action Arbiter\Action
39
         */
40 3
        list($action, $args) = $this->dispatch(
41 3
            $this->dispatcher(),
42 3
            $request->getMethod(),
43 3
            $request->getUri()->getPath()
44
        );
45
46 1
        $request = $request->withAttribute(ActionHandler::ACTION_ATTRIBUTE, $action);
47
48 1
        foreach ($args as $key => $value) {
49 1
            $request = $request->withAttribute($key, $value);
50
        }
51
52 1
        return $next($request, $response);
53
    }
54
55
    /**
56
     * @return Dispatcher
57
     */
58
    protected function dispatcher()
59
    {
60 3
        return FastRoute\simpleDispatcher(function (RouteCollector $collector) {
61 3
            foreach ($this->directory as $request => $action) {
62
                // 'GET /foo' becomes ['GET', '/foo']
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
63 2
                list($method, $path) = explode(' ', $request, 2);
64 2
                $collector->addRoute($method, $path, $action);
65
            }
66 3
        });
67
    }
68
69
    /**
70
     * @param Dispatcher $dispatcher
71
     * @param string $method
72
     * @param string $path
73
     *
74
     * @return [Action, $arguments]
0 ignored issues
show
Documentation introduced by
The doc-type Action,">[Action, could not be parsed: Unknown type name "[" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
75
     *
76
     * @throws HttpNotFound
77
     * @throws HttpMethodNotAllowed
78
     */
79 3
    private function dispatch(Dispatcher $dispatcher, $method, $path)
80
    {
81 3
        $route = $dispatcher->dispatch($method, $path);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
82 3
        $status = array_shift($route);
83
84 3
        if (Dispatcher::FOUND === $status) {
85 1
            return $route;
86
        }
87
88 2
        if (Dispatcher::METHOD_NOT_ALLOWED === $status) {
89 1
            $allowed = array_shift($route);
90 1
            throw HttpException::methodNotAllowed($path, $method, $allowed);
91
        }
92
93 1
        throw HttpException::notFound($path);
94
    }
95
}
96