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.
Passed
Push — master ( df11a3...06a5f4 )
by Alessandro
03:28
created

AndFilter::encapsulateValueForLike()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
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 13
    public function __construct(string $entityAlias, array $fields, Join $join)
24
    {
25 13
        $this->entityAlias = $entityAlias;
26 13
        $this->fields = $fields;
27 13
        $this->join = $join;
28
29 13
        $this->conditions = [];
30 13
        $this->parameters = [];
31 13
        $this->parser  = new StringParser();
32 13
    }
33
34 13
    public function createFilter(array $andFilters)
35
    {
36 13
        foreach ($andFilters as $filter => $value) {
37 13
            $this->applyFilter(
38 13
                Objects\FilterObject::fromRawFilter($filter),
39 13
                $value,
40 13
                Objects\Value::fromFilter($value)
41
            );
42
        }
43 13
    }
44
45 13
    private function applyFilter(
46
        Objects\FilterObject $filterObject,
47
        $value,
48
        Objects\Value $filterValue
49
    ) {
50 13
        $whereCondition = $this->entityAlias . '.' . $filterObject->getFieldName() . ' '
51 13
            . $filterObject->getOperatorMeta();
52
53 13
        if (in_array($filterObject->getFieldName(), $this->fields)) {
54 8
            $salt = '_' . random_int(111, 999);
55
56 8
            if ($filterObject->isListContainsType()) {
57 1
                $fieldName = $this->entityAlias . '.' . $filterObject->getFieldName();
58 1
                $whereCondition = $this->createWhereConditionForListContains($value, $fieldName, $filterObject->getFieldName(), $salt);
59 7
            } elseif ($filterObject->isListType()) {
60 1
                $whereCondition .= ' (:field_' . $filterObject->getFieldName() . $salt . ')';
61 6
            } elseif ($filterObject->isFieldEqualityType()) {
62 1
                $whereCondition .= ' ' . $this->entityAlias . '.' . $value;
63 5
            } elseif ($filterObject->isNullType()) {
64 2
                $whereCondition .= ' ';
65
            } else {
66 3
                $whereCondition .= ' :field_' . $filterObject->getFieldName() . $salt;
67
            }
68
69 8
            $this->conditions[] = $whereCondition;
70
71 8
            if ($filterObject->haveOperatorSubstitutionPattern()) {
72 3
                if ($filterObject->isListContainsType()) {
73 1
                    $value = $this->encapsulateValueForLike($value);
74 2
                } elseif ($filterObject->isListType()) {
75 1
                    $value = explode(',', $value);
76
                } else {
77 1
                    $value = str_replace(
78 1
                        '{string}',
79 1
                        $value,
80 1
                        $filterObject->getOperatorsSubstitutionPattern()
81
                    );
82
                }
83
            }
84
85 8
            if (!$filterObject->isNullType()) {
86 6
                if ($filterObject->isListContainsType()) {
87 1
                    $this->addMultipleParameters($value, $filterObject->getFieldName(), $salt);
88
                } else {
89 5
                    $param = [];
90 5
                    $param['field'] = 'field_' . $filterObject->getFieldName() . $salt;
91 5
                    $param['value'] = $value;
92 8
                    $this->parameters[] = $param;
93
                }
94
            }
95
        } else {
96 6
            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 13
        if (strstr($filterObject->getRawFilter(), '_embedded.')) {
105 6
            $this->join->join($filterObject->getRawFilter());
106 6
            $this->relationEntityAlias = $this->join->getRelationEntityAlias();
107
108 6
            $embeddedFields = explode('.', $filterObject->getFieldName());
109 6
            $embeddedFieldName = $this->parser->camelize($embeddedFields[count($embeddedFields) - 1]);
110
111 6
            $salt = '_' . random_int(111, 999);
112
113 6
            $whereCondition = $this->relationEntityAlias . '.' . $embeddedFieldName . ' '
114 6
                . $filterObject->getOperatorMeta();
115
116 6
            if ($filterObject->isListContainsType()) {
117 1
                $fieldName =  $this->relationEntityAlias . '.' . $embeddedFieldName;
118 1
                $whereCondition = $this->createWhereConditionForListContains($value, $fieldName, $embeddedFieldName, $salt);
119 5
            } elseif ($filterObject->isListType()) {
120 1
                $whereCondition .= ' (:field_' . $embeddedFieldName . $salt . ')';
121 4
            } elseif ($filterObject->isNullType()) {
122 2
                $whereCondition .= ' ';
123
            } else {
124 2
                $whereCondition .= ' :field_' . $embeddedFieldName . $salt;
125
            }
126
127 6
            $this->conditions[] = $whereCondition;
128 6
            if ($filterObject->haveOperatorSubstitutionPattern()) {
129 3
                if ($filterObject->isListContainsType()) {
130 1
                    $value = $this->encapsulateValueForLike($value);
131 2
                } elseif ($filterObject->isListType()) {
132 1
                    $value = explode(',', $filterValue->getFilter());
133
                } else {
134 1
                    $value = str_replace(
135 1
                        '{string}',
136 1
                        $value,
137 1
                        $filterObject->getOperatorsSubstitutionPattern()
138
                    );
139
                }
140
            }
141
142 6
            if (!$filterObject->isNullType()) {
143 4
                if ($filterObject->isListContainsType()) {
144 1
                    $this->addMultipleParameters($value, $embeddedFieldName, $salt);
145
                } else {
146 3
                    $param = [];
147 3
                    $param['field'] = 'field_' . $embeddedFieldName . $salt;
148 3
                    $param['value'] = $value;
149 3
                    $this->parameters[] = $param;
150
                }
151
            }
152
        }
153 13
    }
154
155 1
    private function addMultipleParameters($value, $fieldName, $salt)
156
    {
157 1
        foreach ($value as $key => $val) {
158 1
            $param = [];
159 1
            $param['field'] = 'field_' . $fieldName . $salt . $key;
160 1
            $param['value'] = $val;
161 1
            $this->parameters[] = $param;
162
        }
163 1
    }
164
165 1
    private function createWhereConditionForListContains($value, $fieldName, $fieldNameWithoutAlias, $salt) :string
166
    {
167 1
        $whereCondition = '';
168 1
        $values = explode(',', $value);
169 1
        foreach ($values as $key => $val) {
170 1
            if ($whereCondition == '') {
171 1
                $whereCondition = ' (';
172
            } else {
173 1
                $whereCondition .=  ' OR ';
174
            }
175
176
            $whereCondition .= $fieldName .
177 1
                ' LIKE :field_' . str_replace('.', '_', $fieldNameWithoutAlias) . $salt . $key;
178
        }
179
180 1
        $whereCondition .= ')';
181
182 1
        return $whereCondition;
183
    }
184
185 1
    private function encapsulateValueForLike(string $value) : array
186
    {
187 1
        $values = explode(',', $value);
188 1
        foreach ($values as $key => $val) {
189 1
            $values[$key] = '%' . $val . '%';
190
        }
191
192 1
        return $values;
193
    }
194
195 13
    public function getConditions() :array
196
    {
197 13
        return $this->conditions;
198
    }
199
200 13
    public function getParameters() :array
201
    {
202 13
        return $this->parameters;
203
    }
204
205 13
    public function getInnerJoin() :array
206
    {
207 13
        return $this->join->getInnerJoin();
208
    }
209
}