Passed
Push — master ( 193c16...705327 )
by Tomáš
11:03
created

QueryFieldConverter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 18
c 2
b 0
f 0
dl 0
loc 81
ccs 21
cts 21
cp 1
rs 10
wmc 7

9 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ getDescription() 0 3 1
A hp$0 ➔ __construct() 0 4 1
B hp$0 ➔ toObject() 0 63 1
A hp$0 ➔ getArgs() 0 3 1
A hp$0 ➔ getType() 0 3 1
A hp$0 ➔ resolve() 0 3 1
A hp$0 ➔ getName() 0 3 1
A toArray() 0 9 1
toObject() 0 63 ?
1
<?php declare(strict_types = 1);
2
3
namespace Portiny\GraphQL\Converter;
4
5
use GraphQL\Type\Definition\Type;
6
use Portiny\GraphQL\Contract\Field\QueryFieldInterface;
7
8
final class QueryFieldConverter
9
{
10
11 4
	public static function toArray(QueryFieldInterface $queryField): array
12
	{
13
		return [
14 4
			$queryField->getName() => [
15 4
				'type' => $queryField->getType(),
16 4
				'description' => $queryField->getDescription(),
17 4
				'args' => $queryField->getArgs(),
18
				'resolve' => function ($root, $args, $context) use ($queryField) {
19 2
					return call_user_func_array([$queryField, 'resolve'], [$root, $args, $context]);
20 4
				},
21
			],
22
		];
23
	}
24
25
26
	public static function toObject(array $data): QueryFieldInterface
27
	{
28
		return new class($data) implements QueryFieldInterface {
29
			/**
30
			 * @var string
31
			 */
32
			private $name;
33
34
			/**
35
			 * @var array
36
			 */
37
			private $data = [];
38
39
40 1
			public function __construct(array $data)
41
			{
42 1
				$this->name = (string) key($data);
43 1
				$this->data = (array) reset($data);
44 1
			}
45
46
47
			/**
48
			 * {@inheritdoc}
49
			 */
50 1
			public function getName(): string
51
			{
52 1
				return $this->name;
53
			}
54
55
56
			/**
57
			 * {@inheritdoc}
58
			 */
59 1
			public function getDescription(): string
60
			{
61 1
				return $this->data['description'];
62
			}
63
64
65
			/**
66
			 * {@inheritdoc}
67
			 */
68 1
			public function getArgs(): array
69
			{
70 1
				return $this->data['args'];
71
			}
72
73
74
			/**
75
			 * {@inheritdoc}
76
			 */
77 1
			public function getType(): Type
78
			{
79 1
				return $this->data['type'];
80
			}
81
82
83
			/**
84
			 * {@inheritdoc}
85
			 */
86 1
			public function resolve(array $root, array $args, $context = null)
87
			{
88 1
				return $this->data['resolve']($root, $args, $context);
89
			}
90
91
		};
92
	}
93
94
}
95