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
24:00
created

GraphQLCollector::onPostExecutor()   A

Complexity

Conditions 4
Paths 12

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 36
rs 9.52
c 1
b 0
f 0
cc 4
nc 12
nop 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
    public function collect(Request $request, Response $response, \Throwable $exception = null): void
25
    {
26
        $error = false;
27
        $count = 0;
28
        foreach ($this->batches as $batch) {
29
            if (isset($batch['error'])) {
30
                $error = true;
31
            }
32
            $count += $batch['count'];
33
        }
34
35
        $this->data = [
36
            'schema' => $request->attributes->get('_route_params')['schemaName'] ?? 'default',
37
            'batches' => $this->batches,
38
            'count' => $count,
39
            'error' => $error,
40
        ];
41
    }
42
43
    /**
44
     * Check if we have an error.
45
     *
46
     * @return bool
47
     */
48
    public function getError()
49
    {
50
        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
    public function getCount()
59
    {
60
        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
    public function getSchema()
69
    {
70
        return $this->data['schema'] ?? 'default';
71
    }
72
73
    /**
74
     * Return the list of executed batch.
75
     *
76
     * @return array
77
     */
78
    public function getBatches()
79
    {
80
        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
    public function reset(): void
87
    {
88
        $this->data = [];
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function getName()
95
    {
96
        return 'graphql';
97
    }
98
99
    /**
100
     * Hook into the GraphQL events to populate the collector.
101
     */
102
    public function onPostExecutor(ExecutorResultEvent $event): void
103
    {
104
        $executorArgument = $event->getExecutorArguments();
105
        $queryString = $executorArgument->getRequestString();
106
        $variables = $executorArgument->getVariableValue();
107
        $queryTime = microtime(true) - $executorArgument->getStartTime();
108
109
        $result = $event->getResult()->toArray();
110
111
        $batch = [
112
            'queryString' => $queryString,
113
            'queryTime' => $queryTime,
114
            'variables' => $this->cloneVar($variables),
115
            'result' => $this->cloneVar($result),
116
            'count' => 0,
117
        ];
118
119
        try {
120
            $parsed = Parser::parse($queryString);
121
            $batch['graphql'] = $this->extractGraphql($parsed);
122
            if (isset($batch['graphql']['fields'])) {
123
                $batch['count'] += \count($batch['graphql']['fields']);
124
            }
125
            $error = $result['errors'][0] ?? false;
126
            if ($error) {
127
                $batch['error'] = [
128
                    'message' => $error['message'],
129
                    'location' => $error['locations'][0] ?? false,
130
                ];
131
            }
132
        } catch (SyntaxError $error) {
133
            $location = $error->getLocations()[0] ?? false;
134
            $batch['error'] = ['message' => $error->getMessage(), 'location' => $location];
135
        }
136
137
        $this->batches[] = $batch;
138
    }
139
140
    /**
141
     * Extract GraphQL Information from the documentNode².
142
     *
143
     * @return array
144
     */
145
    protected function extractGraphql(DocumentNode $document)
146
    {
147
        $operation = null;
148
        $operationName = null;
149
        $fields = [];
150
151
        foreach ($document->definitions as $definition) {
152
            if ($definition instanceof OperationDefinitionNode) {
153
                $operationName = $definition->name ? $definition->name->value : null;
154
                $operation = $definition->operation;
155
                foreach ($definition->selectionSet->selections as $selection) {
156
                    if ($selection instanceof FieldNode) {
157
                        $name = $selection->name->value;
158
                        $alias = $selection->alias ? $selection->alias->value : null;
159
160
                        $fields[] = [
161
                            'name' => $name,
162
                            'alias' => $alias,
163
                        ];
164
                    }
165
                }
166
            }
167
        }
168
169
        return [
170
            'operation' => $operation,
171
            'operationName' => $operationName,
172
            'fields' => $fields,
173
        ];
174
    }
175
}
176