Completed
Push — master ( 26901c...c05564 )
by Tomáš
01:12
created

RequestProcessor   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 93
ccs 32
cts 34
cp 0.9412
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A createQueryObject() 0 7 1
A __construct() 0 4 1
A setMutationFieldsProvider() 0 4 1
A setQueryFieldsProvider() 0 4 1
B process() 0 26 2
A createSchema() 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\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 RequestParserInterface
21
	 */
22
	private $requestParser;
23
24
	/**
25
	 * @var MutationFieldsProviderInterface
26
	 */
27
	private $mutationFieldsProvider;
28
29
	/**
30
	 * @var QueryFieldsProviderInterface
31
	 */
32
	private $queryFieldsProvider;
33
34 2
	public function __construct(RequestParserInterface $requestParser)
35
	{
36 2
		$this->requestParser = $requestParser;
37 2
	}
38
39 2
	public function setMutationFieldsProvider(MutationFieldsProviderInterface $mutationFieldsProvider): void
40
	{
41 2
		$this->mutationFieldsProvider = $mutationFieldsProvider;
42 2
	}
43
44 2
	public function setQueryFieldsProvider(QueryFieldsProviderInterface $queryFieldsProvider): void
45
	{
46 2
		$this->queryFieldsProvider = $queryFieldsProvider;
47 2
	}
48
49
	/**
50
	 * @param mixed|null $context
51
	 * @param array|null $allowedQueries
52
	 * @param array|null $allowedMutations
53
	 */
54 1
	public function process(
55
		array $rootValue = [],
56
		$context = NULL,
57
		?array $allowedQueries = NULL,
58
		?array $allowedMutations = NULL
59
	): array {
60
		try {
61 1
			$result = GraphQL::executeQuery(
62 1
				$this->createSchema($allowedQueries, $allowedMutations),
63 1
				$this->requestParser->getQuery(),
64 1
				$rootValue,
65 1
				$context,
66 1
				$this->requestParser->getVariables()
67
			);
68
69 1
			$output = $result->toArray($this->isDebug());
70
		} catch (Throwable $exception) {
71
			$output = [
72
				'error' => [
73
					'message' => $exception->getMessage(),
74
				],
75
			];
76
		}
77
78 1
		return $output;
79
	}
80
81 1
	private function createSchema(?array $allowedQueries = NULL, ?array $allowedMutations = NULL): Schema
82
	{
83 1
		return new Schema([
84 1
			'query' => $this->createQueryObject($allowedQueries),
85 1
			'mutation' => $this->createMutationObject($allowedMutations),
86
		]);
87
	}
88
89 1
	private function createQueryObject(?array $allowedQueries = NULL): ObjectType
90
	{
91 1
		return new ObjectType([
92 1
			'name' => 'Query',
93 1
			'fields' => $this->queryFieldsProvider->convertFieldsToArray($allowedQueries),
94
		]);
95
	}
96
97 1
	private function createMutationObject(?array $allowedMutations = NULL): ObjectType
98
	{
99 1
		return new ObjectType([
100 1
			'name' => 'Mutation',
101 1
			'fields' => $this->mutationFieldsProvider->convertFieldsToArray($allowedMutations),
102
		]);
103
	}
104
105 1
	private function isDebug(): int
106
	{
107 1
		return ! Debugger::$productionMode ? Debug::INCLUDE_DEBUG_MESSAGE | Debug::INCLUDE_TRACE : 0;
108
	}
109
}
110