Completed
Pull Request — master (#115)
by Phil
02:51
created

Dispatcher::handleNotFound()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 14
ccs 0
cts 10
cp 0
rs 9.4285
cc 2
eloc 7
nc 1
nop 0
crap 6
1
<?php
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;
8
use League\Route\Http\Exception\NotFoundException;
9
use League\Route\Middleware\ExecutionChain;
10
use League\Route\Strategy\JsonStrategy;
11
use League\Route\Strategy\StrategyAwareInterface;
12
use League\Route\Strategy\StrategyAwareTrait;
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
16
class Dispatcher extends GroupCountBasedDispatcher implements StrategyAwareInterface
17
{
18
    use StrategyAwareTrait;
19
20
    /**
21
     * Match and dispatch a route matching the given http method and
22
     * uri, retruning an execution chain.
23
     *
24
     * @param \Psr\Http\Message\ServerRequestInterface $request
25
     *
26
     * @return \League\Route\Middleware\ExecutionChain
27
     */
28
    public function handle(ServerRequestInterface $request)
29
    {
30
        $match = $this->dispatch(
31
            $request->getMethod(),
32
            $request->getUri()->getPath()
33
        );
34
35
        if ($match[0] === FastRoute::NOT_FOUND) {
36
            return $this->handleNotFound();
37
        }
38
39
        if ($match[0] === FastRoute::METHOD_NOT_ALLOWED) {
40
            $allowed = (array) $match[1];
41
            return $this->handleNotAllowed($allowed);
42
        }
43
44
        return $this->handleFound($match[1], (array) $match[2]);
45
    }
46
47
    /**
48
     * Handle dispatching of a found route.
49
     *
50
     * @param callable $route
51
     * @param array    $vars
52
     *
53
     * @return \League\Route\Middleware\ExecutionChain
54
     */
55
    protected function handleFound(callable $route, array $vars)
56
    {
57
        return call_user_func_array($route, [$vars]);
58
    }
59
60
    /**
61
     * Handle a not found route.
62
     *
63
     * @return \League\Route\Middleware\ExecutionChain
64
     */
65
    protected function handleNotFound()
66
    {
67
        $middleware = function (ServerRequestInterface $request, ResponseInterface $response) {
68
            $exception = new NotFoundException;
69
70
            if ($this->getStrategy() instanceof JsonStrategy) {
71
                return $exception->buildJsonResponse($response);
72
            }
73
74
            throw $exception;
75
        };
76
77
        return (new ExecutionChain)->middleware($middleware);
78
    }
79
80
    /**
81
     * Handles a not allowed route.
82
     *
83
     * @param array $allowed
84
     *
85
     * @return \League\Route\Middleware\ExecutionChain
86
     */
87
    protected function handleNotAllowed(array $allowed)
88
    {
89
        $middleware = function (ServerRequestInterface $request, ResponseInterface $response) use ($allowed) {
90
            $exception = new MethodNotAllowedException($allowed);
91
92
            if ($this->getStrategy() instanceof JsonStrategy) {
93
                return $exception->buildJsonResponse($response);
94
            }
95
96
            throw $exception;
97
        };
98
99
        return (new ExecutionChain)->middleware($middleware);
100
    }
101
}
102