HttpExceptionConverter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 15
c 1
b 0
f 0
dl 0
loc 25
ccs 13
cts 13
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A convert() 0 20 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