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 — 0.12 (#548)
by Jérémiah
18:41 queued 15:51
created

Executor::addSchemaBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Overblog\GraphQLBundle\Request;
6
7
use GraphQL\Executor\ExecutionResult;
8
use GraphQL\Executor\Promise\PromiseAdapter;
9
use GraphQL\GraphQL;
10
use GraphQL\Type\Schema;
11
use GraphQL\Validator\DocumentValidator;
12
use GraphQL\Validator\Rules\DisableIntrospection;
13
use GraphQL\Validator\Rules\QueryComplexity;
14
use GraphQL\Validator\Rules\QueryDepth;
15
use Overblog\GraphQLBundle\Event\EventDispatcherVersionHelper;
16
use Overblog\GraphQLBundle\Event\Events;
17
use Overblog\GraphQLBundle\Event\ExecutorArgumentsEvent;
18
use Overblog\GraphQLBundle\Event\ExecutorContextEvent;
19
use Overblog\GraphQLBundle\Event\ExecutorResultEvent;
20
use Overblog\GraphQLBundle\Executor\ExecutorInterface;
21
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
22
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
23
24
class Executor
25
{
26
    public const PROMISE_ADAPTER_SERVICE_ID = 'overblog_graphql.promise_adapter';
27
28
    private $schemas = [];
29
30
    private $dispatcher;
31
32
    private $promiseAdapter;
33
34
    private $executor;
35
36
    private $defaultFieldResolver;
37
38
    private $useExperimentalExecutor;
39
40 104
    public function __construct(
41
        ExecutorInterface $executor,
42
        PromiseAdapter $promiseAdapter,
43
        EventDispatcherInterface $dispatcher,
44
        ?callable $defaultFieldResolver = null,
45
        bool $useExperimental = false
46
    ) {
47 104
        $this->executor = $executor;
48 104
        $this->promiseAdapter = $promiseAdapter;
49 104
        $this->dispatcher = $dispatcher;
50 104
        $this->defaultFieldResolver = $defaultFieldResolver;
51 104
        $this->useExperimentalExecutor = $useExperimental;
52 104
    }
53
54
    public function setExecutor(ExecutorInterface $executor): self
55
    {
56
        $this->executor = $executor;
57
58
        return $this;
59
    }
60
61 96
    public function addSchemaBuilder(string $name, callable $builder): self
62
    {
63 96
        $this->schemas[$name] = $builder;
64
65 96
        return $this;
66
    }
67
68
    /**
69
     * @param string $name
70
     * @param Schema $schema
71
     *
72
     * @return self
73
     */
74
    public function addSchema(string $name, Schema $schema): self
75
    {
76
        $this->schemas[$name] = $schema;
77
78
        return $this;
79
    }
80
81
    /**
82
     * @param string|null $name
83
     *
84
     * @return Schema
85
     */
86 90
    public function getSchema(?string $name = null): Schema
87
    {
88 90
        if (empty($this->schemas)) {
89 1
            throw new \RuntimeException('At least one schema should be declare.');
90
        }
91
92 89
        if (null === $name) {
93 83
            $name = self::arrayFirstKey($this->schemas);
94
        }
95 89
        if (!isset($this->schemas[$name])) {
96 1
            throw new NotFoundHttpException(\sprintf('Could not found "%s" schema.', $name));
97
        }
98 88
        if (\is_callable($this->schemas[$name])) {
99 88
            $this->schemas[$name] = $this->schemas[$name]();
100
        }
101
102 87
        return $this->schemas[$name];
103
    }
104
105
    /**
106
     * TODO(mcg-web): Replace by array_key_first PHP 7 >= 7.3.0.
107
     *
108
     * @param iterable $iterable
109
     *
110
     * @return int|string|null
111
     */
112 83
    private static function arrayFirstKey(iterable $iterable)
113
    {
114 83
        foreach ($iterable as $key => $unused) {
115 83
            return $key;
116
        }
117
118
        return null;
119
    }
120
121 103
    public function setMaxQueryDepth($maxQueryDepth): void
122
    {
123
        /** @var QueryDepth $queryDepth */
124 103
        $queryDepth = DocumentValidator::getRule('QueryDepth');
125 103
        $queryDepth->setMaxQueryDepth($maxQueryDepth);
126 103
    }
127
128 103
    public function setMaxQueryComplexity($maxQueryComplexity): void
129
    {
130
        /** @var QueryComplexity $queryComplexity */
131 103
        $queryComplexity = DocumentValidator::getRule('QueryComplexity');
132 103
        $queryComplexity->setMaxQueryComplexity($maxQueryComplexity);
133 103
    }
134
135 102
    public function enableIntrospectionQuery(): void
136
    {
137 102
        DocumentValidator::addRule(new DisableIntrospection(DisableIntrospection::DISABLED));
138 102
    }
139
140 1
    public function disableIntrospectionQuery(): void
141
    {
142 1
        DocumentValidator::addRule(new DisableIntrospection());
143 1
    }
144
145
    /**
146
     * @param string|null                    $schemaName
147
     * @param array                          $request
148
     * @param array|\ArrayObject|object|null $rootValue
149
     *
150
     * @return ExecutionResult
151
     */
152 86
    public function execute(?string $schemaName, array $request, $rootValue = null): ExecutionResult
153
    {
154 86
        $this->useExperimentalExecutor ? GraphQL::useExperimentalExecutor() : GraphQL::useReferenceExecutor();
155
156 86
        $executorArgumentsEvent = $this->preExecute(
157 86
            $this->getSchema($schemaName),
158 84
            $request[ParserInterface::PARAM_QUERY] ?? null,
159 84
            new \ArrayObject(),
160 84
            $rootValue,
161 84
            $request[ParserInterface::PARAM_VARIABLES],
162 84
            $request[ParserInterface::PARAM_OPERATION_NAME] ?? null
163
        );
164
165 84
        $executorArgumentsEvent->getSchema()->processExtensions();
166
167 84
        $result = $this->executor->execute(
168 84
            $this->promiseAdapter,
169 84
            $executorArgumentsEvent->getSchema(),
170 84
            $executorArgumentsEvent->getRequestString(),
171 84
            $executorArgumentsEvent->getRootValue(),
172 84
            $executorArgumentsEvent->getContextValue(),
0 ignored issues
show
Bug introduced by
$executorArgumentsEvent->getContextValue() of type ArrayObject is incompatible with the type array|null expected by parameter $contextValue of Overblog\GraphQLBundle\E...torInterface::execute(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

172
            /** @scrutinizer ignore-type */ $executorArgumentsEvent->getContextValue(),
Loading history...
173 84
            $executorArgumentsEvent->getVariableValue(),
174 84
            $executorArgumentsEvent->getOperationName(),
175 84
            $this->defaultFieldResolver
176
        );
177
178 84
        $result = $this->postExecute($result);
179
180 83
        return $result;
181
    }
182
183 84
    private function preExecute(
184
        Schema $schema,
185
        ?string $requestString,
186
        \ArrayObject $contextValue,
187
        $rootValue = null,
188
        ?array $variableValue = null,
189
        ?string $operationName = null
190
    ): ExecutorArgumentsEvent {
191 84
        EventDispatcherVersionHelper::dispatch(
192 84
            $this->dispatcher,
193 84
            new ExecutorContextEvent($contextValue),
194 84
            Events::EXECUTOR_CONTEXT
195
        );
196
197 84
        return EventDispatcherVersionHelper::dispatch(
198 84
            $this->dispatcher,
199 84
            ExecutorArgumentsEvent::create($schema, $requestString, $contextValue, $rootValue, $variableValue, $operationName),
200 84
            Events::PRE_EXECUTOR
201
        );
202
    }
203
204 84
    private function postExecute(ExecutionResult $result): ExecutionResult
205
    {
206 84
        return EventDispatcherVersionHelper::dispatch(
207 84
            $this->dispatcher,
208 84
            new ExecutorResultEvent($result),
209 84
            Events::POST_EXECUTOR
210 83
        )->getResult();
211
    }
212
}
213