Passed
Push — master ( ce7fff...9920f1 )
by Alan
04:04
created

HttpExceptionNormalizer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 23
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsNormalization() 0 3 2
A normalize() 0 10 2
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\GraphQl\Serializer\Exception;
15
16
use GraphQL\Error\Error;
17
use GraphQL\Error\FormattedError;
18
use Symfony\Component\HttpKernel\Exception\HttpException;
19
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20
21
/**
22
 * Normalize HTTP exceptions.
23
 *
24
 * @experimental
25
 *
26
 * @author Alan Poulain <[email protected]>
27
 */
28
final class HttpExceptionNormalizer implements NormalizerInterface
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function normalize($object, $format = null, array $context = []): array
34
    {
35
        /** @var HttpException */
36
        $httpException = $object->getPrevious();
37
        $error = FormattedError::createFromException($object);
38
        $error['message'] = $httpException->getMessage();
39
        $error['extensions']['status'] = $statusCode = $httpException->getStatusCode();
40
        $error['extensions']['category'] = $statusCode < 500 ? 'user' : Error::CATEGORY_INTERNAL;
41
42
        return $error;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function supportsNormalization($data, $format = null): bool
49
    {
50
        return $data instanceof Error && $data->getPrevious() instanceof HttpException;
51
    }
52
}
53