MutationFieldConverter.php$0 ➔ __construct()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php declare(strict_types = 1);
2
3
namespace Portiny\GraphQL\Converter;
4
5
use GraphQL\Type\Definition\Type;
6
use Portiny\GraphQL\Contract\Mutation\MutationFieldInterface;
7
8
class MutationFieldConverter
9
{
10
11 4
	public static function toArray(MutationFieldInterface $mutationField): array
12
	{
13
		return [
14 4
			$mutationField->getName() => [
15 4
				'type' => $mutationField->getType(),
16 4
				'description' => $mutationField->getDescription(),
17 4
				'args' => $mutationField->getArgs(),
18
				'resolve' => function ($root, $args, $context) use ($mutationField) {
19 2
					return call_user_func_array([$mutationField, 'resolve'], [$root, $args, $context]);
20 4
				},
21
			],
22
		];
23
	}
24
25
26
	public static function toObject(array $data): MutationFieldInterface
27
	{
28
		return new class($data) implements MutationFieldInterface {
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