Passed
Push — master ( 9da6fd...0354d3 )
by Rafael
05:30
created

DefaultErrorFormatter::format()   C

Complexity

Conditions 10
Paths 11

Size

Total Lines 48
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 10.1228

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 25
cts 28
cp 0.8929
rs 5.3454
c 0
b 0
f 0
cc 10
eloc 27
nc 11
nop 2
crap 10.1228

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Error;
12
13
use GraphQL\Error\ClientAware;
14
use GraphQL\Error\Error;
15
use GraphQL\Error\FormattedError;
16
use GraphQL\Language\AST\FieldNode;
17
use Symfony\Component\HttpFoundation\Response;
18
use Ynlo\GraphQLBundle\Exception\Controlled\ValidationError;
19
use Ynlo\GraphQLBundle\Exception\ControlledErrorInterface;
20
use Ynlo\GraphQLBundle\Util\Uuid;
21
22
class DefaultErrorFormatter implements ErrorFormatterInterface
23
{
24
    /**
25
     * @var ControlledErrorManager
26
     */
27
    protected $errorManager;
28
29
    /**
30
     * DefaultErrorFormatter constructor.
31
     *
32
     * @param ControlledErrorManager $errorManager
33
     */
34 22
    public function __construct(ControlledErrorManager $errorManager)
35
    {
36 22
        $this->errorManager = $errorManager;
37 22
    }
38
39
    /**
40
     * @inheritDoc
41
     */
42 2
    public function format(Error $error, $debug = false): array
43
    {
44 2
        $formattedError = FormattedError::createFromException($error, $debug);
45
46 2
        if (!isset($formattedError['tracking_id'])) {
47 2
            $originError = $error->getTrace()[0]['args'][0] ?? null;
48
49 2
            $trackingId = null;
50 2
            $errorCode = Response::HTTP_INTERNAL_SERVER_ERROR;
51 2
            if ($originError instanceof \Exception) {
52 2
                if ($originError instanceof ControlledErrorInterface) {
53 1
                    $errorCode = $originError->getCode();
54 1
                    if (!$originError->getMessage() && $this->errorManager->has($errorCode)) {
55 1
                        $formattedError['message'] = $this->errorManager->get($errorCode)->getMessage();
56
                    }
57
58 1
                } elseif ($originError instanceof ClientAware && $originError->isClientSafe()) {
59 1
                    $errorCode = null;
60
                }
61
62 2
                if ($originError instanceof ValidationError) {
63 1
                    $formattedError['constraintViolations'] = $originError->getViolationsArray();
64
                }
65
66 2
                $trackingId = Uuid::createFromData(
67
                    [
68 2
                        'message' => $originError->getMessage(),
69 2
                        'code' => $originError->getCode(),
70 2
                        'file' => $originError->getFile(),
71 2
                        'line' => $originError->getLine(),
72
                    ]
73
                );
74
75
            } elseif ($originError instanceof FieldNode) {
76
                $errorCode = Response::HTTP_BAD_REQUEST;
77
                $trackingId = Uuid::createFromData($formattedError['message'] ?? null);
78
            }
79
80 2
            $formattedError = array_merge(
81
                [
82 2
                    'code' => $errorCode,
83 2
                    'tracking_id' => $trackingId,
84
                ],
85 2
                $formattedError
86
            );
87
        }
88
89 2
        return $formattedError;
90
    }
91
}
92