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

Passed
Pull Request — master (#695)
by Timur
07:14
created

Executor::postExecute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 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 123
    public function __construct(
45
        ExecutorInterface $executor,
46
        PromiseAdapter $promiseAdapter,
47
        EventDispatcherInterface $dispatcher,
48
        ?callable $defaultFieldResolver = null,
49
        bool $useExperimental = false
50
    ) {
51 123
        $this->executor = $executor;
52 123
        $this->promiseAdapter = $promiseAdapter;
53 123
        $this->dispatcher = $dispatcher;
54 123
        $this->defaultFieldResolver = $defaultFieldResolver;
55 123
        $this->useExperimentalExecutor = $useExperimental;
56 123
    }
57
58
    public function setExecutor(ExecutorInterface $executor): self
59
    {
60
        $this->executor = $executor;
61
62
        return $this;
63
    }
64
65 115
    public function addSchemaBuilder(string $name, Closure $builder): self
66
    {
67 115
        $this->schemas[$name] = $builder;
68
69 115
        return $this;
70
    }
71
72 106
    public function addSchema(string $name, Schema $schema): self
73
    {
74 106
        $this->schemas[$name] = $schema;
75
76 106
        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
            // TODO(mcg-web): Replace by array_key_first PHP 7 >= 7.3.0.
87 102
            foreach ($this->schemas as $name => $schema) {
88 102
                break;
89
            }
90
        }
91 108
        if (!isset($this->schemas[$name])) {
92 1
            throw new NotFoundHttpException(sprintf('Could not found "%s" schema.', $name));
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 122
    public function setMaxQueryDepth(int $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(int $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(),
151 103
            $executorArgumentsEvent->getVariableValue(),
152 103
            $executorArgumentsEvent->getOperationName(),
153 103
            $this->defaultFieldResolver
154
        );
155
156 103
        $result = $this->postExecute($result);
157
158 101
        return $result;
159
    }
160
161
    /**
162
     * @param mixed $rootValue
163
     */
164 103
    private function preExecute(
165
        Schema $schema,
166
        ?string $requestString,
167
        ArrayObject $contextValue,
168
        $rootValue = null,
169
        ?array $variableValue = null,
170
        ?string $operationName = null
171
    ): ExecutorArgumentsEvent {
172
        // @phpstan-ignore-next-line
173 103
        $this->dispatcher->dispatch(new ExecutorContextEvent($contextValue), Events::EXECUTOR_CONTEXT);
174
175
        /** @var ExecutorArgumentsEvent $object */
176
        // @phpstan-ignore-next-line
177 103
        $object = $this->dispatcher->dispatch(
178
            /** @var ExtensibleSchema $schema */
179 103
            ExecutorArgumentsEvent::create($schema, $requestString, $contextValue, $rootValue, $variableValue, $operationName),
0 ignored issues
show
Bug introduced by
It seems like $requestString can also be of type null; however, parameter $requestString of Overblog\GraphQLBundle\E...rgumentsEvent::create() 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

179
            ExecutorArgumentsEvent::create($schema, /** @scrutinizer ignore-type */ $requestString, $contextValue, $rootValue, $variableValue, $operationName),
Loading history...
180 103
            Events::PRE_EXECUTOR
181
        );
182
183 103
        return $object;
184
    }
185
186 103
    private function postExecute(ExecutionResult $result): ExecutionResult
187
    {
188
        // @phpstan-ignore-next-line
189 103
        return $this->dispatcher->dispatch(
190 103
            new ExecutorResultEvent($result),
191 103
            Events::POST_EXECUTOR
192 101
        )->getResult();
193
    }
194
}
195