Completed
Push — master ( f0c7c5...1bf4f0 )
by Rafael
08:34
created

Nodes::__invoke()   C

Complexity

Conditions 11
Paths 24

Size

Total Lines 63
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 11.083

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 63
ccs 31
cts 34
cp 0.9118
rs 6.1137
cc 11
eloc 34
nc 24
nop 1
crap 11.083

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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