Passed
Push — master ( 3d645a...097498 )
by Tomáš
05:27
created

anonymous//src/Converter/QueryFieldConverter.php$0   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Portiny\GraphQL\Converter;
6
7
use GraphQL\Type\Definition\Type;
8
use Portiny\GraphQL\Contract\Field\QueryFieldInterface;
9
10
final class QueryFieldConverter
11
{
12 4
	public static function toArray(QueryFieldInterface $queryField): array
13
	{
14
		return [
15 4
			$queryField->getName() => [
16 4
				'type' => $queryField->getType(),
17 4
				'description' => $queryField->getDescription(),
18 4
				'args' => $queryField->getArgs(),
19
				'resolve' => function ($root, $args, $context) use ($queryField) {
20 2
					return call_user_func_array([$queryField, 'resolve'], [$root, $args, $context]);
21 4
				},
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 1
			public function __construct(array $data)
40
			{
41 1
				$this->name = (string) key($data);
42 1
				$this->data = (array) reset($data);
43 1
			}
44
45
			/**
46
			 * {@inheritdoc}
47
			 */
48 1
			public function getName(): string
49
			{
50 1
				return $this->name;
51
			}
52
53
			/**
54
			 * {@inheritdoc}
55
			 */
56 1
			public function getDescription(): string
57
			{
58 1
				return $this->data['description'];
59
			}
60
61
			/**
62
			 * {@inheritdoc}
63
			 */
64 1
			public function getArgs(): array
65
			{
66 1
				return $this->data['args'];
67
			}
68
69
			/**
70
			 * {@inheritdoc}
71
			 */
72 1
			public function getType(): Type
73
			{
74 1
				return $this->data['type'];
75
			}
76
77
			/**
78
			 * {@inheritdoc}
79
			 */
80 1
			public function resolve(array $root, array $args, $context = null)
81
			{
82 1
				return $this->data['resolve']($root, $args, $context);
83
			}
84
		};
85
	}
86
}
87