Passed
Push — master ( e52344...df819b )
by Rafael
09:21
created

ObjectDefinitionType::resolveFields()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 26
Code Lines 15

Duplication

Lines 26
Ratio 100 %

Code Coverage

Tests 15
CRAP Score 5.0061

Importance

Changes 0
Metric Value
dl 26
loc 26
c 0
b 0
f 0
ccs 15
cts 16
cp 0.9375
rs 8.439
cc 5
eloc 15
nc 7
nop 0
crap 5.0061
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\Type\Definition;
12
13
use GraphQL\Type\Definition\ObjectType;
14
use GraphQL\Type\Definition\ResolveInfo;
15
use GraphQL\Type\Definition\Type;
16
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
17
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
18
use Ynlo\GraphQLBundle\Definition\ObjectDefinition;
19
use Ynlo\GraphQLBundle\Resolver\ObjectFieldResolver;
20
use Ynlo\GraphQLBundle\Type\Registry\TypeRegistry;
21
use Ynlo\GraphQLBundle\Util\GraphQLBuilder;
22
23
/**
24
 * Class ObjectDefinitionType
25
 */
26
class ObjectDefinitionType extends ObjectType implements
27
    ContainerAwareInterface,
28
    EndpointAwareInterface
29
{
30
    use ContainerAwareTrait;
31
    use EndpointAwareTrait;
32
33
    /**
34
     * @var ObjectDefinition
35
     */
36
    protected $definition;
37
38
    /**
39
     * @param ObjectDefinition $definition
40
     */
41 21
    public function __construct(ObjectDefinition $definition)
42
    {
43 1
        $this->definition = $definition;
44
45 1
        parent::__construct(
46
            [
47 1
                'name' => $definition->getName(),
48 1
                'description' => $definition->getDescription(),
49 1
                'fields' => function () {
50 1
                    return $this->resolveFields();
51 1
                },
52 1
                'interfaces' => function () {
53 1
                    return $this->resolveInterfaces();
54 1
                },
55 21 View Code Duplication
                'resolveField' => function ($root, array $args, $context, ResolveInfo $resolveInfo) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56 21
                    $resolver = new ObjectFieldResolver($this->container, $this->endpoint, $this->definition);
57
58 21
                    return $resolver($root, $args, $context, $resolveInfo);
59 1
                },
60 21
                'isTypeOf' => function ($value, $context, ResolveInfo $info) {
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

60
                'isTypeOf' => function ($value, /** @scrutinizer ignore-unused */ $context, ResolveInfo $info) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

60
                'isTypeOf' => function (/** @scrutinizer ignore-unused */ $value, $context, ResolveInfo $info) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $info is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

60
                'isTypeOf' => function ($value, $context, /** @scrutinizer ignore-unused */ ResolveInfo $info) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
                    //TODO: implement this
62 21
                },
63
            ]
64
        );
65 1
    }
66
67
    /**
68
     * @return array
69
     */
70 1 View Code Duplication
    private function resolveFields()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72 1
        $fields = [];
73 1
        foreach ($this->definition->getFields() as $fieldDefinition) {
74 1
            $type = TypeRegistry::get($fieldDefinition->getType());
75
76 1
            if ($fieldDefinition->isList()) {
77 1
                if ($fieldDefinition->isNonNullList()) {
78
                    $type = Type::nonNull($type);
79
                }
80 1
                $type = Type::listOf($type);
81
            }
82
83 1
            if ($fieldDefinition->isNonNull()) {
84 1
                $type = Type::nonNull($type);
85
            }
86
87 1
            $fields[$fieldDefinition->getName()] = [
88 1
                'type' => $type,
89 1
                'description' => $fieldDefinition->getDescription(),
90 1
                'deprecationReason' => $fieldDefinition->getDeprecationReason(),
91 1
                'args' => GraphQLBuilder::buildArguments($fieldDefinition),
92
            ];
93
        }
94
95 1
        return $fields;
96
    }
97
98
    /**
99
     * @return array
100
     */
101 1
    private function resolveInterfaces()
102
    {
103 1
        $interfaces = [];
104 1
        foreach ($this->definition->getInterfaces() as $interface) {
105 1
            $interfaces[] = TypeRegistry::get($interface);
106
        }
107
108 1
        return $interfaces;
109
    }
110
}
111