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 — feature/refactor_and_or_filter ( ca568a...4a7062 )
by Alessandro
03:12
created

QueryBuilderFactory::noExistsJoin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Mado\QueryBundle\Queries;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\QueryBuilder;
7
use Mado\QueryBundle\Component\Meta\Exceptions\UnInitializedQueryBuilderException;
8
use Mado\QueryBundle\Dictionary;
9
use Mado\QueryBundle\Exceptions;
10
use Mado\QueryBundle\Queries\Objects\FilterObject;
11
12
class QueryBuilderFactory extends AbstractQuery
13
{
14
    const DIRECTION_AZ = 'asc';
15
16
    const DIRECTION_ZA = 'desc';
17
18
    const DEFAULT_OPERATOR = 'eq';
19
20
    private const AND_OPERATOR_LOGIC = 'AND';
21
22
    private const OR_OPERATOR_LOGIC = 'OR';
23
24
    protected $qBuilder;
25
26
    protected $fields;
27
28
    protected $andFilters;
29
30
    protected $orFilters;
31
32
    private $relationEntityAlias;
33
34
    protected $sorting;
35
36
    private $joins = [];
37
38
    protected $rel;
39
40
    protected $printing;
41
42
    protected $page;
43
44
    protected $pageLength;
45
46
    protected $select;
47
48 1
    public function getAvailableFilters()
49
    {
50 1
        return array_keys($this->getValueAvailableFilters());
51
    }
52
53 2
    public function getValueAvailableFilters()
54
    {
55 2
        return Dictionary::getOperators();
56
    }
57
58 29
    public function setFields(array $fields = [])
59
    {
60 29
        $this->fields = $fields;
61
62 29
        return $this;
63
    }
64
65 2
    public function getFields()
66
    {
67 2
        if (null === $this->fields) {
68 1
            throw new \RuntimeException(
69 1
                'Oops! Fields are not defined'
70
            );
71
        }
72
73 1
        return $this->fields;
74
    }
75
76
    /** @since version 2.2 */
77 14
    public function setAndFilters(array $andFilters = [])
78
    {
79 14
        $this->andFilters = $andFilters;
80
81 14
        return $this;
82
    }
83
84 13
    public function setOrFilters(array $orFilters = [])
85
    {
86 13
        $this->orFilters = $orFilters;
87
88 13
        return $this;
89
    }
90
91 4
    public function setSorting(array $sorting = [])
92
    {
93 4
        $this->sorting = $sorting;
94
95 4
        return $this;
96
    }
97
98 1
    public function getAndFilters()
99
    {
100 1
        return $this->andFilters;
101
    }
102
103 1
    public function getOrFilters()
104
    {
105 1
        return $this->orFilters;
106
    }
107
108
    /**
109
     * @param String $relation Nome della relazione semplice (groups.name) o con embedded (_embedded.groups.name)
110
     * @return $this
111
     */
112 1
    public function join(String $relation, $logicOperator = self::AND_OPERATOR_LOGIC)
113
    {
114 1
        $this->joinFactory->join($relation, $logicOperator);
115
116 1
        $innerJoins = $this->joinFactory->getInnerJoin();
117 1
        $leftJoins = $this->joinFactory->getLeftJoin();
118
119 1
        foreach ($innerJoins as $innerJoin) {
120 1
            $needle = $innerJoin['field'] . '_' . $innerJoin['relation'];
121 1
            if (!in_array($needle, $this->joins)) {
122 1
                $this->joins[] = $needle;
123 1
                $this->qBuilder->innerJoin($innerJoin['field'], $innerJoin['relation']);
124
125
            }
126
        }
127
128 1
        foreach ($leftJoins as $leftJoin) {
129
            $needle = $leftJoin['field'] . '_' . $leftJoin['relation'];
130
            if (!in_array($needle, $this->joins)) {
131
                $this->joins[] = $needle;
132
                $this->qBuilder->leftJoin($leftJoin['field'], $leftJoin['relation']);
133
            }
134
        }
135 1
    }
136
137 25
    public function filter()
138
    {
139 25
        if (null === $this->andFilters && null === $this->orFilters) {
140 1
            throw new Exceptions\MissingFiltersException();
141
        }
142
143 24
        if (!$this->fields) {
144 1
            throw new Exceptions\MissingFieldsException();
145
        }
146
147 23
        if (null !== $this->andFilters) {
148 12
            $andFilterFactory = new AndFilterFactory($this->entityAlias, $this->fields, $this->joinFactory);
149 12
            $andFilterFactory->createFilter($this->andFilters);
150
151 12
            $conditions = $andFilterFactory->getConditions();
152 12
            $parameters = $andFilterFactory->getParameters();
153 12
            $innerJoins = $andFilterFactory->getInnerJoin();
154
155 12
            foreach($conditions as $condition) {
156 12
                $this->qBuilder->andWhere($condition);
157
            }
158
159 12
            foreach($parameters as $parameter) {
160 8
                $this->qBuilder->setParameter($parameter['field'], $parameter['value']);
161
            }
162
163 12
            foreach ($innerJoins as $innerJoin) {
164 5
                $needle = $innerJoin['field'] . '_' . $innerJoin['relation'];
165 5
                if (!in_array($needle, $this->joins)) {
166 5
                    $this->joins[] = $needle;
167 5
                    $this->qBuilder->innerJoin($innerJoin['field'], $innerJoin['relation']);
168
                }
169
            }
170
        }
171
172 23
        if (null !== $this->orFilters) {
173 11
            $orFilterFactory = new OrFilterFactory($this->entityAlias, $this->fields, $this->joinFactory);
174 11
            $orFilterFactory->createFilter($this->orFilters);
175
176 11
            $conditions = $orFilterFactory->getConditions();
177 11
            $parameters = $orFilterFactory->getParameters();
178 11
            $leftJoins = $orFilterFactory->getLeftJoin();
179
180 11
            if ($conditions != '') {
181 11
                $this->qBuilder->andWhere($conditions);
182
183 11
                foreach ($parameters as $parameter) {
184 7
                    $this->qBuilder->setParameter($parameter['field'], $parameter['value']);
185
                }
186
187 11
                foreach ($leftJoins as $leftJoin) {
188 5
                    $needle = $leftJoin['field'] . '_' . $leftJoin['relation'];
189 5
                    if (!in_array($needle, $this->joins)) {
190 5
                        $this->joins[] = $needle;
191 5
                        $this->qBuilder->leftJoin($leftJoin['field'], $leftJoin['relation']);
192
                    }
193
                }
194
            }
195
196
            /*$orFilter = [];
197
            $orFilter['orCondition'] = null;
198
            $orFilter['parameters'] = [];
199
200
            foreach ($this->orFilters as $filter => $value) {
201
                $orFilter = $this->applyFilterOr(
202
                    Objects\FilterObject::fromRawFilter($filter),
203
                    $value,
204
                    $orFilter
205
                );
206
            }
207
208
            if ((count($orFilter) > 0) && (null != $orFilter['orCondition'])) {
209
                $this->qBuilder->andWhere($orFilter['orCondition']);
210
211
                foreach ($orFilter['parameters'] as $parameter) {
212
                    $this->qBuilder->setParameter($parameter['field'], $parameter['value']);
213
                }
214
            }*/
215
        }
216
217 23
        return $this;
218
    }
219
220 5
    public function sort()
221
    {
222 5
        if (!$this->fields) {
223 1
            throw new \RuntimeException(
224 1
                'Oops! Fields are not defined'
225
            );
226
        }
227
228 4
        if (null === $this->sorting) {
229 1
            throw new \RuntimeException(
230 1
                'Oops! Sorting is not defined'
231
            );
232
        }
233
234 3
        foreach ($this->sorting as $sort => $val) {
235 3
            $val = strtolower($val);
236
237 3
            $fieldName = $this->parser->camelize($sort);
238
239 3
            if (in_array($fieldName, $this->fields)) {
240 2
                $direction = ($val === self::DIRECTION_AZ) ? self::DIRECTION_AZ : self::DIRECTION_ZA;
241 2
                $this->ensureQueryBuilderIsDefined();
242 1
                $this->qBuilder->addOrderBy($this->entityAlias . '.' . $fieldName, $direction);
243
            }
244
245 2
            if (strstr($sort, '_embedded.')) {
246 1
                $this->join($sort);
247 1
                $relationEntityAlias = $this->joinFactory->getRelationEntityAlias();
248
249 1
                $embeddedFields = explode('.', $sort);
250 1
                $fieldName = $this->parser->camelize($embeddedFields[2]);
251 1
                $direction = ($val === self::DIRECTION_AZ) ? self::DIRECTION_AZ : self::DIRECTION_ZA;
252
253 2
                $this->qBuilder->addOrderBy($relationEntityAlias . '.' . $fieldName, $direction);
254
            }
255
256
        }
257
258 2
        return $this;
259
    }
260
261 22
    public function getQueryBuilder() :QueryBuilder
262
    {
263 22
        if (!$this->qBuilder) {
264 1
            throw new UnInitializedQueryBuilderException();
265
        }
266
267 21
        return $this->qBuilder;
268
    }
269
270
    private function setRelationEntityAlias(string $relationEntityAlias)
0 ignored issues
show
Unused Code introduced by
The method setRelationEntityAlias() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
271
    {
272
        $this->relationEntityAlias = $relationEntityAlias;
273
    }
274
275
    private function getRelationEntityAlias()
276
    {
277
        return $this->relationEntityAlias;
278
    }
279
280 10
    public function setRel(array $rel)
281
    {
282 10
        $this->rel = $rel;
283
284 10
        return $this;
285
    }
286
287 2
    public function getRel() : array
288
    {
289 2
        return $this->rel;
290
    }
291
292 1
    public function addRel($relation)
293
    {
294 1
        array_push($this->rel, $relation);
295 1
    }
296
297 2
    public function setPrinting($printing)
298
    {
299 2
        $this->printing = $printing;
300
301 2
        return $this;
302
    }
303
304 1
    public function getPrinting()
305
    {
306 1
        return $this->printing;
307
    }
308
309 2
    public function setPage(int $page)
310
    {
311 2
        $this->page = $page;
312
313 2
        return $this;
314
    }
315
316 1
    public function getPage() :int
317
    {
318 1
        return $this->page;
319
    }
320
321 2
    public function setPageLength($pageLength)
322
    {
323 2
        $this->pageLength = $pageLength;
324
325 2
        return $this;
326
    }
327
328 1
    public function getPageLength()
329
    {
330 1
        return $this->pageLength;
331
    }
332
333 2
    public function setSelect($select) : QueryBuilderFactory
334
    {
335 2
        $this->select = $select;
336
337 2
        return $this;
338
    }
339
340 1
    public function getSelect()
341
    {
342 1
        return $this->select;
343
    }
344
345 1
    public function getEntityManager() : EntityManager
346
    {
347 1
        return $this->manager;
348
    }
349
350 2
    public function ensureQueryBuilderIsDefined()
351
    {
352 2
        if (!$this->qBuilder) {
353 1
            throw new \RuntimeException(
354
                'Oops! QueryBuilder was never initialized. '
355
                . "\n" . 'QueryBuilderFactory::createQueryBuilder()'
356 1
                . "\n" . 'QueryBuilderFactory::createSelectAndGroupBy()'
357
            );
358
        }
359 1
    }
360
}
361