Passed
Push — master ( 724c6f...9da6fd )
by Rafael
06:22
created

GraphQLBuilder   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 77.35%

Importance

Changes 0
Metric Value
wmc 17
dl 0
loc 91
ccs 41
cts 53
cp 0.7735
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
{
21 22
    public static function resolveFields(FieldsAwareDefinitionInterface $definition): array
22
    {
23 22
        $fields = [];
24 22
        foreach ($definition->getFields() as $fieldDefinition) {
25
            try {
26 22
                $type = TypeRegistry::get($fieldDefinition->getType());
27
            } catch (\UnexpectedValueException $exception) {
28
                $msg = sprintf(
29
                    'The property "%s" of object "%s" does not have valid type. %s',
30
                    $fieldDefinition->getName(),
31
                    $definition->getName(),
32
                    $exception->getMessage()
33
                );
34
                throw new \RuntimeException($msg);
35
            }
36
37 22
            if ($fieldDefinition->isList()) {
38 22
                if ($fieldDefinition->isNonNullList()) {
39 22
                    $type = Type::nonNull($type);
40
                }
41 22
                $type = Type::listOf($type);
42
            }
43
44 22
            if ($fieldDefinition->isNonNull()) {
45 22
                $type = Type::nonNull($type);
46
            }
47
48 22
            $fields[$fieldDefinition->getName()] = [
49 22
                'type' => $type,
50 22
                'description' => $fieldDefinition->getDescription(),
51 22
                'deprecationReason' => $fieldDefinition->getDeprecationReason(),
52 22
                'args' => GraphQLBuilder::buildArguments($fieldDefinition),
53 22
                'complexity' => GraphQLBuilder::buildComplexityFn($fieldDefinition->getComplexity()),
54
            ];
55
        }
56
57 22
        return $fields;
58
    }
59
60 22
    public static function buildArguments(ArgumentAwareInterface $argumentAware): array
61
    {
62 22
        $args = [];
63 22
        foreach ($argumentAware->getArguments() as $argDefinition) {
64 22
            $arg = [];
65 22
            $arg['description'] = $argDefinition->getDescription();
66 22
            $argType = TypeRegistry::get($argDefinition->getType());
67
68 22
            if ($argDefinition->isList()) {
69 22
                if ($argDefinition->isNonNullList()) {
70 22
                    $argType = Type::nonNull($argType);
71
                }
72 22
                $argType = Type::listOf($argType);
73
            }
74
75 22
            if ($argDefinition->isNonNull()) {
76 22
                $argType = Type::nonNull($argType);
77
            }
78
79 22
            $arg['type'] = $argType;
80 22
            if ($argDefinition->getDefaultValue()) {
81
                $arg['defaultValue'] = $argDefinition->getDefaultValue();
82
            }
83 22
            $args[$argDefinition->getName()] = $arg;
84
        }
85
86 22
        return $args;
87
    }
88
89 22
    public static function buildComplexityFn(?string $complexity): ?callable
90
    {
91 22
        if (null === $complexity) {
92 22
            return null;
93
        }
94
95 22
        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 22
        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 22
        $el = new ExpressionLanguage();
107
108 22
        return function ($childrenComplexity, $args) use ($el, $complexity) {
109 2
            return $el->evaluate($complexity, array_merge($args, ['children_complexity' => $childrenComplexity]));
110 22
        };
111
    }
112
}
113