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

StringFilter::applyFilter()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.5222
c 0
b 0
f 0
cc 5
nc 5
nop 4
crap 5
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\StringComparisonExpression;
17
use Ynlo\GraphQLBundle\Type\StringComparisonOperatorType;
18
19
/**
20
 * string filter to compare strings and filter by them
21
 */
22
class StringFilter implements FilterInterface
23
{
24
    /**
25
     * @inheritDoc
26
     */
27 7
    public function __invoke(FilterContext $context, QueryBuilder $qb, $condition)
28
    {
29 7
        if (!$condition instanceof StringComparisonExpression) {
30 1
            throw new \RuntimeException('Invalid filter condition');
31
        }
32
33 6
        if (!$context->getField() || !$context->getField()->getName()) {
34 1
            throw new \RuntimeException('There are not valid field related to this filter.');
35
        }
36
37 5
        $alias = $qb->getRootAliases()[0];
38 5
        $column = $context->getField()->getOriginName();
39 5
        if ($context->getField()->getOriginType() === 'ReflectionMethod') {
40
            $column = $context->getField()->getName();
41
        }
42
43 5
        $this->applyFilter($qb, $alias, $column, $condition);
44 5
    }
45
46
    /**
47
     * @param QueryBuilder               $qb
48
     * @param string                     $alias
49
     * @param string                     $column
50
     * @param StringComparisonExpression $condition
51
     */
52 5
    protected function applyFilter(QueryBuilder $qb, $alias, $column, StringComparisonExpression $condition): void
53
    {
54 5
        switch ($condition->getOp()) {
55
            case StringComparisonOperatorType::EQUAL:
56 2
                $qb->andWhere("{$alias}.{$column} = '{$condition->getValue()}'");
57 2
                break;
58
            case StringComparisonOperatorType::CONTAINS:
59 1
                $qb->andWhere("{$alias}.{$column} LIKE '%{$condition->getValue()}%'");
60 1
                break;
61
            case StringComparisonOperatorType::STARTS_WITH:
62 1
                $qb->andWhere("{$alias}.{$column} LIKE '{$condition->getValue()}%'");
63 1
                break;
64
            case StringComparisonOperatorType::ENDS_WITH:
65 1
                $qb->andWhere("{$alias}.{$column} LIKE '%{$condition->getValue()}'");
66 1
                break;
67
        }
68 5
    }
69
}
70