Completed
Pull Request — master (#246)
by Derek Stephen
11:15 queued 10:03
created

Dispatcher::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 \Psr\Http\Message\ServerRequestInterface $request
25
     *
26
     * @return \Psr\Http\Message\ResponseInterface
27
     */
28 30
    public function dispatchRequest(ServerRequestInterface $request) : ResponseInterface
29
    {
30 30
        $match = $this->dispatch($request->getMethod(), $request->getUri()->getPath());
31
32 30
        switch ($match[0]) {
33 30
            case FastRoute::NOT_FOUND:
34 12
                $this->setNotFoundDecoratorMiddleware();
35 12
                break;
36 18
            case FastRoute::METHOD_NOT_ALLOWED:
37 4
                $allowed = (array) $match[1];
38 4
                $this->setMethodNotAllowedDecoratorMiddleware($allowed);
39 4
                break;
40 14
            case FastRoute::FOUND:
41 14
                $match[1]->setVars($match[2]);
42 14
                $this->setFoundMiddleware($match[1]);
43 14
                break;
44
        }
45
46 30
        return $this->handle($request);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 30
    public function handle(ServerRequestInterface $request) : ResponseInterface
53
    {
54 30
        $middleware = $this->shiftMiddleware();
55
56 30
        return $middleware->process($request, $this);
57
    }
58
59
    /**
60
     * Set up middleware for a found route
61
     *
62
     * @param \League\Route\Route $route
63
     *
64
     * @return void
65
     */
66 14
    protected function setFoundMiddleware(Route $route) : void
67
    {
68 14
        if (is_null($route->getStrategy())) {
69
            $route->setStrategy($this->getStrategy());
70
        }
71
72
        // wrap entire dispatch process in exception handler
73 14
        $this->prependMiddleware($route->getStrategy()->getExceptionHandler());
74
75
        // add group and route specific middleware
76 14
        if ($group = $route->getParentGroup()) {
77 4
            $this->middlewares($group->getMiddlewareStack());
78
        }
79
80 14
        $this->middlewares($route->getMiddlewareStack());
81
82
        // add actual route to end of stack
83 14
        $this->middleware($route);
84 14
    }
85
86
    /**
87
     * Set up middleware for a not found route
88
     *
89
     * @return void
90
     */
91 12
    protected function setNotFoundDecoratorMiddleware() : void
92
    {
93 12
        $middleware = $this->getStrategy()->getNotFoundDecorator(new NotFoundException);
94 12
        $this->prependMiddleware($middleware);
95 12
    }
96
97
    /**
98
     * Set up middleware for a not allowed route
99
     *
100
     * @param array $allowed
101
     *
102
     * @return void
103
     */
104 4
    protected function setMethodNotAllowedDecoratorMiddleware(array $allowed) : void
105
    {
106 4
        $middleware = $this->getStrategy()->getMethodNotAllowedDecorator(
107 4
            new MethodNotAllowedException($allowed)
108
        );
109
110 4
        $this->prependMiddleware($middleware);
111 4
    }
112
}
113