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
26:04 queued 01:02
created

Executor::preExecute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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

165
            /** @scrutinizer ignore-type */ $executorArgumentsEvent->getContextValue(),
Loading history...
166 103
            $executorArgumentsEvent->getVariableValue(),
167
            $executorArgumentsEvent->getOperationName(),
168 102
            $this->defaultFieldResolver
169
        );
170
171 103
        $result = $this->postExecute($result, $executorArgumentsEvent);
172
173
        return $result;
174
    }
175
176
    private function preExecute(
177
        Schema $schema,
178
        ?string $requestString,
179 103
        \ArrayObject $contextValue,
180 103
        $rootValue = null,
181 103
        ?array $variableValue = null,
182
        ?string $operationName = null
183
    ): ExecutorArgumentsEvent {
184 103
        $this->dispatcher->dispatch(
185 103
            new ExecutorContextEvent($contextValue),
186 103
            Events::EXECUTOR_CONTEXT
187
        );
188
189
        return $this->dispatcher->dispatch(
190 103
            ExecutorArgumentsEvent::create($schema, $requestString, $contextValue, $rootValue, $variableValue, $operationName),
191
            Events::PRE_EXECUTOR
192 103
        );
193 103
    }
194 103
195 102
    private function postExecute(ExecutionResult $result, ExecutorArgumentsEvent $executorArguments): ExecutionResult
196
    {
197
        return $this->dispatcher->dispatch(
198
            new ExecutorResultEvent($result, $executorArguments),
199
            Events::POST_EXECUTOR
200
        )->getResult();
201
    }
202
}
203