Completed
Pull Request — master (#5)
by Yonel Ceruto
10:19 queued 23s
created

GraphQLBuilder::buildComplexityFn()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 2
cts 2
cp 1
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 10
nc 4
nop 1
crap 5
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\Type\Registry\TypeRegistry;
17
18
class GraphQLBuilder
19
{
20
    public static function buildArguments(ArgumentAwareInterface $argumentAware): array
21
    {
22
        $args = [];
23
        foreach ($argumentAware->getArguments() as $argDefinition) {
24
            $arg = [];
25
            $arg['description'] = $argDefinition->getDescription();
26
            $argType = TypeRegistry::get($argDefinition->getType());
27 1
28
            if ($argDefinition->isList()) {
29 1
                if ($argDefinition->isNonNullList()) {
30 1
                    $argType = Type::nonNull($argType);
31 1
                }
32 1
                $argType = Type::listOf($argType);
33 1
            }
34
35 1
            if ($argDefinition->isNonNull()) {
36 1
                $argType = Type::nonNull($argType);
37 1
            }
38
39 1
            $arg['type'] = $argType;
40
            if ($argDefinition->getDefaultValue()) {
41
                $arg['defaultValue'] = $argDefinition->getDefaultValue();
42 1
            }
43 1
            $args[$argDefinition->getName()] = $arg;
44
        }
45
46 1
        return $args;
47 1
    }
48
49
    public static function buildComplexityFn(?string $complexity): ?callable
50 1
    {
51
        if (null === $complexity) {
52
            return null;
53 1
        }
54
55
        if (is_numeric($complexity)) {
56
            return function ($childrenComplexity) use ($complexity) {
57
                return $childrenComplexity + $complexity;
58
            };
59
        }
60
61
        // support only static string callable func
62
        if (\is_string($complexity) && \is_callable($complexity)) {
63
            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...
64
        }
65
66
        $el = new ExpressionLanguage();
67
68
        return function ($childrenComplexity, $args) use ($el, $complexity) {
69
            return $el->evaluate($complexity, array_merge($args, ['children_complexity' => $childrenComplexity]));
70
        };
71
    }
72
}
73