Completed
Push — master ( 216b02...3062b3 )
by Phil
116:53 queued 108:43
created

JsonStrategy::getExceptionDecorator()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.7333
c 0
b 0
f 0
cc 2
nc 1
nop 1
crap 2
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\Route;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use RuntimeException;
13
14
class JsonStrategy implements StrategyInterface
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19 9
    public function getCallable(Route $route, array $vars)
20
    {
21
        return function (ServerRequestInterface $request, ResponseInterface $response, callable $next) use ($route, $vars) {
22 9
            $return = call_user_func_array($route->getCallable(), [$request, $response, $vars]);
23
24 3
            if (! $return instanceof ResponseInterface) {
25 3
                throw new RuntimeException(
26 2
                    'Route callables must return an instance of (Psr\Http\Message\ResponseInterface)'
27 1
                );
28
            }
29
30
            $response = $return;
31
            $response = $next($request, $response);
32
33
            if (! $response->hasHeader('content-type')) {
34
                return $response->withHeader('content-type', 'application/json');
35
            }
36
37
            return $response;
38 9
        };
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 3
    public function getNotFoundDecorator(NotFoundException $exception)
45
    {
46
        return function (ServerRequestInterface $request, ResponseInterface $response) use ($exception) {
47 3
            return $exception->buildJsonResponse($response);
48 3
        };
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 3
    public function getMethodNotAllowedDecorator(MethodNotAllowedException $exception)
55
    {
56
        return function (ServerRequestInterface $request, ResponseInterface $response) use ($exception) {
57 3
            return $exception->buildJsonResponse($response);
58 3
        };
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function getExceptionDecorator(Exception $exception)
65
    {
66 9
        return function (ServerRequestInterface $request, ResponseInterface $response) use ($exception) {
67 9
            if ($exception instanceof HttpException) {
68 3
                return $exception->buildJsonResponse($response);
69
            }
70
71 6
            $response->getBody()->write(json_encode([
72 6
                'status_code'   => 500,
73 6
                'reason_phrase' => $exception->getMessage()
74 2
            ]));
75
76 6
            $response = $response->withAddedHeader('content-type', 'application/json');
77 6
            return $response->withStatus(500, strtok($exception->getMessage(), "\n"));
78 9
        };
79
    }
80
}
81