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