Passed
Push — master ( 4525d7...46c26b )
by Siim
13:08
created

FilterQueryBuilder   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 55
dl 0
loc 151
rs 10
c 0
b 0
f 0
wmc 23

9 Methods

Rating   Name   Duplication   Size   Complexity  
A addEqualExpression() 0 5 1
A convertStringToArray() 0 13 4
A addInExpression() 0 8 2
B buildQuery() 0 32 10
A addLikeExpression() 0 5 1
A addNotInExpression() 0 8 2
A addNotEqualExpression() 0 5 1
A addIsNotNullExpression() 0 3 1
A addIsNullExpression() 0 3 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: siim
5
 * Date: 28.01.19
6
 * Time: 19:30
7
 */
8
9
namespace Sf4\Api\Dto\Filter;
10
11
use Doctrine\ORM\QueryBuilder;
12
13
class FilterQueryBuilder
14
{
15
    /**
16
     * @param QueryBuilder $queryBuilder
17
     * @param FilterItemInterface $filterItem
18
     * @param string $fieldName
19
     */
20
    public static function buildQuery(QueryBuilder $queryBuilder, FilterItemInterface $filterItem, string $fieldName)
21
    {
22
        if(empty($filterItem->getValue())) {
23
            return ;
24
        }
25
        switch ($filterItem->getType()) {
26
            case FilterItemInterface::TYPE_LIKE:
27
                $expression = static::addLikeExpression($queryBuilder, $filterItem, $fieldName);
28
                break;
29
            case FilterItemInterface::TYPE_EQUAL:
30
                $expression = static::addEqualExpression($queryBuilder, $filterItem, $fieldName);
31
                break;
32
            case FilterItemInterface::TYPE_NOT_EQUAL:
33
                $expression = static::addNotEqualExpression($queryBuilder, $filterItem, $fieldName);
34
                break;
35
            case FilterItemInterface::TYPE_IN:
36
                $expression = static::addInExpression($queryBuilder, $filterItem, $fieldName);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $expression is correct as static::addInExpression(...filterItem, $fieldName) targeting Sf4\Api\Dto\Filter\Filte...lder::addInExpression() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
37
                break;
38
            case FilterItemInterface::TYPE_NOT_IN:
39
                $expression = static::addNotInExpression($queryBuilder, $filterItem, $fieldName);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $expression is correct as static::addNotInExpressi...filterItem, $fieldName) targeting Sf4\Api\Dto\Filter\Filte...r::addNotInExpression() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
40
                break;
41
            case FilterItemInterface::TYPE_IS_NULL:
42
                $expression = static::addIsNullExpression($queryBuilder, $fieldName);
43
                break;
44
            case FilterItemInterface::TYPE_NOT_NULL:
45
                $expression = static::addIsNotNullExpression($queryBuilder, $fieldName);
46
                break;
47
            default: $expression = null;
48
        }
49
        if($expression) {
50
            $queryBuilder->andWhere(
51
                $expression
52
            );
53
        }
54
    }
55
56
    /**
57
     * @param QueryBuilder $queryBuilder
58
     * @param FilterItemInterface $filterItem
59
     * @param string $fieldName
60
     * @return \Doctrine\ORM\Query\Expr\Comparison
61
     */
62
    protected static function addLikeExpression(QueryBuilder $queryBuilder, FilterItemInterface $filterItem, string $fieldName)
63
    {
64
        $paramKey = uniqid(':like_');
65
        $queryBuilder->setParameter($paramKey, $filterItem->getValue());
66
        return $queryBuilder->expr()->like($fieldName, $paramKey);
67
    }
68
69
    /**
70
     * @param QueryBuilder $queryBuilder
71
     * @param FilterItemInterface $filterItem
72
     * @param string $fieldName
73
     * @return \Doctrine\ORM\Query\Expr\Comparison
74
     */
75
    protected static function addEqualExpression(QueryBuilder $queryBuilder, FilterItemInterface $filterItem, string $fieldName)
76
    {
77
        $paramKey = uniqid(':eq_');
78
        $queryBuilder->setParameter($paramKey, $filterItem->getValue());
79
        return $queryBuilder->expr()->eq($fieldName, $paramKey);
80
    }
81
82
    /**
83
     * @param QueryBuilder $queryBuilder
84
     * @param FilterItemInterface $filterItem
85
     * @param string $fieldName
86
     * @return \Doctrine\ORM\Query\Expr\Comparison
87
     */
88
    protected static function addNotEqualExpression(QueryBuilder $queryBuilder, FilterItemInterface $filterItem, string $fieldName)
89
    {
90
        $paramKey = uniqid(':neq_');
91
        $queryBuilder->setParameter($paramKey, $filterItem->getValue());
92
        return $queryBuilder->expr()->neq($fieldName, $paramKey);
93
    }
94
95
    /**
96
     * @param QueryBuilder $queryBuilder
97
     * @param FilterItemInterface $filterItem
98
     * @param string $fieldName
99
     * @return \Doctrine\ORM\Query\Expr\Func|null
100
     */
101
    protected static function addInExpression(QueryBuilder $queryBuilder, FilterItemInterface $filterItem, string $fieldName)
102
    {
103
        $values = static::convertStringToArray($filterItem->getValue());
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $values is correct as static::convertStringToA...filterItem->getValue()) targeting Sf4\Api\Dto\Filter\Filte...:convertStringToArray() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
104
105
        if(true === is_array($values)) {
0 ignored issues
show
introduced by
The condition true === is_array($values) is always false.
Loading history...
106
            return $queryBuilder->expr()->in($fieldName, $values);
107
        }
108
        return null;
109
    }
110
111
    /**
112
     * @param QueryBuilder $queryBuilder
113
     * @param FilterItemInterface $filterItem
114
     * @param string $fieldName
115
     * @return \Doctrine\ORM\Query\Expr\Func|null
116
     */
117
    protected static function addNotInExpression(QueryBuilder $queryBuilder, FilterItemInterface $filterItem, string $fieldName)
118
    {
119
        $values = static::convertStringToArray($filterItem->getValue());
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $values is correct as static::convertStringToA...filterItem->getValue()) targeting Sf4\Api\Dto\Filter\Filte...:convertStringToArray() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
120
121
        if(true === is_array($values)) {
0 ignored issues
show
introduced by
The condition true === is_array($values) is always false.
Loading history...
122
            return $queryBuilder->expr()->notIn($fieldName, $values);
123
        }
124
        return null;
125
    }
126
127
    /**
128
     * @param $string
129
     * @return array|null
130
     */
131
    protected static function convertStringToArray($string)
132
    {
133
        if(false === is_array($string) && true === is_scalar($string)) {
0 ignored issues
show
introduced by
The condition true === is_scalar($string) is always false.
Loading history...
134
            if(false !== strpos($string, ',')) {
135
                return explode(',', $string);
136
            } else {
137
                return [
138
                    $string
139
                ];
140
            }
141
        }
142
143
        return null;
144
    }
145
146
    /**
147
     * @param QueryBuilder $queryBuilder
148
     * @param string $fieldName
149
     * @return string
150
     */
151
    protected static function addIsNullExpression(QueryBuilder $queryBuilder, string $fieldName)
152
    {
153
        return $queryBuilder->expr()->isNull($fieldName);
154
    }
155
156
    /**
157
     * @param QueryBuilder $queryBuilder
158
     * @param string $fieldName
159
     * @return string
160
     */
161
    protected static function addIsNotNullExpression(QueryBuilder $queryBuilder, string $fieldName)
162
    {
163
        return $queryBuilder->expr()->isNotNull($fieldName);
164
    }
165
}
166