Completed
Push — master ( cc72d2...c9b5f2 )
by Phil
20:18
created

JsonStrategy   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

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
1
<?php
2
3
namespace League\Route\Strategy;
4
5
<<<<<<< Updated upstream
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_SL
Loading history...
6
use \Exception;
7
use League\Route\Http\Exception\MethodNotAllowedException;
8
use League\Route\Http\Exception\NotFoundException;
9
=======
10
use ArrayObject;
11
use Exception;
12
>>>>>>> Stashed changes
13
use League\Route\Http\Exception as HttpException;
14
use League\Route\Route;
15
use Psr\Http\Message\ResponseInterface;
16
use Psr\Http\Message\ServerRequestInterface;
17
use RuntimeException;
18
19 9
class JsonStrategy implements StrategyInterface
20
{
21
    /**
22 9
     * {@inheritdoc}
23
     */
24 3
    public function getCallable(Route $route, array $vars)
25 3
    {
26
        return function (ServerRequestInterface $request, ResponseInterface $response, callable $next) use ($route, $vars) {
27 3
            $return = call_user_func_array($route->getCallable(), [$request, $response, $vars]);
28
29
            if (! $return instanceof ResponseInterface) {
30
                throw new RuntimeException(
31
                    'Route callables must return an instance of (Psr\Http\Message\ResponseInterface)'
32
                );
33
            }
34 9
35
            $response = $return;
36
            $response = $next($request, $response);
37
38
            return $response->withAddedHeader('content-type', 'application/json');
39
        };
40 3
    }
41
42
    /**
43 3
     * {@inheritdoc}
44 3
     */
45
    public function getNotFoundDecorator(NotFoundException $exception)
46
    {
47
        return function (ServerRequestInterface $request, ResponseInterface $response) use ($exception) {
48
            return $exception->buildJsonResponse($response);
49
        };
50 3
    }
51
52
    /**
53 3
     * {@inheritdoc}
54 3
     */
55
    public function getMethodNotAllowedDecorator(MethodNotAllowedException $exception)
56
    {
57
        return function (ServerRequestInterface $request, ResponseInterface $response) use ($exception) {
58
            return $exception->buildJsonResponse($response);
59
        };
60
    }
61
62 6
    /**
63 6
     * {@inheritdoc}
64 3
     */
65
    public function getExceptionDecorator(Exception $exception)
66
    {
67 3
        return function (ServerRequestInterface $request, ResponseInterface $response) use ($exception) {
68 3
            if ($exception instanceof HttpException) {
69 3
                return $exception->buildJsonResponse($response);
70 3
            }
71
<<<<<<< Updated upstream
72 3
=======
73 3
        } catch (HttpException $e) {
74 6
            return $e->buildJsonResponse($this->getResponse());
75
        } catch (Exception $e) {
76
            $response = $this->getResponse();
77
78
            $response->getBody()->write(json_encode([
79
                'status_code'   => 500,
80
                'reason_phrase' => $e->getMessage()
81
            ]));
82
83
            return $response->withAddedHeader('content-type', 'application/json')->withStatusCode(500);
84
        }
85
>>>>>>> Stashed changes
86
87
            $response->getBody()->write(json_encode([
88
                'status_code'   => 500,
89
                'reason_phrase' => $exception->getMessage()
90
            ]));
91
92
            $response = $response->withAddedHeader('content-type', 'application/json');
93
            return $response->withStatus(500, $exception->getMessage());
94
        };
95
    }
96
}
97