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 — master ( cc7f41...e963e5 )
by Vincent
30s queued 13s
created

Executor::setExecutor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

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