Completed
Push — master ( 0d2e3e...e223e2 )
by Phil
25:52
created

JsonStrategy   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 6
dl 0
loc 63
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCallable() 0 17 2
A getNotFoundDecorator() 0 6 1
A getMethodNotAllowedDecorator() 0 6 1
A getExceptionDecorator() 0 16 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
    public function getCallable(Route $route, array $vars)
20 12
    {
21
        return function (ServerRequestInterface $request, ResponseInterface $response, callable $next) use ($route, $vars) {
22
            $return = call_user_func_array($route->getCallable(), [$request, $response, $vars]);
23
24
            if (! $return instanceof ResponseInterface) {
25 12
                throw new RuntimeException(
26
                    'Route callables must return an instance of (Psr\Http\Message\ResponseInterface)'
27 12
                );
28
            }
29 6
30 3
            $response = $return;
31
            $response = $next($request, $response);
32 3
33
            return $response->withAddedHeader('content-type', 'application/json');
34
        };
35 3
    }
36 3
37
    /**
38 3
     * {@inheritdoc}
39 12
     */
40
    public function getNotFoundDecorator(NotFoundException $exception)
41 12
    {
42
        return function (ServerRequestInterface $request, ResponseInterface $response) use ($exception) {
43
            return $exception->buildJsonResponse($response);
44 12
        };
45
    }
46 12
47 3
    /**
48 12
     * {@inheritdoc}
49
     */
50 12
    public function getMethodNotAllowedDecorator(MethodNotAllowedException $exception)
51
    {
52
        return function (ServerRequestInterface $request, ResponseInterface $response) use ($exception) {
53
            return $exception->buildJsonResponse($response);
54
        };
55
    }
56 3
57
    /**
58
     * {@inheritdoc}
59 3
     */
60 3
    public function getExceptionDecorator(Exception $exception)
61
    {
62
        return function (ServerRequestInterface $request, ResponseInterface $response) use ($exception) {
63
            if ($exception instanceof HttpException) {
64
                return $exception->buildJsonResponse($response);
65
            }
66 3
67
            $response->getBody()->write(json_encode([
68
                'status_code'   => 500,
69 3
                'reason_phrase' => $exception->getMessage()
70 3
            ]));
71
72
            $response = $response->withAddedHeader('content-type', 'application/json');
73
            return $response->withStatus(500, $exception->getMessage());
74
        };
75
    }
76
}
77