Passed
Push — master ( 253428...340885 )
by Bruno
03:15
created

FormulariumUtils::directiveToExtradata()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php declare(strict_types=1);
2
3
namespace Modelarium;
4
5
use Formularium\Exception\ClassNotFoundException;
6
use Formularium\Extradata;
7
use Formularium\ExtradataParameter;
8
use Formularium\Field;
9
use Formularium\Factory\ValidatorFactory;
10
use Formularium\Metadata;
11
use GraphQL\Language\AST\DirectiveNode;
12
use GraphQL\Language\AST\NodeList;
13
use Modelarium\Exception\Exception;
14
15
class FormulariumUtils
16
{
17 10
    public static function getFieldFromDirectives(
18
        string $fieldName,
19
        string $datatypeName,
20
        NodeList $directives
21
    ): Field {
22 10
        $validators = [];
23 10
        $renderable = [];
24 10
        $extradata = [];
25 10
        foreach ($directives as $directive) {
26 8
            $name = $directive->name->value;
27
28 8
            if ($name === 'renderable') {
29
                foreach ($directive->arguments as $arg) {
30
                    /**
31
                     * @var \GraphQL\Language\AST\ArgumentNode $arg
32
                     */
33
34
                    $argName = $arg->name->value;
35
                    $argValue = $arg->value->value; /** @phpstan-ignore-line */
0 ignored issues
show
Bug introduced by
Accessing value on the interface GraphQL\Language\AST\ValueNode suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
36
                    $renderable[$argName] = $argValue;
37
                }
38
                continue;
39
            }
40
41 8
            $extradata[] = FormulariumUtils::directiveToExtradata($directive);
42
43 8
            $validator = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $validator is dead and can be removed.
Loading history...
44
            try {
45 8
                $validator = ValidatorFactory::class($name);
46 8
            } catch (ClassNotFoundException $e) {
47 8
                continue;
48
            }
49
50
            /**
51
             * @var Metadata $metadata
52
             */
53
            $metadata = $validator::getMetadata();
54
            $arguments = [];
55
56
            foreach ($directive->arguments as $arg) {
57
                /**
58
                 * @var \GraphQL\Language\AST\ArgumentNode $arg
59
                 */
60
61
                $argName = $arg->name->value;
62
                $argValue = $arg->value->value; /** @phpstan-ignore-line */
63
64
                $argValidator = $metadata->parameter($argName);
65
                if (!$argValidator) {
66
                    throw new Exception("Directive $validator does not have argument $argName");
67
                }
68
                if ($argValidator->type === 'Int') {
69
                    $argValue = (int)$argValue;
70
                }
71
                $arguments[$argName] = $argValue;
72
            }
73
74
            $validators[$name] = $arguments;
75
        }
76
77 10
        return new Field(
78 10
            $fieldName,
79
            $datatypeName,
80
            $renderable,
81
            $validators,
82
            $extradata
83
        );
84
    }
85
86 9
    public static function directiveToExtradata(DirectiveNode $directive): Extradata
87
    {
88 9
        $metadataArgs = [];
89 9
        foreach ($directive->arguments as $arg) {
90 4
            $metadataArgs[] = new ExtradataParameter(
91 4
                $arg->name->value,
92
                // @phpstan-ignore-next-line
93 4
                $arg->value->value
0 ignored issues
show
Bug introduced by
Accessing value on the interface GraphQL\Language\AST\ValueNode suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
94
            );
95
        }
96 9
        return new Extradata(
97 9
            $directive->name->value,
98
            $metadataArgs
99
        );
100
    }
101
}
102