Completed
Push — master ( 097498...99dcbb )
by Tomáš
04:51
created

RequestProcessor::isDebug()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Portiny\GraphQL\GraphQL;
6
7
use GraphQL\Error\Debug;
8
use GraphQL\Error\FormattedError;
9
use GraphQL\Executor\Promise\Promise;
10
use GraphQL\Executor\Promise\PromiseAdapter;
11
use GraphQL\GraphQL;
12
use GraphQL\Type\Definition\ObjectType;
13
use GraphQL\Type\Schema;
14
use Portiny\GraphQL\Contract\Http\Request\RequestParserInterface;
15
use Portiny\GraphQL\Contract\Provider\MutationFieldsProviderInterface;
16
use Portiny\GraphQL\Contract\Provider\QueryFieldsProviderInterface;
17
use Portiny\GraphQL\GraphQL\Schema\SchemaCacheProvider;
18
use Throwable;
19
use Tracy\ILogger;
20
21
final class RequestProcessor
22
{
23
	/**
24
	 * @var bool
25
	 */
26
	private $debugMode = false;
27
28
	/**
29
	 * @var bool
30
	 */
31
	private $schemaCache = false;
32
33
	/**
34
	 * @var MutationFieldsProviderInterface
35
	 */
36
	private $mutationFieldsProvider;
37
38
	/**
39
	 * @var QueryFieldsProviderInterface
40
	 */
41
	private $queryFieldsProvider;
42
43
	/**
44
	 * @var SchemaCacheProvider
45
	 */
46
	private $schemaCacheProvider;
47
48 2
	public function __construct(
49
		bool $debugMode,
50
		MutationFieldsProviderInterface $mutationFieldsProvider,
51
		QueryFieldsProviderInterface $queryFieldsProvider,
52
		SchemaCacheProvider $schemaCacheProvider
53
	) {
54 2
		$this->debugMode = $debugMode;
55 2
		$this->mutationFieldsProvider = $mutationFieldsProvider;
56 2
		$this->queryFieldsProvider = $queryFieldsProvider;
57 2
		$this->schemaCacheProvider = $schemaCacheProvider;
58 2
	}
59
60 1
	public function setSchemaCache(bool $useSchemaCache): void
61
	{
62 1
		$this->schemaCache = $useSchemaCache;
63 1
	}
64
65
	/**
66
	 * @param mixed|null $context
67
	 * @param array|null $allowedQueries
68
	 * @param array|null $allowedMutations
69
	 */
70 1
	public function process(
71
		RequestParserInterface $requestParser,
72
		array $rootValue = [],
73
		$context = null,
74
		?array $allowedQueries = null,
75
		?array $allowedMutations = null,
76
		?ILogger $logger = null
77
	): array {
78
		try {
79 1
			if ($this->schemaCache && $this->schemaCacheProvider->isCached()) {
80
				$schema = $this->schemaCacheProvider->getSchema();
81
			} else {
82 1
				$schema = $this->createSchema($allowedQueries, $allowedMutations);
83 1
				if ($this->schemaCache) {
84
					$this->schemaCacheProvider->save($schema);
85
				}
86
			}
87
88 1
			$result = GraphQL::executeQuery(
89 1
				$schema,
90 1
				$requestParser->getQuery(),
91
				$rootValue,
92
				$context,
93 1
				$requestParser->getVariables()
94
			);
95
96 1
			$output = $result->toArray($this->detectDebugLevel($logger));
97
		} catch (Throwable $throwable) {
98
			if ($logger) {
99
				$logger->log($throwable, ILogger::EXCEPTION);
100
			}
101
102
			$output = [
103
				'errors' => [FormattedError::createFromException($throwable, false, 'An error occurred.')],
104
			];
105
		}
106
107 1
		return $output;
108
	}
109
110
	/**
111
	 * @param mixed|null $context
112
	 * @param array|null $allowedQueries
113
	 * @param array|null $allowedMutations
114
	 */
115
	public function processViaPromise(
116
		PromiseAdapter $promiseAdapter,
117
		RequestParserInterface $requestParser,
118
		array $rootValue = [],
119
		$context = null,
120
		?array $allowedQueries = null,
121
		?array $allowedMutations = null,
122
		?ILogger $logger = null
123
	): Promise {
124
		try {
125
			return GraphQL::promiseToExecute(
126
				$promiseAdapter,
127
				$this->createSchema($allowedQueries, $allowedMutations),
128
				$requestParser->getQuery(),
129
				$rootValue,
130
				$context,
131
				$requestParser->getVariables()
132
			);
133
		} catch (Throwable $throwable) {
134
			if ($logger) {
135
				$logger->log($throwable);
136
			}
137
138
			return $promiseAdapter->createRejected($throwable);
139
		}
140
	}
141
142 1
	private function createSchema(?array $allowedQueries = null, ?array $allowedMutations = null): Schema
143
	{
144
		$configuration = [
145 1
			'query' => $this->createQueryObject($allowedQueries),
146
		];
147
148 1
		$mutationObject = $this->createMutationObject($allowedMutations);
149 1
		if ($mutationObject->getFields()) {
150 1
			$configuration['mutation'] = $mutationObject;
151
		}
152
153 1
		return new Schema($configuration);
154
	}
155
156 1
	private function detectDebugLevel(?ILogger $logger): int
157
	{
158 1
		return $this->debugMode
159
			? Debug::INCLUDE_DEBUG_MESSAGE | Debug::INCLUDE_TRACE
160 1
			: ($logger === null ? 0 : Debug::RETHROW_INTERNAL_EXCEPTIONS);
161
	}
162
163 1
	private function createQueryObject(?array $allowedQueries = null): ObjectType
164
	{
165 1
		return new ObjectType([
166 1
			'name' => 'Query',
167 1
			'fields' => $this->queryFieldsProvider->convertFieldsToArray($allowedQueries),
168
		]);
169
	}
170
171 1
	private function createMutationObject(?array $allowedMutations = null): ObjectType
172
	{
173 1
		return new ObjectType([
174 1
			'name' => 'Mutation',
175 1
			'fields' => $this->mutationFieldsProvider->convertFieldsToArray($allowedMutations),
176
		]);
177
	}
178
}
179