HttpExceptionConverter::convert()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 20
ccs 13
cts 13
cp 1
rs 9.7998
cc 4
nc 4
nop 1
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace jin2chen\ApiBundle\Response\ExceptionConverter;
6
7
use InvalidArgumentException;
8
use jin2chen\ApiBundle\Response\ExceptionConverterInterface;
9
use Symfony\Component\HttpKernel\Exception\HttpException;
10
use Throwable;
11
12
class HttpExceptionConverter implements ExceptionConverterInterface
13
{
14
    /**
15
     * @inheritdoc
16
     */
17 6
    public function convert(Throwable $e): array
18
    {
19 6
        if (!$e instanceof HttpException) {
20 1
            throw new InvalidArgumentException(sprintf('%s is not a %s', get_class($e), HttpException::class));
21
        }
22
23 5
        $statusCode = $e->getStatusCode();
24 5
        $message = 'Internal Service Error.';
25 5
        if ($statusCode < 400) {
26 1
            $statusCode = 500;
27 5
        } elseif ($statusCode >= 600) {
28 1
            $statusCode = 500;
29
        } else {
30 4
            $message = $e->getMessage();
31
        }
32
33
        return [
34 5
            'status' => $statusCode,
35 5
            'code' => (int) $e->getCode(),
36 5
            'message' => $message,
37
        ];
38
    }
39
}
40