Passed
Push — master ( 7a0761...b9ca99 )
by Bruno
03:45
created

FormulariumUtils   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 54.35%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 53
dl 0
loc 98
ccs 25
cts 46
cp 0.5435
rs 10
c 1
b 0
f 0
wmc 12

2 Methods

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