Passed
Pull Request — master (#2)
by Florian
04:09 queued 02:04
created

FastRouteMiddleware::dispatch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.0116
1
<?php
2
declare(strict_types=1);
3
4
namespace Burzum\FastRouteMiddleware;
5
6
use Burzum\FastRouteMiddleware\Handler\FoundHandlerInterface;
7
use Burzum\FastRouteMiddleware\Handler\NotAllowedHandlerInterface;
8
use Burzum\FastRouteMiddleware\Handler\NotFoundHandlerInterface;
9
use Psr\Http\Server\MiddlewareInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
use Psr\Http\Message\ResponseInterface;
13
use FastRoute\Dispatcher as DispatcherInterface;
14
15
/**
16
 * FastRoute Dispatcher Middleware
17
 *
18
 * @link https://github.com/nikic/FastRoute
19
 */
20
class FastRouteMiddleware implements MiddlewareInterface
21
{
22
    /**
23
     * Fast Route Dispatcher
24
     *
25
     * @var \FastRoute\Dispatcher
26
     */
27
    protected $dispatcher;
28
29
    /**
30
     * Route not allowed Handler
31
     *
32
     * @var null|\Burzum\FastRouteMiddleware\Handler\NotAllowedHandlerInterface
33
     */
34
    protected $notAllowedHandler;
35
36
    /**
37
     * Route not found Handler
38
     *
39
     * @var null|\Burzum\FastRouteMiddleware\Handler\NotFoundHandlerInterface
40
     */
41
    protected $notFoundHandler;
42
43
    /**
44
     * Route Found Handler
45
     *
46
     * @var null|\Burzum\FastRouteMiddleware\Handler\FoundHandlerInterface
47
     */
48
    protected $foundHandler;
49
50
    /**
51
     * Route Attribute for the Request
52
     *
53
     * @var string
54
     */
55
    protected $routeAttribute = 'route';
56
57
    /**
58
     * Constructor
59
     *
60
     * @param \FastRoute\Dispatcher $dispatcher Fastroute Dispatcher
61
     */
62 1
    public function __construct(
63
        DispatcherInterface $dispatcher,
64
        ?FoundHandlerInterface $foundHandler = null,
65
        ?NotFoundHandlerInterface $notFoundHandler = null,
66
        ?NotAllowedHandlerInterface $notAllowedHandler = null
0 ignored issues
show
Unused Code introduced by
The parameter $notAllowedHandler is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
    ) {
68 1
        $this->dispatcher = $dispatcher;
69 1
        $this->foundHandler = $foundHandler;
70 1
        $this->notFoundHandler = $notFoundHandler;
71 1
        $this->notAllowedHandler = $notFoundHandler;
72 1
    }
73
74
    /**
75
     * Sets the route attribute name
76
     *
77
     * @param string $attributeName Attribute Name
78
     * @return $this
79
     */
80
    public function setRouteAttribute(string $attributeName): self
81
    {
82
        $this->routeAttribute = $attributeName;
83
84
        return $this;
85
    }
86
87
    /**
88
     * Process an incoming server request and return a response, optionally
89
     * delegating response creation to a handler.
90
     *
91
     * @param \Psr\Http\Message\ServerRequestInterface $request Request
92
     * @param \Psr\Http\Server\RequestHandlerInterface $requestHandler Request Handler
93
     * @return \Psr\Http\Message\ResponseInterface
94
     */
95 1
    public function process(ServerRequestInterface $request, RequestHandlerInterface $requestHandler): ResponseInterface
96
    {
97 1
        $routeInfo = $this->dispatch();
98 1
        $result = null;
99
100 1
        switch ($routeInfo[0]) {
101 1
            case DispatcherInterface::NOT_FOUND:
102 1
                if ($this->notFoundHandler !== null) {
103 1
                    $result = $this->notFoundHandler->handle($request);
104
                }
105 1
                break;
106 1
            case DispatcherInterface::METHOD_NOT_ALLOWED:
107
                if ($this->notAllowedHandler !== null) {
108
                    $result = $this->notAllowedHandler->handle($request, $routeInfo[1]);
109
                }
110
                break;
111 1
            case DispatcherInterface::FOUND:
112 1
                $request = $request->withAttribute(
113 1
                    $this->routeAttribute,
114 1
                    new RouteInfo($routeInfo[1], $routeInfo[2])
115
                );
116 1
                if ($this->foundHandler !== null) {
117 1
                    $result = $this->foundHandler->handle($request, $routeInfo[1], $routeInfo[2]);
118
                }
119 1
                break;
120
        }
121
122 1
        if ($result instanceof ResponseInterface) {
123
            return $result;
124
        }
125
126 1
        return $requestHandler->handle($request);
127
    }
128
129
    /**
130
     * Dispatches the Request URI
131
     *
132
     * @return array
133
     */
134 1
    protected function dispatch(): array
135
    {
136 1
        $httpMethod = $_SERVER['REQUEST_METHOD'];
137 1
        $uri = $_SERVER['REQUEST_URI'];
138
139
        // Strip query string (?foo=bar) and decode URI
140 1
        if (false !== $pos = strpos($uri, '?')) {
141
            $uri = substr($uri, 0, $pos);
142
        }
143 1
        $uri = rawurldecode($uri);
144
145 1
        return $this->dispatcher->dispatch($httpMethod, $uri);
146
    }
147
}
148