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

JsonStrategy   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 82.14%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 6
dl 0
loc 67
ccs 23
cts 28
cp 0.8214
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCallable() 0 21 3
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 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