Completed
Push — master ( cbfed7...c39fb6 )
by Rafael
05:06
created

ObjectFieldResolver   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 90.32%

Importance

Changes 0
Metric Value
dl 0
loc 78
ccs 28
cts 31
cp 0.9032
rs 10
c 0
b 0
f 0
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C __invoke() 0 46 9
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\Resolver;
12
13
use Doctrine\Common\Collections\Collection;
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 Symfony\Component\DependencyInjection\ContainerInterface;
19
use Symfony\Component\PropertyAccess\PropertyAccessor;
20
use Ynlo\GraphQLBundle\Definition\FieldsAwareDefinitionInterface;
21
use Ynlo\GraphQLBundle\Definition\QueryDefinition;
22
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionManager;
23
use Ynlo\GraphQLBundle\Model\ID;
24
use Ynlo\GraphQLBundle\Type\DefinitionManagerAwareInterface;
25
use Ynlo\GraphQLBundle\Type\DefinitionManagerAwareTrait;
26
27
/**
28
 * Default resolver for all object fields
29
 */
30
class ObjectFieldResolver implements ContainerAwareInterface, DefinitionManagerAwareInterface
31
{
32
    use ContainerAwareTrait;
33
    use DefinitionManagerAwareTrait;
34
35
    /**
36
     * @var FieldsAwareDefinitionInterface
37
     */
38
    protected $definition;
39
40
    /**
41
     * ObjectFieldResolver constructor.
42
     *
43
     * @param ContainerInterface             $container
44
     * @param DefinitionManager              $definitionManager
45
     * @param FieldsAwareDefinitionInterface $definition
46
     */
47 14
    public function __construct(ContainerInterface $container, DefinitionManager $definitionManager, FieldsAwareDefinitionInterface $definition)
48
    {
49 14
        $this->definition = $definition;
50 14
        $this->container = $container;
51 14
        $this->manager = $definitionManager;
52 14
    }
53
54
    /**
55
     * @param mixed       $root
56
     * @param array       $args
57
     * @param mixed       $context
58
     * @param ResolveInfo $info
59
     *
60
     * @return mixed|null|string
61
     */
62 14
    public function __invoke($root, array $args, $context, ResolveInfo $info)
63
    {
64 14
        $value = null;
65 14
        $fieldDefinition = $this->definition->getField($info->fieldName);
66
67
        //when use external resolver or use a object method with arguments
68 14
        if ($fieldDefinition->getResolver() || $fieldDefinition->getArguments()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fieldDefinition->getResolver() of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
69 1
            $queryDefinition = new QueryDefinition();
70 1
            $queryDefinition->setName($fieldDefinition->getName());
71 1
            $queryDefinition->setType($fieldDefinition->getType());
72 1
            $queryDefinition->setArguments($fieldDefinition->getArguments());
73 1
            $queryDefinition->setList($fieldDefinition->isList());
74 1
            $queryDefinition->setMetas($fieldDefinition->getMetas());
75
76 1
            if (!$fieldDefinition->getResolver()) {
77
                if ($fieldDefinition->getOriginType() === \ReflectionMethod::class) {
78
                    $queryDefinition->setResolver($fieldDefinition->getOriginName());
79
                }
80
            } else {
81 1
                $queryDefinition->setResolver($fieldDefinition->getResolver());
82
            }
83
84 1
            $resolver = new ResolverExecutor($this->container, $this->manager, $queryDefinition);
85 1
            $value = $resolver($root, $args, $context, $info);
86
        } else {
87 14
            $accessor = new PropertyAccessor(true);
88 14
            $originName = $fieldDefinition->getOriginName();
89 14
            $value = $accessor->getValue($root, $originName);
90
        }
91
92 14
        if (null !== $value && Type::ID === $fieldDefinition->getType()) {
93
            //ID are formed with base64 representation of the Types and real database ID
94
            //in order to create a unique and global identifier for each resource
95
            //@see https://facebook.github.io/relay/docs/graphql-object-identification.html
96 12
            if ($value instanceof ID) {
97 2
                $value = (string) $value;
98
            } else {
99 11
                $value = (string) new ID($this->definition->getName(), $value);
100
            }
101
        }
102
103 14
        if ($value instanceof Collection) {
104
            $value = $value->toArray();
105
        }
106
107 14
        return $value;
108
    }
109
}
110