Completed
Push — master ( ac54ef...b19cf5 )
by Tomáš
01:31
created

RequestProcessor::createQueryObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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