JsonStrategy   A
last analyzed

Coupling/Cohesion

Components 0
Dependencies 6

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 88%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getNotFoundDecorator() 0 6 1
A getMethodNotAllowedDecorator() 0 6 1
A getExceptionDecorator() 0 16 2
A getCallable() 0 17 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
                    'Route callables must return an instance of (Psr\Http\Message\ResponseInterface)'
27 3
                );
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 3
            ]));
71
72 3
            $response = $response->withAddedHeader('content-type', 'application/json');
73 3
            return $response->withStatus(500, $exception->getMessage());
74 6
        };
75
    }
76
}
77