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

Nodes::__invoke()   C

Complexity

Conditions 11
Paths 24

Size

Total Lines 55
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 11.1097

Importance

Changes 0
Metric Value
cc 11
eloc 31
nc 24
nop 1
dl 0
loc 55
ccs 28
cts 31
cp 0.9032
crap 11.1097
rs 6.6153
c 0
b 0
f 0

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 Ynlo\GraphQLBundle\Annotation as GraphQL;
15
use Ynlo\GraphQLBundle\Definition\ArgumentDefinition;
16
use Ynlo\GraphQLBundle\Model\ID;
17
use Ynlo\GraphQLBundle\Resolver\AbstractResolver;
18
19
/**
20
 * @GraphQL\Query(list=true)
21
 * @GraphQL\Argument(name="ids", type="[ID!]!")
22
 */
23
class Nodes extends AbstractResolver
24
{
25
    /**
26
     * @param ID[]|mixed[] $ids
27
     *
28
     * @return mixed
29
     */
30 2
    public function __invoke($ids)
31
    {
32 2
        if (empty($ids)) {
33
            return [];
34
        }
35
36 2
        $types = [];
37 2
        $expectedResultsOrder = [];
38
39 2
        if (current($ids) instanceof ID) {
40 1
            $searchField = 'id';
41 1
            foreach ($ids as $id) {
42 1
                $types[$id->getNodeType()][] = $id->getDatabaseId();
43 1
                $expectedResultsOrder[md5($id->getNodeType().$id->getDatabaseId())] = null;
44
            }
45
        } else {
46
            //when use a different field to fetch,
47
            //@see QueryGet::fetchBy
48 1
            $type = $this->getContext()->getDefinition()->getType();
49 1
            $objectDefinition = $this->getContext()->getDefinitionManager()->getType($type);
50
51
            /** @var ArgumentDefinition $arg */
52 1
            $arg = array_values($this->getContext()->getDefinition()->getArguments())[0];
53
54 1
            $field = null;
55 1
            if ($objectDefinition->hasField($arg->getName())) {
56
                $field = $objectDefinition->getField($arg->getName());
57 1
            } elseif ($objectDefinition->hasField(Inflector::singularize($arg->getName()))) { //by convention, singularize
58 1
                $field = $objectDefinition->getField(Inflector::singularize($arg->getName()));
59
            }
60
61 1
            if (null === $field) {
62
                throw new \RuntimeException(sprintf('Can`t resolve the field `%s` inside type `%s`', $arg->getName(), $type));
63
            }
64
65 1
            $searchField = $field->getOriginName();
66 1
            $types[$type] = $ids;
67 1
            foreach ($ids as $identifier) {
68 1
                $expectedResultsOrder[md5($type.$identifier)] = null;
69
            }
70
        }
71
72 2
        foreach ($types as $type => $searchValues) {
73 2
            if ($this->getContext()->getDefinitionManager()->hasType($type)) {
74 2
                foreach ($searchValues as $searchValue) {
75
                    //TODO: improve this to find all nodes in the same Repo with only one query, NOTE: the order and empty results are very IMPORTANT!
76
                    //The list of given id should match with the list of results including non-found nodes
77 2
                    $entity = $this->getContext()->getDefinitionManager()->getClassForType($type);
78 2
                    $result = $this->getManager()->getRepository($entity)->findOneBy([$searchField => $searchValue]);
79 2
                    $expectedResultsOrder[md5($type.$searchValue)] = $result;
80
                }
81
            }
82
        }
83
84 2
        return array_values($expectedResultsOrder);
85
    }
86
}
87