Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
ApiErrorRenderer
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
1 / 1
 createApiResponse
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace Apie\Common\ErrorHandler;
4
5use Apie\Common\ContextBuilders\Exceptions\WrongTokenException;
6use Apie\Common\Events\AddAuthenticationCookie;
7use Apie\Core\Exceptions\HttpStatusCodeException;
8use Symfony\Component\HttpFoundation\JsonResponse;
9use Symfony\Component\HttpFoundation\Response;
10use Throwable;
11
12class ApiErrorRenderer
13{
14    /**
15     * @TODO move to PSR request + response?
16     */
17    public function createApiResponse(Throwable $error): Response
18    {
19        $statusCode = $error instanceof HttpStatusCodeException ? $error->getStatusCode() : 500;
20        $response = new JsonResponse(
21            [
22                'message' => $error->getMessage(),
23                'code' => $error->getCode(),
24            ],
25            $statusCode
26        );
27        if ($error instanceof WrongTokenException) {
28            $response->headers->clearCookie(AddAuthenticationCookie::COOKIE_NAME);
29        }
30        return $response;
31    }
32}