Completed
Push — master ( c341b6...280bd5 )
by Rafael
04:24
created

NumberFilter   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 96.43%

Importance

Changes 0
Metric Value
wmc 13
eloc 33
dl 0
loc 55
ccs 27
cts 28
cp 0.9643
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 17 5
B applyFilter() 0 25 8
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
        if ($context->getField()->getOriginType() === 'ReflectionMethod') {
40
            $column = $context->getField()->getName();
41
        }
42
43 7
        $this->applyFilter($qb, $alias, $column, $condition);
44 7
    }
45
46
    /**
47
     * @param QueryBuilder              $qb
48
     * @param string                    $alias
49
     * @param string                    $column
50
     * @param FloatComparisonExpression $condition
51
     */
52 7
    protected function applyFilter(QueryBuilder $qb, $alias, $column, FloatComparisonExpression $condition): void
53
    {
54 7
        switch ($condition->getOp()) {
55
            case NumberComparisonOperatorType::EQ:
56 1
                $qb->andWhere("{$alias}.{$column} = {$condition->getValue()}");
57 1
                break;
58
            case NumberComparisonOperatorType::NEQ:
59 1
                $qb->andWhere("{$alias}.{$column} <> {$condition->getValue()}");
60 1
                break;
61
            case NumberComparisonOperatorType::GT:
62 1
                $qb->andWhere("{$alias}.{$column} > {$condition->getValue()}");
63 1
                break;
64
            case NumberComparisonOperatorType::GTE:
65 1
                $qb->andWhere("{$alias}.{$column} >= {$condition->getValue()}");
66 1
                break;
67
            case NumberComparisonOperatorType::LT:
68 1
                $qb->andWhere("{$alias}.{$column} < {$condition->getValue()}");
69 1
                break;
70
            case NumberComparisonOperatorType::LTE:
71 1
                $qb->andWhere("{$alias}.{$column} <= {$condition->getValue()}");
72 1
                break;
73
            case NumberComparisonOperatorType::BETWEEN:
74 1
                $max = $condition->getMaxValue() ?? $condition->getValue();
75 1
                $qb->andWhere("{$alias}.{$column} BETWEEN {$condition->getValue()} AND {$max}");
76 1
                break;
77
        }
78 7
    }
79
}
80