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 (#713)
by Vincent
07:00
created

Executor   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Test Coverage

Coverage 96.1%

Importance

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

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

143
            /** @scrutinizer ignore-type */ $schemaName,
Loading history...
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
            /** @var ExtensibleSchema $schema */
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