Completed
Push — master ( b9d832...421ea6 )
by Phil
06:19 queued 04:14
created

Dispatcher::handleFound()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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