AllNodes::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 15
c 0
b 0
f 0
ccs 0
cts 9
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
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\Model\OrderBy;
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
        //keep orderBy for BC
55
        $orderBy = array_merge($args['orderBy'] ?? [], $args['order'] ?? []);
56
57
        $this->initialize();
58
        $qb = $this->createQuery();
59
        $this->applyOrderBy($qb, $orderBy);
0 ignored issues
show
Deprecated Code introduced by
The function Ynlo\GraphQLBundle\Query...llNodes::applyOrderBy() has been deprecated: only All nodes with pagination should have orderBy ( Ignorable by Annotation )

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

59
        /** @scrutinizer ignore-deprecated */ $this->applyOrderBy($qb, $orderBy);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
60
61
        $this->configureQuery($qb);
62
        foreach ($this->extensions as $extension) {
63
            $extension->configureQuery($qb, $this, $this->context);
64
        }
65
66
        return $qb->getQuery()->getResult();
67
    }
68
69
    /**
70
     * initialize
71
     */
72
    public function initialize()
73
    {
74
        $this->queryDefinition = $this->context->getDefinition();
75
        $this->entity = $this->context->getNode()->getClass();
76
        $this->objectDefinition = $this->context->getNode();
77
    }
78
79
    /**
80
     * @param QueryBuilder $qb
81
     */
82
    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

82
    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...
83
    {
84
        //implements on childs to customize the query
85
    }
86
87
    /**
88
     * @param QueryBuilder    $qb
89
     * @param array|OrderBy[] $orderBy
90
     *
91
     * @throws Error
92
     *
93
     * @deprecated only All nodes with pagination should have orderBy
94
     */
95
    protected function applyOrderBy(QueryBuilder $qb, $orderBy)
96
    {
97
        foreach ($orderBy as $order) {
98
            $column = $order->getField();
99
            if (strpos($column, '.') !== false) {
100
                [$relation, $column] = explode('.', $column);
101
                $childAlias = 'orderBy'.ucfirst($relation);
102
                $qb->leftJoin("{$this->queryAlias}.{$relation}", $childAlias);
103
                $qb->addOrderBy("$childAlias.$column", $order->getDirection());
104
            } else {
105
                if ($this->objectDefinition && $this->objectDefinition->hasField($column)) {
106
                    $column = $this->objectDefinition->getField($column)->getOriginName() ?: $column;
107
                }
108
                $qb->addOrderBy("{$this->queryAlias}.$column", $order->getDirection());
109
            }
110
        }
111
    }
112
113
    /**
114
     * @return QueryBuilder
115
     */
116
    protected function createQuery(): QueryBuilder
117
    {
118
        return $this->getManager()
119
                    ->getRepository($this->entity)
120
                    ->createQueryBuilder($this->queryAlias);
0 ignored issues
show
Bug introduced by
The method createQueryBuilder() does not exist on Doctrine\Persistence\ObjectRepository. It seems like you code against a sub-type of Doctrine\Persistence\ObjectRepository such as Doctrine\ORM\EntityRepository. ( Ignorable by Annotation )

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

120
                    ->/** @scrutinizer ignore-call */ createQueryBuilder($this->queryAlias);
Loading history...
121
    }
122
}
123