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
10:44
created

GraphQLCollector::getSchema()   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
    public function reset(): void
87
    {
88
        $this->data = [];
89
    }
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
     * @param ExecutorResultEvent $event
103
     */
104 1
    public function onPostExecutor(ExecutorResultEvent $event): void
105
    {
106 1
        $executorArgument = $event->getExecutorArguments();
107 1
        $queryString = $executorArgument->getRequestString();
108 1
        $variables = $executorArgument->getVariableValue();
109
110 1
        $result = $event->getResult()->toArray();
111
112
        $batch = [
113 1
            'queryString' => $queryString,
114 1
            'variables' => $this->cloneVar($variables),
115 1
            'result' => $this->cloneVar($result),
116 1
            'count' => 0,
117
        ];
118
119
        try {
120 1
            $parsed = Parser::parse($queryString);
121 1
            $batch['graphql'] = $this->extractGraphql($parsed);
122 1
            if (isset($batch['graphql']['fields'])) {
123 1
                $batch['count'] += \count($batch['graphql']['fields']);
124
            }
125 1
            $error = $result['errors'][0] ?? false;
126 1
            if ($error) {
127
                $batch['error'] = [
128 1
                    'message' => $error['message'],
129
                    'location' => $error['locations'][0] ?? false,
130
                ];
131
            }
132 1
        } catch (SyntaxError $error) {
133 1
            $location = $error->getLocations()[0] ?? false;
134 1
            $batch['error'] = ['message' => $error->getMessage(), 'location' => $location];
135
        }
136
137 1
        $this->batches[] = $batch;
138 1
    }
139
140
    /**
141
     * Extract GraphQL Information from the documentNode².
142
     *
143
     * @param DocumentNode $document
144
     *
145
     * @return array
146
     */
147 1
    protected function extractGraphql(DocumentNode $document)
148
    {
149 1
        $operation = null;
150 1
        $fields = [];
151
152 1
        foreach ($document->definitions as $definition) {
153 1
            if ($definition instanceof OperationDefinitionNode) {
154 1
                $operation = $definition->operation;
155 1
                foreach ($definition->selectionSet->selections as $selection) {
156 1
                    if ($selection instanceof FieldNode) {
157 1
                        $name = $selection->name->value;
158 1
                        $alias = $selection->alias ? $selection->alias->value : null;
159
160 1
                        $fields[] = [
161 1
                            'name' => $name,
162 1
                            'alias' => $alias,
163
                        ];
164
                    }
165
                }
166
            }
167
        }
168
169
        return [
170 1
            'operation' => $operation,
171 1
            'fields' => $fields,
172
        ];
173
    }
174
}
175