Completed
Push — master ( 2975e7...46c3ee )
by Rafael
07:49
created

EnumFilter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 24
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 19 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\Filter\Common;
12
13
use Doctrine\ORM\QueryBuilder;
14
use Ynlo\GraphQLBundle\Filter\FilterContext;
15
use Ynlo\GraphQLBundle\Filter\FilterInterface;
16
use Ynlo\GraphQLBundle\Model\Filter\EnumComparisonExpression;
17
use Ynlo\GraphQLBundle\Type\NodeComparisonOperatorType;
18
19
class EnumFilter implements FilterInterface
20
{
21
    /**
22
     * @inheritDoc
23
     */
24 4
    public function __invoke(FilterContext $context, QueryBuilder $qb, $condition)
25
    {
26 4
        if (!$condition instanceof EnumComparisonExpression) {
27 1
            throw new \RuntimeException('Invalid filter condition');
28
        }
29
30 3
        if (!$context->getField() || !$context->getField()->getName()) {
31 1
            throw new \RuntimeException('There are not valid field related to this filter.');
32
        }
33
34 2
        $alias = $qb->getRootAliases()[0];
35 2
        $column = $context->getField()->getOriginName();
36 2
        switch ($condition->getOp()) {
37 2
            case NodeComparisonOperatorType::IN:
38 1
                $qb->andWhere($qb->expr()->in("{$alias}.{$column}", $condition->getValues()));
39 1
                break;
40 1
            case NodeComparisonOperatorType::NIN:
41 1
                $qb->andWhere($qb->expr()->notIn("{$alias}.{$column}", $condition->getValues()));
42 1
                break;
43
        }
44 2
    }
45
}
46