Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — master (#638)
by Vincent
12:27
created

GraphQLCollector::getError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Overblog\GraphQLBundle\DataCollector;
6
7
use GraphQL\Error\SyntaxError;
8
use GraphQL\Language\AST\DocumentNode;
9
use GraphQL\Language\AST\FieldNode;
10
use GraphQL\Language\AST\OperationDefinitionNode;
11
use GraphQL\Language\Parser;
12
use Overblog\GraphQLBundle\Event\ExecutorResultEvent;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
16
17
class GraphQLCollector extends DataCollector
18
{
19
    /**
20
     * GraphQL Batchs executed.
21
     */
22
    protected $batches = [];
23
24 1
    public function collect(Request $request, Response $response, \Throwable $exception = null): void
25
    {
26 1
        $error = false;
27 1
        $count = 0;
28 1
        foreach ($this->batches as $batch) {
29 1
            if (isset($batch['error'])) {
30 1
                $error = true;
31
            }
32 1
            $count += $batch['count'];
33
        }
34
35 1
        $this->data = [
36 1
            'schema' => $request->attributes->get('_route_params')['schemaName'] ?? 'default',
37 1
            'batches' => $this->batches,
38 1
            'count' => $count,
39 1
            'error' => $error,
40
        ];
41 1
    }
42
43
    /**
44
     * Check if we have an error.
45
     *
46
     * @return bool
47
     */
48 1
    public function getError()
49
    {
50 1
        return $this->data['error'] ?? false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->data['error'] ?? false also could return the type Symfony\Component\VarDumper\Cloner\Data which is incompatible with the documented return type boolean.
Loading history...
51
    }
52
53
    /**
54
     * Count the number of executed queries.
55
     *
56
     * @return int
57
     */
58 1
    public function getCount()
59
    {
60 1
        return $this->data['count'] ?? 0;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->data['count'] ?? 0 also could return the type Symfony\Component\VarDumper\Cloner\Data which is incompatible with the documented return type integer.
Loading history...
61
    }
62
63
    /**
64
     * Return the targeted schema.
65
     *
66
     * @return string
67
     */
68 1
    public function getSchema()
69
    {
70 1
        return $this->data['schema'] ?? 'default';
71
    }
72
73
    /**
74
     * Return the list of executed batch.
75
     *
76
     * @return array
77
     */
78 1
    public function getBatches()
79
    {
80 1
        return $this->data['batches'] ?? [];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->data['batches'] ?? array() also could return the type Symfony\Component\VarDumper\Cloner\Data which is incompatible with the documented return type array.
Loading history...
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 103
    public function reset(): void
87
    {
88 103
        $this->data = [];
89 103
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 1
    public function getName()
95
    {
96 1
        return 'graphql';
97
    }
98
99
    /**
100
     * Hook into the GraphQL events to populate the collector.
101
     */
102 104
    public function onPostExecutor(ExecutorResultEvent $event): void
103
    {
104 104
        $executorArgument = $event->getExecutorArguments();
105 104
        $queryString = $executorArgument->getRequestString();
106 104
        $operationName = $executorArgument->getOperationName();
107 104
        $variables = $executorArgument->getVariableValue();
108 104
        $queryTime = \microtime(true) - $executorArgument->getStartTime();
109
110 104
        $result = $event->getResult()->toArray();
111
112
        $batch = [
113 104
            'queryString' => $queryString,
114 104
            'queryTime' => $queryTime,
115 104
            'variables' => $this->cloneVar($variables),
116 104
            'result' => $this->cloneVar($result),
117 104
            'count' => 0,
118
        ];
119
120
        try {
121 104
            $parsed = Parser::parse($queryString);
122 104
            $batch['graphql'] = $this->extractGraphql($parsed, $operationName);
123 104
            if (isset($batch['graphql']['fields'])) {
124 104
                $batch['count'] += \count($batch['graphql']['fields']);
125
            }
126 104
            $error = $result['errors'][0] ?? false;
127 104
            if ($error) {
128 29
                $batch['error'] = [
129 104
                    'message' => $error['message'],
130 29
                    'location' => $error['locations'][0] ?? false,
131
                ];
132
            }
133 1
        } catch (SyntaxError $error) {
134 1
            $location = $error->getLocations()[0] ?? false;
135 1
            $batch['error'] = ['message' => $error->getMessage(), 'location' => $location];
136
        }
137
138 104
        $this->batches[] = $batch;
139 104
    }
140
141
    /**
142
     * Extract GraphQL Information from the documentNode².
143
     *
144
     * @return array
145
     */
146 104
    protected function extractGraphql(DocumentNode $document, ?string $operationName)
147
    {
148 104
        $operation = null;
149 104
        $fields = [];
150
151 104
        foreach ($document->definitions as $definition) {
152 104
            if ($definition instanceof OperationDefinitionNode) {
153 104
                $definitionOperation = $definition->name ? $definition->name->value : null;
154 104
                if ($operationName != $definitionOperation) {
155 29
                    continue;
156
                }
157
158 78
                $operation = $definition->operation;
159 78
                foreach ($definition->selectionSet->selections as $selection) {
160 78
                    if ($selection instanceof FieldNode) {
161 78
                        $name = $selection->name->value;
162 78
                        $alias = $selection->alias ? $selection->alias->value : null;
163
164 78
                        $fields[] = [
165 78
                            'name' => $name,
166 78
                            'alias' => $alias,
167
                        ];
168
                    }
169
                }
170
            }
171
        }
172
173
        return [
174 104
            'operation' => $operation,
175 104
            'operationName' => $operationName,
176 104
            'fields' => $fields,
177
        ];
178
    }
179
}
180