Passed
Push — master ( 724c6f...9da6fd )
by Rafael
06:22
created

Nodes::__invoke()   C

Complexity

Conditions 10
Paths 29

Size

Total Lines 60
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 10.0822

Importance

Changes 0
Metric Value
dl 0
loc 60
ccs 29
cts 32
cp 0.9063
rs 6.5333
c 0
b 0
f 0
cc 10
eloc 31
nc 29
nop 1
crap 10.0822

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\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