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
Push — 0.12 ( 247c91...94418a )
by Jérémiah
24:10 queued 24:06
created

Executor::addSchema()   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 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
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 87
    public function addSchema(string $name, Schema $schema): self
75
    {
76 87
        $this->schemas[$name] = $schema;
77
78 87
        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
            // TODO(mcg-web): Replace by array_key_first PHP 7 >= 7.3.0.
94 83
            foreach ($this->schemas as $name => $schema) {
95 83
                break;
96
            }
97
        }
98 89
        if (!isset($this->schemas[$name])) {
99 1
            throw new NotFoundHttpException(\sprintf('Could not found "%s" schema.', $name));
100
        }
101 88
        $schema = $this->schemas[$name];
102 88
        if (\is_callable($schema)) {
103 88
            $schema = $schema();
104 87
            $this->addSchema($name, $schema);
105
        }
106
107 87
        return $schema;
108
    }
109
110 103
    public function setMaxQueryDepth($maxQueryDepth): void
111
    {
112
        /** @var QueryDepth $queryDepth */
113 103
        $queryDepth = DocumentValidator::getRule('QueryDepth');
114 103
        $queryDepth->setMaxQueryDepth($maxQueryDepth);
115 103
    }
116
117 103
    public function setMaxQueryComplexity($maxQueryComplexity): void
118
    {
119
        /** @var QueryComplexity $queryComplexity */
120 103
        $queryComplexity = DocumentValidator::getRule('QueryComplexity');
121 103
        $queryComplexity->setMaxQueryComplexity($maxQueryComplexity);
122 103
    }
123
124 102
    public function enableIntrospectionQuery(): void
125
    {
126 102
        DocumentValidator::addRule(new DisableIntrospection(DisableIntrospection::DISABLED));
127 102
    }
128
129 1
    public function disableIntrospectionQuery(): void
130
    {
131 1
        DocumentValidator::addRule(new DisableIntrospection());
132 1
    }
133
134
    /**
135
     * @param string|null                    $schemaName
136
     * @param array                          $request
137
     * @param array|\ArrayObject|object|null $rootValue
138
     *
139
     * @return ExecutionResult
140
     */
141 86
    public function execute(?string $schemaName, array $request, $rootValue = null): ExecutionResult
142
    {
143 86
        $this->useExperimentalExecutor ? GraphQL::useExperimentalExecutor() : GraphQL::useReferenceExecutor();
144
145 86
        $executorArgumentsEvent = $this->preExecute(
146 86
            $this->getSchema($schemaName),
147 84
            $request[ParserInterface::PARAM_QUERY] ?? null,
148 84
            new \ArrayObject(),
149 84
            $rootValue,
150 84
            $request[ParserInterface::PARAM_VARIABLES],
151 84
            $request[ParserInterface::PARAM_OPERATION_NAME] ?? null
152
        );
153
154 84
        $executorArgumentsEvent->getSchema()->processExtensions();
155
156 84
        $result = $this->executor->execute(
157 84
            $this->promiseAdapter,
158 84
            $executorArgumentsEvent->getSchema(),
159 84
            $executorArgumentsEvent->getRequestString(),
160 84
            $executorArgumentsEvent->getRootValue(),
161 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

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