Completed
Push — master ( 1826e3...724c6f )
by Rafael
07:29
created

GraphQLBuilder   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 17
dl 0
loc 91
ccs 25
cts 30
cp 0.8333
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B buildComplexityFn() 0 21 5
B buildArguments() 0 27 6
B resolveFields() 0 37 6
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Util;
12
13
use GraphQL\Type\Definition\Type;
14
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
15
use Ynlo\GraphQLBundle\Definition\ArgumentAwareInterface;
16
use Ynlo\GraphQLBundle\Definition\FieldsAwareDefinitionInterface;
17
use Ynlo\GraphQLBundle\Type\Registry\TypeRegistry;
18
19
class GraphQLBuilder
20 22
{
21
    public static function resolveFields(FieldsAwareDefinitionInterface $definition): array
22 22
    {
23 22
        $fields = [];
24 22
        foreach ($definition->getFields() as $fieldDefinition) {
25 22
            try {
26 22
                $type = TypeRegistry::get($fieldDefinition->getType());
27
            } catch (\UnexpectedValueException $exception) {
28 22
                $msg = sprintf(
29 22
                    'The property "%s" of object "%s" does not have valid type. %s',
30 22
                    $fieldDefinition->getName(),
31
                    $definition->getName(),
32 22
                    $exception->getMessage()
33
                );
34
                throw new \RuntimeException($msg);
35 22
            }
36 22
37
            if ($fieldDefinition->isList()) {
38
                if ($fieldDefinition->isNonNullList()) {
39 22
                    $type = Type::nonNull($type);
40 22
                }
41
                $type = Type::listOf($type);
42
            }
43 22
44
            if ($fieldDefinition->isNonNull()) {
45
                $type = Type::nonNull($type);
46 22
            }
47
48
            $fields[$fieldDefinition->getName()] = [
49 22
                'type' => $type,
50
                'description' => $fieldDefinition->getDescription(),
51 22
                'deprecationReason' => $fieldDefinition->getDeprecationReason(),
52 22
                'args' => GraphQLBuilder::buildArguments($fieldDefinition),
53
                'complexity' => GraphQLBuilder::buildComplexityFn($fieldDefinition->getComplexity()),
54
            ];
55 22
        }
56
57
        return $fields;
58
    }
59
60
    public static function buildArguments(ArgumentAwareInterface $argumentAware): array
61
    {
62 22
        $args = [];
63
        foreach ($argumentAware->getArguments() as $argDefinition) {
64
            $arg = [];
65
            $arg['description'] = $argDefinition->getDescription();
66 22
            $argType = TypeRegistry::get($argDefinition->getType());
67
68 22
            if ($argDefinition->isList()) {
69 2
                if ($argDefinition->isNonNullList()) {
70 22
                    $argType = Type::nonNull($argType);
71
                }
72
                $argType = Type::listOf($argType);
73
            }
74
75
            if ($argDefinition->isNonNull()) {
76
                $argType = Type::nonNull($argType);
77
            }
78
79
            $arg['type'] = $argType;
80
            if ($argDefinition->getDefaultValue()) {
81
                $arg['defaultValue'] = $argDefinition->getDefaultValue();
82
            }
83
            $args[$argDefinition->getName()] = $arg;
84
        }
85
86
        return $args;
87
    }
88
89
    public static function buildComplexityFn(?string $complexity): ?callable
90
    {
91
        if (null === $complexity) {
92
            return null;
93
        }
94
95
        if (is_numeric($complexity)) {
96
            return function ($childrenComplexity) use ($complexity) {
97
                return $childrenComplexity + $complexity;
98
            };
99
        }
100
101
        // support only static string callable func
102
        if (\is_string($complexity) && \is_callable($complexity)) {
103
            return $complexity;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $complexity returns the type string which is incompatible with the type-hinted return null|callable.
Loading history...
104
        }
105
106
        $el = new ExpressionLanguage();
107
108
        return function ($childrenComplexity, $args) use ($el, $complexity) {
109
            return $el->evaluate($complexity, array_merge($args, ['children_complexity' => $childrenComplexity]));
110
        };
111
    }
112
}
113