Passed
Push — master ( 3b20b8...2ad278 )
by Rafael
03:27
created

StringFilter::applyFilter()   C

Complexity

Conditions 17
Paths 13

Size

Total Lines 47
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 59.1306

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 47
ccs 18
cts 38
cp 0.4737
rs 5.2166
cc 17
nc 13
nop 4
crap 59.1306

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Query\Expr\Orx;
14
use Doctrine\ORM\QueryBuilder;
15
use Ynlo\GraphQLBundle\Filter\FilterContext;
16
use Ynlo\GraphQLBundle\Filter\FilterInterface;
17
use Ynlo\GraphQLBundle\Model\Filter\StringComparisonExpression;
18
use Ynlo\GraphQLBundle\Type\StringComparisonOperatorType;
19
20
/**
21
 * string filter to compare strings and filter by them
22
 */
23
class StringFilter implements FilterInterface
24
{
25
    /**
26
     * @inheritDoc
27
     */
28 7
    public function __invoke(FilterContext $context, QueryBuilder $qb, $condition)
29
    {
30 7
        if (!$condition instanceof StringComparisonExpression) {
31 1
            throw new \RuntimeException('Invalid filter condition');
32
        }
33
34 6
        if (!$context->getField() || !$context->getField()->getName()) {
35 1
            throw new \RuntimeException('There are not valid field related to this filter.');
36
        }
37
38 5
        $alias = $qb->getRootAliases()[0];
39 5
        $column = $context->getField()->getOriginName();
40 5
        if ($context->getField()->getOriginType() === 'ReflectionMethod') {
41
            $column = $context->getField()->getName();
42
        }
43
44 5
        $this->applyFilter($qb, $alias, $column, $condition);
45 5
    }
46
47
    /**
48
     * @param QueryBuilder               $qb
49
     * @param string                     $alias
50
     * @param string                     $column
51
     * @param StringComparisonExpression $condition
52
     */
53 5
    protected function applyFilter(QueryBuilder $qb, $alias, $column, StringComparisonExpression $condition): void
54
    {
55 5
        switch ($condition->getOp()) {
56 5
            case StringComparisonOperatorType::EQUAL:
57 1
                if ($condition->getValue()) {
58 1
                    $qb->andWhere("{$alias}.{$column} = '{$condition->getValue()}'");
59
                } elseif ($condition->getValues()) {
60
                    $orx = new Orx();
61
                    foreach ($condition->getValues() as $value) {
62
                        $orx->add("{$alias}.{$column} = '{$value}'");
63
                    }
64
                    $qb->andWhere($orx);
65
                }
66 1
                break;
67 4
            case StringComparisonOperatorType::CONTAINS:
68 2
                if ($condition->getValue()) {
69 2
                    $qb->andWhere("{$alias}.{$column} LIKE '%{$condition->getValue()}%'");
70
                } elseif ($condition->getValues()) {
71
                    $orx = new Orx();
72
                    foreach ($condition->getValues() as $value) {
73
                        $orx->add("{$alias}.{$column} LIKE '%{$value}%'");
74
                    }
75
                    $qb->andWhere($orx);
76
                }
77 2
                break;
78 2
            case StringComparisonOperatorType::STARTS_WITH:
79 1
                if ($condition->getValue()) {
80 1
                    $qb->andWhere("{$alias}.{$column} LIKE '{$condition->getValue()}%'");
81
                } elseif ($condition->getValues()) {
82
                    $orx = new Orx();
83
                    foreach ($condition->getValues() as $value) {
84
                        $orx->add("{$alias}.{$column} LIKE '{$value}%'");
85
                    }
86
                    $qb->andWhere($orx);
87
                }
88 1
                break;
89 1
            case StringComparisonOperatorType::ENDS_WITH:
90 1
                if ($condition->getValue()) {
91 1
                    $qb->andWhere("{$alias}.{$column} LIKE '%{$condition->getValue()}'");
92
                } elseif ($condition->getValues()) {
93
                    $orx = new Orx();
94
                    foreach ($condition->getValues() as $value) {
95
                        $orx->add("{$alias}.{$column} LIKE '%{$value}'");
96
                    }
97
                    $qb->andWhere($orx);
98
                }
99 1
                break;
100
        }
101 5
    }
102
}
103