Completed
Push — master ( 7d44d2...620869 )
by Tomáš
04:35
created

RequestProcessor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Portiny\GraphQL\GraphQL;
6
7
use GraphQL\Error\Debug;
8
use GraphQL\GraphQL;
9
use GraphQL\Type\Definition\ObjectType;
10
use GraphQL\Type\Schema;
11
use Portiny\GraphQL\Contract\Http\Request\RequestParserInterface;
12
use Portiny\GraphQL\Contract\Provider\MutationFieldsProviderInterface;
13
use Portiny\GraphQL\Contract\Provider\QueryFieldsProviderInterface;
14
use Throwable;
15
use Tracy\Debugger;
16
17
final class RequestProcessor
18
{
19
	/**
20
	 * @var MutationFieldsProviderInterface
21
	 */
22
	private $mutationFieldsProvider;
23
24
	/**
25
	 * @var QueryFieldsProviderInterface
26
	 */
27
	private $queryFieldsProvider;
28
29 2
	public function setMutationFieldsProvider(MutationFieldsProviderInterface $mutationFieldsProvider): void
30
	{
31 2
		$this->mutationFieldsProvider = $mutationFieldsProvider;
32 2
	}
33
34 2
	public function setQueryFieldsProvider(QueryFieldsProviderInterface $queryFieldsProvider): void
35
	{
36 2
		$this->queryFieldsProvider = $queryFieldsProvider;
37 2
	}
38
39
	/**
40
	 * @param mixed|null $context
41
	 * @param array|null $allowedQueries
42
	 * @param array|null $allowedMutations
43
	 */
44 1
	public function process(
45
		RequestParserInterface $requestParser,
46
		array $rootValue = [],
47
		$context = NULL,
48
		?array $allowedQueries = NULL,
49
		?array $allowedMutations = NULL
50
	): array {
51
		try {
52 1
			$result = GraphQL::executeQuery(
53 1
				$this->createSchema($allowedQueries, $allowedMutations),
54 1
				$requestParser->getQuery(),
55 1
				$rootValue,
56 1
				$context,
57 1
				$requestParser->getVariables()
58
			);
59
60 1
			$output = $result->toArray($this->isDebug());
61
		} catch (Throwable $exception) {
62
			$output = [
63
				'error' => [
64
					'message' => $exception->getMessage(),
65
				],
66
			];
67
		}
68
69 1
		return $output;
70
	}
71
72 1
	private function createSchema(?array $allowedQueries = NULL, ?array $allowedMutations = NULL): Schema
73
	{
74 1
		return new Schema([
75 1
			'query' => $this->createQueryObject($allowedQueries),
76 1
			'mutation' => $this->createMutationObject($allowedMutations),
77
		]);
78
	}
79
80 1
	private function createQueryObject(?array $allowedQueries = NULL): ObjectType
81
	{
82 1
		return new ObjectType([
83 1
			'name' => 'Query',
84 1
			'fields' => $this->queryFieldsProvider->convertFieldsToArray($allowedQueries),
85
		]);
86
	}
87
88 1
	private function createMutationObject(?array $allowedMutations = NULL): ObjectType
89
	{
90 1
		return new ObjectType([
91 1
			'name' => 'Mutation',
92 1
			'fields' => $this->mutationFieldsProvider->convertFieldsToArray($allowedMutations),
93
		]);
94
	}
95
96 1
	private function isDebug(): int
97
	{
98 1
		return ! Debugger::$productionMode ? Debug::INCLUDE_DEBUG_MESSAGE | Debug::INCLUDE_TRACE : 0;
99
	}
100
}
101