Passed
Push — master ( c0a95b...c5e7d6 )
by Bruno
04:51
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 ErrorException;
6
use Formularium\Exception\ClassNotFoundException;
7
use Formularium\Extradata;
8
use Formularium\ExtradataParameter;
9
use Formularium\Field;
10
use Formularium\Factory\ValidatorFactory;
11
use Formularium\Metadata;
12
use GraphQL\Language\AST\DirectiveNode;
13
use GraphQL\Language\AST\NodeList;
14
use Modelarium\Exception\Exception;
15
16
class FormulariumUtils
17
{
18 10
    public static function getFieldFromDirectives(
19
        string $fieldName,
20
        string $datatypeName,
21
        NodeList $directives
22
    ): Field {
23 10
        $validators = [];
24 10
        $renderable = [];
25 10
        $extradata = [];
26 10
        foreach ($directives as $directive) {
27 8
            $name = $directive->name->value;
28
29 8
            if ($name === 'renderable') {
30
                foreach ($directive->arguments as $arg) {
31
                    /**
32
                     * @var \GraphQL\Language\AST\ArgumentNode $arg
33
                     */
34
35
                    $argName = $arg->name->value;
36
                    $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...
37
                    $renderable[$argName] = $argValue;
38
                }
39
                continue;
40
            }
41
42 8
            $extradata[] = FormulariumUtils::directiveToExtradata($directive);
43
44 8
            $validator = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $validator is dead and can be removed.
Loading history...
45
            try {
46 8
                $validator = ValidatorFactory::class($name);
47 8
            } catch (ClassNotFoundException $e) {
48 8
                continue;
49
            }
50
51
            /**
52
             * @var Metadata $metadata
53
             */
54
            $metadata = $validator::getMetadata();
55
            $arguments = [];
56
57
            foreach ($directive->arguments as $arg) {
58
                /**
59
                 * @var \GraphQL\Language\AST\ArgumentNode $arg
60
                 */
61
62
                $argName = $arg->name->value;
63
                $argValue = $arg->value->value; /** @phpstan-ignore-line */
64
65
                $argValidator = $metadata->parameter($argName);
66
                if (!$argValidator) {
67
                    throw new Exception("Directive $validator does not have argument $argName");
68
                }
69
                if ($argValidator->type === 'Int') {
70
                    $argValue = (int)$argValue;
71
                }
72
                $arguments[$argName] = $argValue;
73
            }
74
75
            $validators[$name] = $arguments;
76
        }
77
78 10
        return new Field(
79 10
            $fieldName,
80
            $datatypeName,
81
            $renderable,
82
            $validators,
83
            $extradata
84
        );
85
    }
86
87 9
    public static function directiveToExtradata(DirectiveNode $directive): Extradata
88
    {
89 9
        $metadataArgs = [];
90 9
        foreach ($directive->arguments as $arg) {
91 4
            $metadataArgs[] = new ExtradataParameter(
92 4
                $arg->name->value,
93
                // @phpstan-ignore-next-line
94 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...
95
            );
96
        }
97 9
        return new Extradata(
98 9
            $directive->name->value,
99
            $metadataArgs
100
        );
101
    }
102
}
103