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 ( 7d433d...cbed54 )
by Vincent
30s queued 13s
created

Executor   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Test Coverage

Coverage 96.1%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 19
eloc 68
dl 0
loc 174
ccs 74
cts 77
cp 0.961
rs 10
c 3
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A addSchema() 0 5 1
A __construct() 0 12 1
A addSchemaBuilder() 0 5 1
A setExecutor() 0 5 1
A postExecute() 0 7 1
A preExecute() 0 21 1
A disableIntrospectionQuery() 0 3 1
A setMaxQueryComplexity() 0 5 1
A execute() 0 34 2
A setMaxQueryDepth() 0 5 1
A getSchemasNames() 0 3 1
A enableIntrospectionQuery() 0 3 1
A getSchema() 0 21 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Overblog\GraphQLBundle\Request;
6
7
use ArrayObject;
8
use Closure;
9
use GraphQL\Executor\ExecutionResult;
10
use GraphQL\Executor\Promise\PromiseAdapter;
11
use GraphQL\GraphQL;
12
use GraphQL\Type\Schema;
13
use GraphQL\Validator\DocumentValidator;
14
use GraphQL\Validator\Rules\DisableIntrospection;
15
use GraphQL\Validator\Rules\QueryComplexity;
16
use GraphQL\Validator\Rules\QueryDepth;
17
use Overblog\GraphQLBundle\Event\Events;
18
use Overblog\GraphQLBundle\Event\ExecutorArgumentsEvent;
19
use Overblog\GraphQLBundle\Event\ExecutorContextEvent;
20
use Overblog\GraphQLBundle\Event\ExecutorResultEvent;
21
use Overblog\GraphQLBundle\Executor\ExecutorInterface;
22
use RuntimeException;
23
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
24
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
25
use function array_keys;
26
use function is_callable;
27
use function sprintf;
28
29
class Executor
30
{
31
    public const PROMISE_ADAPTER_SERVICE_ID = 'overblog_graphql.promise_adapter';
32
33
    private array $schemas = [];
34
    private EventDispatcherInterface $dispatcher;
35
    private PromiseAdapter $promiseAdapter;
36
    private ExecutorInterface $executor;
37
    private bool $useExperimentalExecutor;
38
39
    /**
40
     * @var callable|null
41
     */
42
    private $defaultFieldResolver;
43
44 124
    public function __construct(
45
        ExecutorInterface $executor,
46
        PromiseAdapter $promiseAdapter,
47
        EventDispatcherInterface $dispatcher,
48
        ?callable $defaultFieldResolver = null,
49
        bool $useExperimental = false
50
    ) {
51 124
        $this->executor = $executor;
52 124
        $this->promiseAdapter = $promiseAdapter;
53 124
        $this->dispatcher = $dispatcher;
54 124
        $this->defaultFieldResolver = $defaultFieldResolver;
55 124
        $this->useExperimentalExecutor = $useExperimental;
56 124
    }
57
58
    public function setExecutor(ExecutorInterface $executor): self
59
    {
60
        $this->executor = $executor;
61
62
        return $this;
63
    }
64
65 116
    public function addSchemaBuilder(string $name, Closure $builder): self
66
    {
67 116
        $this->schemas[$name] = $builder;
68
69 116
        return $this;
70
    }
71
72 107
    public function addSchema(string $name, Schema $schema): self
73
    {
74 107
        $this->schemas[$name] = $schema;
75
76 107
        return $this;
77
    }
78
79 109
    public function getSchema(string $name = null): Schema
80
    {
81 109
        if (empty($this->schemas)) {
82 1
            throw new RuntimeException('At least one schema should be declare.');
83
        }
84
85 108
        if (null === $name) {
86 102
            $name = isset($this->schemas['default']) ? 'default' : array_key_first($this->schemas);
87
        }
88
89 108
        if (!isset($this->schemas[$name])) {
90 1
            throw new NotFoundHttpException(sprintf('Could not found "%s" schema.', $name));
91
        }
92
93 107
        $schema = $this->schemas[$name];
94 107
        if (is_callable($schema)) {
95 107
            $schema = $schema();
96 106
            $this->addSchema((string) $name, $schema);
97
        }
98
99 106
        return $schema;
100
    }
101
102 1
    public function getSchemasNames(): array
103
    {
104 1
        return array_keys($this->schemas);
105
    }
106
107 122
    public function setMaxQueryDepth(int $maxQueryDepth): void
108
    {
109
        /** @var QueryDepth $queryDepth */
110 122
        $queryDepth = DocumentValidator::getRule('QueryDepth');
111 122
        $queryDepth->setMaxQueryDepth($maxQueryDepth);
112 122
    }
113
114 122
    public function setMaxQueryComplexity(int $maxQueryComplexity): void
115
    {
116
        /** @var QueryComplexity $queryComplexity */
117 122
        $queryComplexity = DocumentValidator::getRule('QueryComplexity');
118 122
        $queryComplexity->setMaxQueryComplexity($maxQueryComplexity);
119 122
    }
120
121 121
    public function enableIntrospectionQuery(): void
122
    {
123 121
        DocumentValidator::addRule(new DisableIntrospection(DisableIntrospection::DISABLED));
124 121
    }
125
126 1
    public function disableIntrospectionQuery(): void
127
    {
128 1
        DocumentValidator::addRule(new DisableIntrospection());
129 1
    }
130
131
    /**
132
     * @param array|ArrayObject|object|null $rootValue
133
     */
134 105
    public function execute(?string $schemaName, array $request, $rootValue = null): ExecutionResult
135
    {
136 105
        $this->useExperimentalExecutor ? GraphQL::useExperimentalExecutor() : GraphQL::useReferenceExecutor();
137
138 105
        $schema = $this->getSchema($schemaName);
139
        /** @var string $schemaName */
140 103
        $schemaName = array_search($schema, $this->schemas);
141
142 103
        $executorArgumentsEvent = $this->preExecute(
143 103
            $schemaName,
144
            $schema,
145 103
            $request[ParserInterface::PARAM_QUERY] ?? null,
0 ignored issues
show
Bug introduced by
It seems like $request[Overblog\GraphQ...e::PARAM_QUERY] ?? null can also be of type null; however, parameter $requestString of Overblog\GraphQLBundle\R...\Executor::preExecute() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

145
            /** @scrutinizer ignore-type */ $request[ParserInterface::PARAM_QUERY] ?? null,
Loading history...
146 103
            new ArrayObject(),
147
            $rootValue,
148 103
            $request[ParserInterface::PARAM_VARIABLES],
149 103
            $request[ParserInterface::PARAM_OPERATION_NAME] ?? null
150
        );
151
152 103
        $executorArgumentsEvent->getSchema()->processExtensions();
153
154 103
        $result = $this->executor->execute(
155 103
            $this->promiseAdapter,
156 103
            $executorArgumentsEvent->getSchema(),
157 103
            $executorArgumentsEvent->getRequestString(),
158 103
            $executorArgumentsEvent->getRootValue(),
159 103
            $executorArgumentsEvent->getContextValue(),
160 103
            $executorArgumentsEvent->getVariableValue(),
161 103
            $executorArgumentsEvent->getOperationName(),
162 103
            $this->defaultFieldResolver
163
        );
164
165 103
        $result = $this->postExecute($result, $executorArgumentsEvent);
166
167 101
        return $result;
168
    }
169
170
    /**
171
     * @param mixed $rootValue
172
     */
173 103
    private function preExecute(
174
        string $schemaName,
175
        Schema $schema,
176
        string $requestString,
177
        ArrayObject $contextValue,
178
        $rootValue = null,
179
        ?array $variableValue = null,
180
        ?string $operationName = null
181
    ): ExecutorArgumentsEvent {
182
        // @phpstan-ignore-next-line (only for Symfony 4.4)
183 103
        $this->dispatcher->dispatch(new ExecutorContextEvent($contextValue), Events::EXECUTOR_CONTEXT);
184
185
        /** @var ExecutorArgumentsEvent $object */
186
        // @phpstan-ignore-next-line (only for Symfony 4.4)
187 103
        $object = $this->dispatcher->dispatch(
188
            // @phpstan-ignore-next-line
189 103
            ExecutorArgumentsEvent::create($schemaName, $schema, $requestString, $contextValue, $rootValue, $variableValue, $operationName),
190 103
            Events::PRE_EXECUTOR
191
        );
192
193 103
        return $object;
194
    }
195
196 103
    private function postExecute(ExecutionResult $result, ExecutorArgumentsEvent $executorArguments): ExecutionResult
197
    {
198
        // @phpstan-ignore-next-line (only for Symfony 4.4)
199 103
        return $this->dispatcher->dispatch(
200 103
            new ExecutorResultEvent($result, $executorArguments),
201 103
            Events::POST_EXECUTOR
202 101
        )->getResult();
203
    }
204
}
205