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 (#497)
by Jérémiah
20:39
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 103
    public function __construct(
41
        ExecutorInterface $executor,
42
        PromiseAdapter $promiseAdapter,
43
        EventDispatcherInterface $dispatcher,
44
        ?callable $defaultFieldResolver = null,
45
        bool $useExperimental = false
46
    ) {
47 103
        $this->executor = $executor;
48 103
        $this->promiseAdapter = $promiseAdapter;
49 103
        $this->dispatcher = $dispatcher;
50 103
        $this->defaultFieldResolver = $defaultFieldResolver;
51 103
        $this->useExperimentalExecutor = $useExperimental;
52 103
    }
53
54
    public function setExecutor(ExecutorInterface $executor): self
55
    {
56
        $this->executor = $executor;
57
58
        return $this;
59
    }
60
61
    /**
62
     * @param string $name
63
     * @param Schema $schema
64
     *
65
     * @return self
66
     */
67 94
    public function addSchema(string $name, Schema $schema): self
68
    {
69 94
        $this->schemas[$name] = $schema;
70
71 94
        return $this;
72
    }
73
74
    /**
75
     * @param string|null $name
76
     *
77
     * @return Schema
78
     */
79 88
    public function getSchema(?string $name = null): Schema
80
    {
81 88
        if (empty($this->schemas)) {
82 1
            throw new \RuntimeException('At least one schema should be declare.');
83
        }
84
85 87
        if (null === $name) {
86 81
            $schema = \array_values($this->schemas)[0];
87
        } else {
88 6
            if (!isset($this->schemas[$name])) {
89 1
                throw new NotFoundHttpException(\sprintf('Could not found "%s" schema.', $name));
90
            }
91 5
            $schema = $this->schemas[$name];
92
        }
93
94 86
        return $schema;
95
    }
96
97 102
    public function setMaxQueryDepth($maxQueryDepth): void
98
    {
99
        /** @var QueryDepth $queryDepth */
100 102
        $queryDepth = DocumentValidator::getRule('QueryDepth');
101 102
        $queryDepth->setMaxQueryDepth($maxQueryDepth);
102 102
    }
103
104 102
    public function setMaxQueryComplexity($maxQueryComplexity): void
105
    {
106
        /** @var QueryComplexity $queryComplexity */
107 102
        $queryComplexity = DocumentValidator::getRule('QueryComplexity');
108 102
        $queryComplexity->setMaxQueryComplexity($maxQueryComplexity);
109 102
    }
110
111 100
    public function enableIntrospectionQuery(): void
112
    {
113 100
        DocumentValidator::addRule(new DisableIntrospection(DisableIntrospection::DISABLED));
114 100
    }
115
116 1
    public function disableIntrospectionQuery(): void
117
    {
118 1
        DocumentValidator::addRule(new DisableIntrospection());
119 1
    }
120
121
    /**
122
     * @param string|null                    $schemaName
123
     * @param array                          $request
124
     * @param array|\ArrayObject|object|null $rootValue
125
     *
126
     * @return ExecutionResult
127
     */
128 84
    public function execute(?string $schemaName, array $request, $rootValue = null): ExecutionResult
129
    {
130 84
        $this->useExperimentalExecutor ? GraphQL::useExperimentalExecutor() : GraphQL::useReferenceExecutor();
131
132 84
        $executorArgumentsEvent = $this->preExecute(
133 84
            $this->getSchema($schemaName),
134 83
            $request[ParserInterface::PARAM_QUERY] ?? null,
135 83
            new \ArrayObject(),
136 83
            $rootValue,
137 83
            $request[ParserInterface::PARAM_VARIABLES],
138 83
            $request[ParserInterface::PARAM_OPERATION_NAME] ?? null
139
        );
140
141 83
        $executorArgumentsEvent->getSchema()->processExtensions();
142
143 83
        $result = $this->executor->execute(
144 83
            $this->promiseAdapter,
145 83
            $executorArgumentsEvent->getSchema(),
146 83
            $executorArgumentsEvent->getRequestString(),
147 83
            $executorArgumentsEvent->getRootValue(),
148 83
            $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

148
            /** @scrutinizer ignore-type */ $executorArgumentsEvent->getContextValue(),
Loading history...
149 83
            $executorArgumentsEvent->getVariableValue(),
150 83
            $executorArgumentsEvent->getOperationName(),
151 83
            $this->defaultFieldResolver
152
        );
153
154 83
        $result = $this->postExecute($result);
155
156 82
        return $result;
157
    }
158
159 83
    private function preExecute(
160
        Schema $schema,
161
        ?string $requestString,
162
        \ArrayObject $contextValue,
163
        $rootValue = null,
164
        ?array $variableValue = null,
165
        ?string $operationName = null
166
    ): ExecutorArgumentsEvent {
167 83
        EventDispatcherVersionHelper::dispatch(
168 83
            $this->dispatcher,
169 83
            new ExecutorContextEvent($contextValue),
170 83
            Events::EXECUTOR_CONTEXT
171
        );
172
173 83
        return EventDispatcherVersionHelper::dispatch(
174 83
            $this->dispatcher,
175 83
            ExecutorArgumentsEvent::create($schema, $requestString, $contextValue, $rootValue, $variableValue, $operationName),
176 83
            Events::PRE_EXECUTOR
177
        );
178
    }
179
180 83
    private function postExecute(ExecutionResult $result): ExecutionResult
181
    {
182 83
        return EventDispatcherVersionHelper::dispatch(
183 83
            $this->dispatcher,
184 83
            new ExecutorResultEvent($result),
185 83
            Events::POST_EXECUTOR
186 82
        )->getResult();
187
    }
188
}
189