Completed
Push — master ( becf03...491f37 )
by Rafael
08:53
created

OrderByRelatedField::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 4
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\OrderBy\Common;
12
13
use Doctrine\ORM\QueryBuilder;
14
use Ynlo\GraphQLBundle\Model\OrderBy;
15
use Ynlo\GraphQLBundle\OrderBy\OrderByContext;
16
use Ynlo\GraphQLBundle\OrderBy\OrderByInterface;
17
18
class OrderByRelatedField implements OrderByInterface
19
{
20
    /**
21
     * @inheritDoc
22
     */
23
    public function __invoke(OrderByContext $context, QueryBuilder $qb, $alias, OrderBy $orderBy)
24
    {
25
        $column = $orderBy->getField();
26
        [$relation, $column] = explode('.', $column);
27
        $childAlias = 'orderBy'.ucfirst($relation);
28
        $qb->leftJoin("{$alias}.{$relation}", $childAlias);
29
        $qb->addOrderBy("$childAlias.$column", $orderBy->getDirection());
30
    }
31
}
32