Passed
Push — master ( c39fb6...9e9a0d )
by Rafael
04:42
created

AllNodes::applyLimits()   C

Complexity

Conditions 12
Paths 15

Size

Total Lines 41
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 76.8991

Importance

Changes 0
Metric Value
cc 12
eloc 30
nc 15
nop 3
dl 0
loc 41
ccs 7
cts 30
cp 0.2333
crap 76.8991
rs 5.1612
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A AllNodes::createQuery() 0 5 1

How to fix   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\ORM\QueryBuilder;
14
use GraphQL\Error\Error;
15
use Ynlo\GraphQLBundle\Definition\ObjectDefinition;
16
use Ynlo\GraphQLBundle\Definition\QueryDefinition;
17
use Ynlo\GraphQLBundle\Extension\ExtensionManager;
18
use Ynlo\GraphQLBundle\Resolver\AbstractResolver;
19
20
/**
21
 * Resolver to fetch a simple list of nodes without pagination, edges etc
22
 */
23
class AllNodes extends AbstractResolver
24
{
25
    /**
26
     * @var string
27
     */
28
    protected $queryAlias = 'o';
29
30
    /**
31
     * @var string
32
     */
33
    protected $entity;
34
35
    /**
36
     * @var QueryDefinition
37
     */
38
    protected $queryDefinition;
39
40
    /**
41
     * @var ObjectDefinition
42
     */
43
    protected $objectDefinition;
44
45
    /**
46
     * @param array[] $args
47
     *
48
     * @return mixed
49
     *
50
     * @throws Error
51
     */
52
    public function __invoke($args = [])
53
    {
54
        $orderBy = $args['orderBy'] ?? [];
55
56
        $this->initialize();
57
        $qb = $this->createQuery();
58
        $this->applyOrderBy($qb, $orderBy);
59
60
        $this->configureQuery($qb);
61
        foreach ($this->container->get(ExtensionManager::class)->getExtensions() as $extension) {
62
            $extension->configureQuery($qb, $this, $this->context);
63
        }
64
65
        return $qb->getQuery()->getResult();
66
    }
67
68
    /**
69
     * initialize
70
     */
71 10
    public function initialize()
72
    {
73 10
        $this->queryDefinition = $this->context->getDefinition();
74 10
        $this->entity = $this->context->getNodeDefinition()->getClass();
75 10
        $this->objectDefinition = $this->context->getNodeDefinition();
76 10
    }
77
78
    /**
79
     * @param QueryBuilder $qb
80
     */
81 9
    public function configureQuery(QueryBuilder $qb)
0 ignored issues
show
Unused Code introduced by
The parameter $qb is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

81
    public function configureQuery(/** @scrutinizer ignore-unused */ QueryBuilder $qb)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
    {
83
        //implements on childs to customize the query
84 9
    }
85
86
    /**
87
     * @param QueryBuilder $qb
88
     * @param array        $orderBy
89
     *
90
     * @throws Error
91
     */
92 10
    protected function applyOrderBy(QueryBuilder $qb, $orderBy)
93
    {
94 10
        $refClass = new \ReflectionClass($this->entity);
95
        //TODO: allow sort using nested entities, e.g. profile.username
96 10
        foreach ($orderBy as $order) {
97 7
            $order->getField();
98 7
            if ($this->objectDefinition->hasField($order->getField())) {
99 7
                $fieldDefinition = $this->objectDefinition->getField($order->getField());
100 7
                if ($fieldDefinition->getOriginType() === \ReflectionProperty::class) {
101 7
                    if ($refClass->hasProperty($fieldDefinition->getOriginName())) {
102 7
                        $qb->addOrderBy($this->queryAlias.'.'.$fieldDefinition->getOriginName(), $order->getDirection());
103
104 7
                        continue;
105
                    }
106
                }
107
            }
108
109
            throw new Error(sprintf('The field "%s" its not valid to order in "%s"', $order->getField(), $this->queryDefinition->getName()));
110
        }
111 10
    }
112
113
    /**
114
     * @return QueryBuilder
115
     */
116 10
    protected function createQuery(): QueryBuilder
117
    {
118 10
        return $this->getManager()
119 10
                    ->getRepository($this->entity)
120 10
                    ->createQueryBuilder($this->queryAlias);
121
    }
122
}
123