Completed
Push — master ( 031fc2...ad5074 )
by Tomáš
01:27
created

RequestProcessor   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 72.09%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 117
ccs 31
cts 43
cp 0.7209
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setMutationFieldsProvider() 0 4 1
A setQueryFieldsProvider() 0 4 1
A process() 0 27 2
A processViaPromise() 0 21 2
A createSchema() 0 13 2
A createQueryObject() 0 7 1
A createMutationObject() 0 7 1
A isDebug() 0 4 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Portiny\GraphQL\GraphQL;
6
7
use GraphQL\Error\Debug;
8
use GraphQL\Executor\Promise\Promise;
9
use GraphQL\Executor\Promise\PromiseAdapter;
10
use GraphQL\GraphQL;
11
use GraphQL\Type\Definition\ObjectType;
12
use GraphQL\Type\Schema;
13
use Portiny\GraphQL\Contract\Http\Request\RequestParserInterface;
14
use Portiny\GraphQL\Contract\Provider\MutationFieldsProviderInterface;
15
use Portiny\GraphQL\Contract\Provider\QueryFieldsProviderInterface;
16
use Throwable;
17
use Tracy\Debugger;
18
19
final class RequestProcessor
20
{
21
	/**
22
	 * @var MutationFieldsProviderInterface
23
	 */
24
	private $mutationFieldsProvider;
25
26
	/**
27
	 * @var QueryFieldsProviderInterface
28
	 */
29
	private $queryFieldsProvider;
30
31 2
	public function setMutationFieldsProvider(MutationFieldsProviderInterface $mutationFieldsProvider): void
32
	{
33 2
		$this->mutationFieldsProvider = $mutationFieldsProvider;
34 2
	}
35
36 2
	public function setQueryFieldsProvider(QueryFieldsProviderInterface $queryFieldsProvider): void
37
	{
38 2
		$this->queryFieldsProvider = $queryFieldsProvider;
39 2
	}
40
41
	/**
42
	 * @param mixed|null $context
43
	 * @param array|null $allowedQueries
44
	 * @param array|null $allowedMutations
45
	 */
46 1
	public function process(
47
		RequestParserInterface $requestParser,
48
		array $rootValue = [],
49
		$context = NULL,
50
		?array $allowedQueries = NULL,
51
		?array $allowedMutations = NULL
52
	): array {
53
		try {
54 1
			$result = GraphQL::executeQuery(
55 1
				$this->createSchema($allowedQueries, $allowedMutations),
56 1
				$requestParser->getQuery(),
57 1
				$rootValue,
58 1
				$context,
59 1
				$requestParser->getVariables()
60
			);
61
62 1
			$output = $result->toArray($this->isDebug());
63
		} catch (Throwable $exception) {
64
			$output = [
65
				'error' => [
66
					'message' => $exception->getMessage(),
67
				],
68
			];
69
		}
70
71 1
		return $output;
72
	}
73
74
	/**
75
	 * @param mixed|null $context
76
	 * @param array|null $allowedQueries
77
	 * @param array|null $allowedMutations
78
	 */
79
	public function processViaPromise(
80
		PromiseAdapter $promiseAdapter,
81
		RequestParserInterface $requestParser,
82
		array $rootValue = [],
83
		$context = NULL,
84
		?array $allowedQueries = NULL,
85
		?array $allowedMutations = NULL
86
	): Promise {
87
		try {
88
			return GraphQL::promiseToExecute(
89
				$promiseAdapter,
90
				$this->createSchema($allowedQueries, $allowedMutations),
91
				$requestParser->getQuery(),
92
				$rootValue,
93
				$context,
94
				$requestParser->getVariables()
95
			);
96
		} catch (Throwable $exception) {
97
			return $promiseAdapter->createRejected($exception);
98
		}
99
	}
100
101 1
	private function createSchema(?array $allowedQueries = NULL, ?array $allowedMutations = NULL): Schema
102
	{
103
		$configuration = [
104 1
			'query' => $this->createQueryObject($allowedQueries),
105
		];
106
107 1
		$mutationObject = $this->createMutationObject($allowedMutations);
108 1
		if ($mutationObject->getFields()) {
109 1
			$configuration['mutation'] = $mutationObject;
110
		}
111
112 1
		return new Schema($configuration);
113
	}
114
115 1
	private function createQueryObject(?array $allowedQueries = NULL): ObjectType
116
	{
117 1
		return new ObjectType([
118 1
			'name' => 'Query',
119 1
			'fields' => $this->queryFieldsProvider->convertFieldsToArray($allowedQueries),
120
		]);
121
	}
122
123 1
	private function createMutationObject(?array $allowedMutations = NULL): ObjectType
124
	{
125 1
		return new ObjectType([
126 1
			'name' => 'Mutation',
127 1
			'fields' => $this->mutationFieldsProvider->convertFieldsToArray($allowedMutations),
128
		]);
129
	}
130
131 1
	private function isDebug(): int
132
	{
133 1
		return ! Debugger::$productionMode ? Debug::INCLUDE_DEBUG_MESSAGE | Debug::INCLUDE_TRACE : 0;
134
	}
135
}
136