Completed
Push — master ( caf90e...9fcb2b )
by Phil
01:24
created

Dispatcher::setFoundMiddleware()   B

Complexity

Conditions 6
Paths 20

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 14
cts 14
cp 1
rs 8.8337
c 0
b 0
f 0
cc 6
nc 20
nop 1
crap 6
1
<?php declare(strict_types=1);
2
3
namespace League\Route;
4
5
use FastRoute\Dispatcher as FastRoute;
6
use FastRoute\Dispatcher\GroupCountBased as GroupCountBasedDispatcher;
7
use League\Route\Http\Exception\{MethodNotAllowedException, NotFoundException};
8
use League\Route\Middleware\{MiddlewareAwareInterface, MiddlewareAwareTrait};
9
use League\Route\Strategy\{StrategyAwareInterface, StrategyAwareTrait};
10
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
11
use Psr\Http\Server\RequestHandlerInterface;
12
13
class Dispatcher extends GroupCountBasedDispatcher implements
14
    MiddlewareAwareInterface,
15
    RequestHandlerInterface,
16
    StrategyAwareInterface
17
{
18
    use MiddlewareAwareTrait;
19
    use StrategyAwareTrait;
20
21
    /**
22
     * Dispatch the current route
23
     *
24
     * @param ServerRequestInterface $request
25
     *
26
     * @return ResponseInterface
27
     */
28 32
    public function dispatchRequest(ServerRequestInterface $request) : ResponseInterface
29
    {
30 32
        $httpMethod = $request->getMethod();
31 32
        $uri        = $request->getUri()->getPath();
32 32
        $match      = $this->dispatch($httpMethod, $uri);
33
34 32
        switch ($match[0]) {
35 32
            case FastRoute::NOT_FOUND:
36 12
                $this->setNotFoundDecoratorMiddleware();
37 12
                break;
38 20
            case FastRoute::METHOD_NOT_ALLOWED:
39 4
                $allowed = (array) $match[1];
40 4
                $this->setMethodNotAllowedDecoratorMiddleware($allowed);
41 4
                break;
42 16
            case FastRoute::FOUND:
43 16
                $route = $this->ensureHandlerIsRoute($match[1], $httpMethod, $uri)->setVars($match[2]);
44 16
                $this->setFoundMiddleware($route);
45 16
                break;
46
        }
47
48 32
        return $this->handle($request);
49
    }
50
51
    /**
52
     * Ensure handler is a Route, honoring the contract of dispatchRequest.
53
     *
54
     * @param Route|mixed $matchingHandler
55
     * @param string      $httpMethod
56
     * @param string      $uri
57
     *
58
     * @return Route
59
     *
60
     */
61 16
    private function ensureHandlerIsRoute($matchingHandler, $httpMethod, $uri) : Route
62
    {
63 16
        if (is_a($matchingHandler, Route::class)) {
64 14
            return $matchingHandler;
65
        }
66 2
        return new Route($httpMethod, $uri, $matchingHandler);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 32
    public function handle(ServerRequestInterface $request) : ResponseInterface
73
    {
74 32
        $middleware = $this->shiftMiddleware();
75
76 32
        return $middleware->process($request, $this);
77
    }
78
79
    /**
80
     * Set up middleware for a found route
81
     *
82
     * @param Route $route
83
     *
84
     * @return void
85
     */
86 16
    protected function setFoundMiddleware(Route $route) : void
87
    {
88 16
        if ($route->getStrategy() === null) {
89 2
            $route->setStrategy($this->getStrategy());
90
        }
91
92 16
        $container = $route->getStrategy()->getContainer();
93
94 16
        foreach ($this->getMiddlewareStack() as $key => $middleware) {
95 2
            $this->middleware[$key] = $this->resolveMiddleware($middleware, $container);
96
        }
97
98
        // wrap entire dispatch process in exception handler
99 16
        $this->prependMiddleware($route->getStrategy()->getExceptionHandler());
100
101
        // add group and route specific middleware
102 16
        if ($group = $route->getParentGroup()) {
103 4
            foreach ($group->getMiddlewareStack() as $middleware) {
104 2
                $this->middleware($this->resolveMiddleware($middleware, $container));
105
            }
106
        }
107
108 16
        foreach ($route->getMiddlewareStack() as $middleware) {
109 2
            $this->middleware($this->resolveMiddleware($middleware, $container));
110
        }
111
112
        // add actual route to end of stack
113 16
        $this->middleware($route);
114 16
    }
115
116
    /**
117
     * Set up middleware for a not found route
118
     *
119
     * @return void
120
     */
121 12
    protected function setNotFoundDecoratorMiddleware() : void
122
    {
123 12
        $middleware = $this->getStrategy()->getNotFoundDecorator(new NotFoundException);
124 12
        $this->prependMiddleware($middleware);
125 12
    }
126
127
    /**
128
     * Set up middleware for a not allowed route
129
     *
130
     * @param array $allowed
131
     *
132
     * @return void
133
     */
134 4
    protected function setMethodNotAllowedDecoratorMiddleware(array $allowed) : void
135
    {
136 4
        $middleware = $this->getStrategy()->getMethodNotAllowedDecorator(
137 4
            new MethodNotAllowedException($allowed)
138
        );
139
140 4
        $this->prependMiddleware($middleware);
141 4
    }
142
}
143