Completed
Push — master ( f5bdea...18af5f )
by Phil
14s
created

JsonStrategy::getNotFoundDecorator()   A

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 6
Code Lines 3

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace League\Route\Strategy;
4
5
use \Exception;
6
use League\Route\Http\Exception\MethodNotAllowedException;
7
use League\Route\Http\Exception\NotFoundException;
8
use League\Route\Http\Exception as HttpException;
9
use League\Route\Middleware\ExecutionChain;
10
use League\Route\Route;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Http\Message\ServerRequestInterface;
13
use RuntimeException;
14
15
class JsonStrategy implements StrategyInterface
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20 12
    public function getExecutionChain(Route $route, array $vars)
21
    {
22
        $middleware = function (
23
            ServerRequestInterface $request, ResponseInterface $response, callable $next
24
        ) use (
25 12
            $route, $vars
26
        ) {
27 12
            $return = call_user_func_array($route->getCallable(), [$request, $response, $vars]);
28
29 6
            if (! $return instanceof ResponseInterface) {
30 3
                throw new RuntimeException(
31
                    'Route callables must return an instance of (Psr\Http\Message\ResponseInterface)'
32 3
                );
33
            }
34
35 3
            $response = $return;
36 3
            $response = $next($request, $response);
37
38 3
            return $response->withAddedHeader('content-type', 'application/json');
39 12
        };
40
41 12
        $execChain = (new ExecutionChain)->middleware($middleware);
42
43
        // ensure middleware is executed in the order it was added
44 12
        $stack = array_reverse($route->getMiddlewareStack());
45
46 12
        foreach ($stack as $middleware) {
47 3
            $execChain->middleware($middleware);
48 12
        }
49
50 12
        return $execChain;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 3
    public function getNotFoundDecorator(NotFoundException $exception)
57
    {
58
        return function (ServerRequestInterface $request, ResponseInterface $response) use ($exception) {
59 3
            return $exception->buildJsonResponse($response);
60 3
        };
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 3
    public function getMethodNotAllowedDecorator(MethodNotAllowedException $exception)
67
    {
68
        return function (ServerRequestInterface $request, ResponseInterface $response) use ($exception) {
69 3
            return $exception->buildJsonResponse($response);
70 3
        };
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function getExceptionDecorator(Exception $exception)
77
    {
78 6
        return function (ServerRequestInterface $request, ResponseInterface $response) use ($exception) {
79 6
            if ($exception instanceof HttpException) {
80 3
                return $exception->buildJsonResponse($response);
81
            }
82
83 3
            $response->getBody()->write(json_encode([
84 3
                'status_code'     => 500,
85 3
                'reason_phrase' => $exception->getMessage()
86 3
            ]));
87
88 3
            $response = $response->withAddedHeader('content-type', 'application/json');
89 3
            return $response->withStatus(500, $exception->getMessage());
90 6
        };
91
    }
92
}
93