Completed
Push — master ( e223e2...f24f84 )
by Phil
21:43 queued 19:48
created

JsonStrategy::getCallable()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.108

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
ccs 7
cts 10
cp 0.7
rs 9.4285
cc 2
eloc 9
nc 1
nop 2
crap 2.108
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 1
                    'Route callables must return an instance of (Psr\Http\Message\ResponseInterface)'
27 2
                );
28
            }
29
30
            $response = $return;
31
            $response = $next($request, $response);
32
33
            return $response->withAddedHeader('content-type', 'application/json');
34 9
        };
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 3
    public function getNotFoundDecorator(NotFoundException $exception)
41
    {
42
        return function (ServerRequestInterface $request, ResponseInterface $response) use ($exception) {
43 3
            return $exception->buildJsonResponse($response);
44 3
        };
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 3
    public function getMethodNotAllowedDecorator(MethodNotAllowedException $exception)
51
    {
52
        return function (ServerRequestInterface $request, ResponseInterface $response) use ($exception) {
53 3
            return $exception->buildJsonResponse($response);
54 3
        };
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function getExceptionDecorator(Exception $exception)
61
    {
62 6
        return function (ServerRequestInterface $request, ResponseInterface $response) use ($exception) {
63 6
            if ($exception instanceof HttpException) {
64 3
                return $exception->buildJsonResponse($response);
65
            }
66
67 3
            $response->getBody()->write(json_encode([
68 3
                'status_code'   => 500,
69 3
                'reason_phrase' => $exception->getMessage()
70 2
            ]));
71
72 3
            $response = $response->withAddedHeader('content-type', 'application/json');
73 3
            return $response->withStatus(500, $exception->getMessage());
74 6
        };
75
    }
76
}
77