Passed
Push — master ( 5cd32c...1a1aa9 )
by Rafael
05:46
created

Nodes   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 90.63%

Importance

Changes 0
Metric Value
wmc 10
dl 0
loc 69
ccs 29
cts 32
cp 0.9063
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C __invoke() 0 60 10
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\Query\Node;
12
13
use Doctrine\Common\Inflector\Inflector;
14
use Doctrine\ORM\EntityRepository;
15
use Ynlo\GraphQLBundle\Annotation as GraphQL;
16
use Ynlo\GraphQLBundle\Definition\ArgumentDefinition;
17
use Ynlo\GraphQLBundle\Definition\FieldsAwareDefinitionInterface;
18
use Ynlo\GraphQLBundle\Model\ID;
19
use Ynlo\GraphQLBundle\Model\NodeInterface;
20
use Ynlo\GraphQLBundle\Resolver\AbstractResolver;
21
22
/**
23
 * @GraphQL\Query(type="[]")
24
 * @GraphQL\Argument(name="ids", type="[ID!]!", internalName="nodes")
25
 */
26
class Nodes extends AbstractResolver
27
{
28
    protected $fetchBy = 'id';
29
30
    /**
31
     * @param ID[]|mixed[] $nodes
32
     *
33
     * @return mixed
34
     */
35 3
    public function __invoke($nodes)
36
    {
37 3
        if (empty($nodes)) {
38
            return [];
39
        }
40
41 3
        $types = [];
42 3
        $expectedResultsOrder = [];
43
44 3
        if (current($nodes) instanceof NodeInterface) {
45 2
            return $nodes;
46
        }
47
48
        //when use a different field to fetch,
49
        //@see QueryGet::fetchBy
50 1
        $type = $this->getContext()->getDefinition()->getType();
51
52
        /** @var FieldsAwareDefinitionInterface $objectDefinition */
53 1
        $objectDefinition = $this->getContext()->getEndpoint()->getType($type);
54
55
        /** @var ArgumentDefinition $arg */
56 1
        $arg = array_values($this->getContext()->getDefinition()->getArguments())[0];
57
58 1
        $field = null;
59 1
        if ($objectDefinition->hasField($arg->getName())) {
60
            $field = $objectDefinition->getField($arg->getName());
61 1
        } elseif ($objectDefinition->hasField(Inflector::singularize($arg->getName()))) { //by convention, singularize
62 1
            $field = $objectDefinition->getField(Inflector::singularize($arg->getName()));
63
        }
64
65 1
        if (null === $field) {
66
            throw new \RuntimeException(sprintf('Can`t resolve the field `%s` inside type `%s`', $arg->getName(), $type));
67
        }
68
69 1
        $types[$type] = $nodes;
70 1
        foreach ($nodes as $identifier) {
71 1
            $expectedResultsOrder[md5($type.$identifier)] = null;
72
        }
73
74 1
        foreach ($types as $type => $searchValues) {
75 1
            if ($this->getContext()->getEndpoint()->hasType($type)) {
76 1
                $entity = $this->getContext()->getEndpoint()->getClassForType($type);
77
78
                /** @var EntityRepository $repo */
79 1
                $repo = $this->getManager()->getRepository($entity);
80
81 1
                $findBy = sprintf('o.%s', $this->fetchBy);
82
83 1
                $qb = $repo->createQueryBuilder('o', $findBy);
84 1
                $entities = $qb->where($qb->expr()->in($findBy, $searchValues))
85 1
                               ->getQuery()
86 1
                               ->getResult();
87
88 1
                foreach ($entities as $searchValue => $entity) {
89 1
                    $expectedResultsOrder[md5($type.$searchValue)] = $entity;
90
                }
91
            }
92
        }
93
94 1
        return array_values($expectedResultsOrder);
95
    }
96
}
97