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

NumberFilter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
dl 0
loc 40
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 35 11
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\FloatComparisonExpression;
17
use Ynlo\GraphQLBundle\Type\NumberComparisonOperatorType;
18
19
/**
20
 * Filter to compare numeric values
21
 */
22
class NumberFilter implements FilterInterface
23
{
24
    /**
25
     * @inheritDoc
26
     */
27 9
    public function __invoke(FilterContext $context, QueryBuilder $qb, $condition)
28
    {
29 9
        if (!$condition instanceof FloatComparisonExpression) {
30 1
            throw new \RuntimeException('Invalid filter condition');
31
        }
32
33 8
        if (!$context->getField() || !$context->getField()->getName()) {
34 1
            throw new \RuntimeException('There are not valid field related to this filter.');
35
        }
36
37 7
        $alias = $qb->getRootAliases()[0];
38 7
        $column = $context->getField()->getOriginName();
39 7
        switch ($condition->getOp()) {
40 7
            case NumberComparisonOperatorType::EQ:
41 1
                $qb->andWhere("{$alias}.{$column} = {$condition->getValue()}");
42 1
                break;
43 6
            case NumberComparisonOperatorType::NEQ:
44 1
                $qb->andWhere("{$alias}.{$column} <> {$condition->getValue()}");
45 1
                break;
46 5
            case NumberComparisonOperatorType::GT:
47 1
                $qb->andWhere("{$alias}.{$column} > {$condition->getValue()}");
48 1
                break;
49 4
            case NumberComparisonOperatorType::GTE:
50 1
                $qb->andWhere("{$alias}.{$column} >= {$condition->getValue()}");
51 1
                break;
52 3
            case NumberComparisonOperatorType::LT:
53 1
                $qb->andWhere("{$alias}.{$column} < {$condition->getValue()}");
54 1
                break;
55 2
            case NumberComparisonOperatorType::LTE:
56 1
                $qb->andWhere("{$alias}.{$column} <= {$condition->getValue()}");
57 1
                break;
58 1
            case NumberComparisonOperatorType::BETWEEN:
59 1
                $max = $condition->getMaxValue() ?? $condition->getValue();
60 1
                $qb->andWhere("{$alias}.{$column} BETWEEN {$condition->getValue()} AND {$max}");
61 1
                break;
62
        }
63 7
    }
64
}
65