GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Failed
Pull Request — 2.2 (#165)
by Simone
03:19 queued 30s
created

AndFilter::applyFilter()   F

Complexity

Conditions 21
Paths 3038

Size

Total Lines 105
Code Lines 74

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 74
dl 0
loc 105
rs 0
c 0
b 0
f 0
cc 21
nc 3038
nop 3

How to fix   Long Method    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
namespace Mado\QueryBundle\Queries;
4
5
use Mado\QueryBundle\Services\StringParser;
6
7
class AndFilter
8
{
9
    private $entityAlias;
10
11
    private $fields;
12
13
    private $join;
14
15
    private $conditions;
16
17
    private $parameters;
18
19
    private $relationEntityAlias;
20
21
    private $parser;
22
23
    public function __construct(string $entityAlias, array $fields, Join $join)
24
    {
25
        $this->entityAlias = $entityAlias;
26
        $this->fields = $fields;
27
        $this->join = $join;
28
29
        $this->conditions = [];
30
        $this->parameters = [];
31
        $this->parser  = new StringParser();
32
    }
33
34
    public function createFilter(array $andFilters)
35
    {
36
        foreach ($andFilters as $filter => $value) {
37
            $this->applyFilter(
38
                Objects\FilterObject::fromRawFilter($filter),
39
                $value,
40
                Objects\Value::fromFilter($value)
41
            );
42
        }
43
    }
44
45
    private function applyFilter(
46
        Objects\FilterObject $filterObject,
47
        $value,
48
        Objects\Value $filterValue
49
    ) {
50
        $whereCondition = $this->entityAlias . '.' . $filterObject->getFieldName() . ' '
51
            . $filterObject->getOperatorMeta();
52
53
        if (in_array($filterObject->getFieldName(), $this->fields)) {
54
            $salt = '_' . random_int(111, 999);
55
56
            if ($filterObject->isListContainsType()) {
57
                $fieldName = $this->entityAlias . '.' . $filterObject->getFieldName();
58
                $whereCondition = $this->createWhereConditionForListContains($value, $fieldName, $filterObject->getFieldName(), $salt);
59
            } elseif ($filterObject->isListType()) {
60
                $whereCondition .= ' (:field_' . $filterObject->getFieldName() . $salt . ')';
61
            } elseif ($filterObject->isFieldEqualityType()) {
62
                $whereCondition .= ' ' . $this->entityAlias . '.' . $value;
63
            } elseif ($filterObject->isNullType()) {
64
                $whereCondition .= ' ';
65
            } else {
66
                $whereCondition .= ' :field_' . $filterObject->getFieldName() . $salt;
67
            }
68
69
            $this->conditions[] = $whereCondition;
70
71
            if ($filterObject->haveOperatorSubstitutionPattern()) {
72
                if ($filterObject->isListContainsType()) {
73
                    $value = $this->encapsulateValueForLike($value);
74
                } elseif ($filterObject->isListType()) {
75
                    $value = explode(',', $value);
76
                } else {
77
                    $value = str_replace(
78
                        '{string}',
79
                        $value,
80
                        $filterObject->getOperatorsSubstitutionPattern()
81
                    );
82
                }
83
            }
84
85
            if (!$filterObject->isNullType()) {
86
                if ($filterObject->isListContainsType()) {
87
                    $this->addMultipleParameters($value, $filterObject->getFieldName(), $salt);
88
                } else {
89
                    $param = [];
90
                    $param['field'] = 'field_' . $filterObject->getFieldName() . $salt;
91
                    $param['value'] = $value;
92
                    $this->parameters[] = $param;
93
                }
94
            }
95
        } else {
96
            if (strpos($filterObject->getFieldName(), 'Embedded.') === false) {
97
                $whereCondition .= ' ' . $this->entityAlias . '.' . $value;
98
                $this->conditions[] = $whereCondition;
99
            }
100
        }
101
102
        // controllo se il filtro si riferisce ad una relazione dell'entità quindi devo fare dei join
103
        // esempio per users: filtering[_embedded.groups.name|eq]=admin
104
        if (strstr($filterObject->getRawFilter(), '_embedded.')) {
105
            $this->join->join($filterObject->getRawFilter());
106
            $this->relationEntityAlias = $this->join->getRelationEntityAlias();
107
108
            $embeddedFields = explode('.', $filterObject->getFieldName());
109
            $embeddedFieldName = $this->parser->camelize($embeddedFields[count($embeddedFields) - 1]);
110
111
            $salt = '_' . random_int(111, 999);
112
113
            $whereCondition = $this->relationEntityAlias . '.' . $embeddedFieldName . ' '
114
                . $filterObject->getOperatorMeta();
115
116
            if ($filterObject->isListContainsType()) {
117
                $fieldName =  $this->relationEntityAlias . '.' . $embeddedFieldName;
118
                $whereCondition = $this->createWhereConditionForListContains($value, $fieldName, $embeddedFieldName, $salt);
119
            } elseif ($filterObject->isListType()) {
120
                $whereCondition .= ' (:field_' . $embeddedFieldName . $salt . ')';
121
            } elseif ($filterObject->isNullType()) {
122
                $whereCondition .= ' ';
123
            } else {
124
                $whereCondition .= ' :field_' . $embeddedFieldName . $salt;
125
            }
126
127
            $this->conditions[] = $whereCondition;
128
            if ($filterObject->haveOperatorSubstitutionPattern()) {
129
                if ($filterObject->isListContainsType()) {
130
                    $value = $this->encapsulateValueForLike($value);
131
                } elseif ($filterObject->isListType()) {
132
                    $value = explode(',', $filterValue->getFilter());
133
                } else {
134
                    $value = str_replace(
135
                        '{string}',
136
                        $value,
137
                        $filterObject->getOperatorsSubstitutionPattern()
138
                    );
139
                }
140
            }
141
142
            if (!$filterObject->isNullType()) {
143
                if ($filterObject->isListContainsType()) {
144
                    $this->addMultipleParameters($value, $embeddedFieldName, $salt);
145
                } else {
146
                    $param = [];
147
                    $param['field'] = 'field_' . $embeddedFieldName . $salt;
148
                    $param['value'] = $value;
149
                    $this->parameters[] = $param;
150
                }
151
            }
152
        }
153
    }
154
155
    private function addMultipleParameters($value, $fieldName, $salt)
156
    {
157
        foreach ($value as $key => $val) {
158
            $param = [];
159
            $param['field'] = 'field_' . $fieldName . $salt . $key;
160
            $param['value'] = $val;
161
            $this->parameters[] = $param;
162
        }
163
    }
164
165
    private function createWhereConditionForListContains($value, $fieldName, $fieldNameWithoutAlias, $salt) :string
166
    {
167
        $whereCondition = '';
168
        $values = explode(',', $value);
169
        foreach ($values as $key => $val) {
170
            if ($whereCondition == '') {
171
                $whereCondition = ' (';
172
            } else {
173
                $whereCondition .=  ' OR ';
174
            }
175
176
            $whereCondition .= $fieldName .
177
                ' LIKE :field_' . str_replace('.', '_', $fieldNameWithoutAlias) . $salt . $key;
178
        }
179
180
        $whereCondition .= ')';
181
182
        return $whereCondition;
183
    }
184
185
    private function encapsulateValueForLike(string $value) : array
186
    {
187
        $values = explode(',', $value);
188
        foreach ($values as $key => $val) {
189
            $values[$key] = '%' . $val . '%';
190
        }
191
192
        return $values;
193
    }
194
195
    public function getConditions() :array
196
    {
197
        return $this->conditions;
198
    }
199
200
    public function getParameters() :array
201
    {
202
        return $this->parameters;
203
    }
204
205
    public function getInnerJoin() :array
206
    {
207
        return $this->join->getInnerJoin();
208
    }
209
}