ApiProblemExceptionHandler   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 5
dl 0
loc 30
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 25 3
1
<?php
2
3
namespace Scheduler\Web\Radar\Middleware;
4
5
use Exception;
6
use Crell\ApiProblem\ApiProblem;
7
use Psr\Http\Message\ServerRequestInterface as Request;
8
use Psr\Http\Message\ResponseInterface as Response;
9
10
class ApiProblemExceptionHandler extends \Relay\Middleware\ExceptionHandler
11
{
12
    const SERVER_ERROR_MESSAGE = "We're experiencing some trouble.";
13
14
    public function __invoke(Request $request, Response $response, callable $next)
15
    {
16
        try {
17
            $response = $next($request, $response);
18
        } catch (Exception $e) {
19
            $response = $this->exceptionResponse
20
                                ->withStatus(500)
21
                                ->withHeader('Content-Type', 'application/problem+json');
22
23
            $problem = new ApiProblem();
24
            $problem->setTitle($response->getReasonPhrase());
25
            $problem->setStatus(500);
26
            $problem->setInstance((string) $request->getUri());
27
28
            if (getenv("APP_ENV") == "dev") {
29
                $problem->setDetail($e->getMessage());
30
            } else {
31
                $problem->setDetail(self::SERVER_ERROR_MESSAGE);
32
            }
33
34
            $response->getBody()->write($problem->asJson());
35
        }
36
37
        return $response;
38
    }
39
}
40