Test Setup Failed
Push — master ( 1595cb...050b68 )
by Kirill
21:00
created

ObjectLikeTypeDefinitionContext   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 13
dl 0
loc 51
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A buildFieldDefinition() 0 15 3
A buildArgumentDefinition() 0 15 3
1
<?php
2
3
/**
4
 * This file is part of Railt package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Railt\SDL\Backend\Context;
13
14
use GraphQL\Contracts\TypeSystem\ArgumentInterface;
15
use GraphQL\Contracts\TypeSystem\FieldInterface;
16
use Railt\SDL\Frontend\Ast\Definition\ArgumentDefinitionNode;
17
use Railt\SDL\Frontend\Ast\Definition\FieldDefinitionNode;
18
use Railt\TypeSystem\Argument;
19
use Railt\TypeSystem\Exception\TypeUniquenessException;
20
use Railt\TypeSystem\Field;
21
22
/**
23
 * Class ObjectLikeTypeDefinitionContext
24
 */
25
abstract class ObjectLikeTypeDefinitionContext extends NamedDefinitionContext
26
{
27
    /**
28
     * @param FieldDefinitionNode $node
29
     * @param array $args
30
     * @return FieldInterface
31
     * @throws \InvalidArgumentException
32
     * @throws \LogicException
33
     * @throws TypeUniquenessException
34
     * @throws \Throwable
35
     */
36
    protected function buildFieldDefinition(FieldDefinitionNode $node, array $args = []): FieldInterface
37
    {
38
        $field = new Field($node->name->value, $this->typeOf($node->type, $args), [
39
            'description' => $this->descriptionOf($node),
40
        ]);
41
42
        foreach ($node->arguments as $argument) {
43
            $field->addArgument($this->buildArgumentDefinition($argument, $args));
44
        }
45
46
        foreach ($node->directives as $directive) {
47
            // TODO Directive
48
        }
49
50
        return $field;
51
    }
52
53
    /**
54
     * @param ArgumentDefinitionNode $node
55
     * @param array $args
56
     * @return ArgumentInterface
57
     * @throws \InvalidArgumentException
58
     * @throws \LogicException
59
     * @throws \Throwable
60
     */
61
    protected function buildArgumentDefinition(ArgumentDefinitionNode $node, array $args = []): ArgumentInterface
62
    {
63
        $argument = new Argument($node->name->value, $this->typeOf($node->type, $args), [
64
            'description' => $this->descriptionOf($node),
65
        ]);
66
67
        if ($node->defaultValue) {
68
            $argument->setDefaultValue($this->value($node->defaultValue));
69
        }
70
71
        foreach ($node->directives as $directive) {
72
            // TODO Directive
73
        }
74
75
        return $argument;
76
    }
77
}
78