Dispatcher   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 0
loc 104
ccs 36
cts 38
cp 0.9474
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A dispatchRequest() 0 20 4
A handle() 0 10 2
A setNotFoundDecoratorMiddleware() 0 5 1
A setMethodNotAllowedDecoratorMiddleware() 0 8 1
A setFoundMiddleware() 0 19 3
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 OutOfBoundsException;
11
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
12
use Psr\Http\Server\RequestHandlerInterface;
13
14
class Dispatcher extends GroupCountBasedDispatcher implements
15
    MiddlewareAwareInterface,
16
    RequestHandlerInterface,
17
    StrategyAwareInterface
18
{
19
    use MiddlewareAwareTrait;
20
    use StrategyAwareTrait;
21
22
    /**
23
     * Dispatch the current route
24
     *
25
     * @param \Psr\Http\Message\ServerRequestInterface $request
26
     *
27
     * @return \Psr\Http\Message\ResponseInterface
28
     */
29 30
    public function dispatchRequest(ServerRequestInterface $request) : ResponseInterface
30
    {
31 30
        $match = $this->dispatch($request->getMethod(), $request->getUri()->getPath());
32
33 30
        switch ($match[0]) {
34 30
            case FastRoute::NOT_FOUND:
35 12
                $this->setNotFoundDecoratorMiddleware();
36 12
                break;
37 18
            case FastRoute::METHOD_NOT_ALLOWED:
38 4
                $allowed = (array) $match[1];
39 4
                $this->setMethodNotAllowedDecoratorMiddleware($allowed);
40 4
                break;
41 14
            case FastRoute::FOUND:
42 14
                $match[1]->setVars($match[2]);
43 14
                $this->setFoundMiddleware($match[1]);
44 14
                break;
45
        }
46
47 30
        return $this->handle($request);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 30
    public function handle(ServerRequestInterface $request) : ResponseInterface
54
    {
55 30
        $middleware = $this->shiftMiddleware();
56
57 30
        if (is_null($middleware)) {
58
            throw new OutOfBoundsException('Reached end of middleware stack. Does your controller return a response?');
59
        }
60
61 30
        return $middleware->process($request, $this);
62
    }
63
64
    /**
65
     * Set up middleware for a found route
66
     *
67
     * @param \League\Route\Route $route
68
     *
69
     * @return void
70
     */
71 14
    protected function setFoundMiddleware(Route $route) : void
72
    {
73 14
        if (is_null($route->getStrategy())) {
74
            $route->setStrategy($this->getStrategy());
75
        }
76
77
        // wrap entire dispatch process in exception handler
78 14
        $this->prependMiddleware($route->getStrategy()->getExceptionHandler());
79
80
        // add group and route specific middleware
81 14
        if ($group = $route->getParentGroup()) {
82 4
            $this->middlewares($group->getMiddlewareStack());
83
        }
84
85 14
        $this->middlewares($route->getMiddlewareStack());
86
87
        // add actual route to end of stack
88 14
        $this->middleware($route);
89 14
    }
90
91
    /**
92
     * Set up middleware for a not found route
93
     *
94
     * @return void
95
     */
96 12
    protected function setNotFoundDecoratorMiddleware() : void
97
    {
98 12
        $middleware = $this->getStrategy()->getNotFoundDecorator(new NotFoundException);
99 12
        $this->prependMiddleware($middleware);
100 12
    }
101
102
    /**
103
     * Set up middleware for a not allowed route
104
     *
105
     * @param array $allowed
106
     *
107
     * @return void
108
     */
109 4
    protected function setMethodNotAllowedDecoratorMiddleware(array $allowed) : void
110
    {
111 4
        $middleware = $this->getStrategy()->getMethodNotAllowedDecorator(
112 4
            new MethodNotAllowedException($allowed)
113
        );
114
115 4
        $this->prependMiddleware($middleware);
116 4
    }
117
}
118