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.4 (#144)
by Alessandro
03:01
created

QueryBuilderFactory   D

Complexity

Total Complexity 61

Size/Duplication

Total Lines 349
Duplicated Lines 0 %

Test Coverage

Coverage 97.53%

Importance

Changes 0
Metric Value
wmc 61
dl 0
loc 349
rs 4.054
c 0
b 0
f 0
ccs 158
cts 162
cp 0.9753

28 Methods

Rating   Name   Duplication   Size   Complexity  
A getValueAvailableFilters() 0 3 1
A getFields() 0 9 2
A storeJoinDone() 0 4 1
A getSelect() 0 3 1
A getAvailableFilters() 0 3 1
A setAndFilters() 0 5 1
A getAndFilters() 0 3 1
A setPageLength() 0 5 1
A setOrFilters() 0 5 1
A addRel() 0 3 1
A getOrFilters() 0 3 1
A setSorting() 0 5 1
A setPage() 0 5 1
A getPage() 0 3 1
A setFields() 0 5 1
A getPrinting() 0 3 1
B join() 0 18 5
C sort() 0 39 8
A joinAlreadyDone() 0 8 2
A getRel() 0 3 1
D filter() 0 84 19
A getPageLength() 0 3 1
A getEntityManager() 0 3 1
A setPrinting() 0 5 1
A ensureQueryBuilderIsDefined() 0 7 2
A setRel() 0 5 1
A setSelect() 0 5 1
A getQueryBuilder() 0 7 2

How to fix   Complexity   

Complex Class

Complex classes like QueryBuilderFactory often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use QueryBuilderFactory, and based on these observations, apply Extract Interface, too.

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
    protected $sorting;
33
34
    private $joins = [];
35
36
    protected $rel;
37
38
    protected $printing;
39
40
    protected $page;
41
42
    protected $pageLength;
43
44
    protected $select;
45
46
    public function getAvailableFilters()
47
    {
48 1
        return array_keys($this->getValueAvailableFilters());
49
    }
50 1
51
    public function getValueAvailableFilters()
52
    {
53 2
        return Dictionary::getOperators();
54
    }
55 2
56
    public function setFields(array $fields = [])
57
    {
58 29
        $this->fields = $fields;
59
60 29
        return $this;
61
    }
62 29
63
    public function getFields()
64
    {
65 2
        if (null === $this->fields) {
66
            throw new \RuntimeException(
67 2
                'Oops! Fields are not defined'
68 1
            );
69 1
        }
70
71
        return $this->fields;
72
    }
73 1
74
    /** @since version 2.2 */
75
    public function setAndFilters(array $andFilters = [])
76
    {
77 14
        $this->andFilters = $andFilters;
78
79 14
        return $this;
80
    }
81 14
82
    public function setOrFilters(array $orFilters = [])
83
    {
84 13
        $this->orFilters = $orFilters;
85
86 13
        return $this;
87
    }
88 13
89
    public function setSorting(array $sorting = [])
90
    {
91 4
        $this->sorting = $sorting;
92
93 4
        return $this;
94
    }
95 4
96
    public function getAndFilters()
97
    {
98 1
        return $this->andFilters;
99
    }
100 1
101
    public function getOrFilters()
102
    {
103 1
        return $this->orFilters;
104
    }
105 1
106
    /**
107
     * @param String $relation Nome della relazione semplice (groups.name) o con embedded (_embedded.groups.name)
108 11
     * @return $this
109
     */
110 11
    public function join(String $relation, $logicOperator = self::AND_OPERATOR_LOGIC)
111 11
    {
112
        $this->joinFactory->join($relation, $logicOperator);
113
114 11
        $innerJoins = $this->joinFactory->getInnerJoin();
115
        $leftJoins = $this->joinFactory->getLeftJoin();
116 11
117
        foreach ($innerJoins as $join) {
118
            if (!$this->joinAlreadyDone($join)) {
119 11
                $this->storeJoinDone($join);
120
                $this->qBuilder->innerJoin($join['field'], $join['relation']);
121 11
            }
122 11
        }
123 11
124
        foreach ($leftJoins as $join) {
125
            if (!$this->joinAlreadyDone($join)) {
126
                $this->storeJoinDone($join);
127
                $this->qBuilder->leftJoin($join['field'], $join['relation']);
128
            }
129 11
        }
130
    }
131 11
132 11
    private function joinAlreadyDone($join) :bool
133
    {
134
        $needle = $join['field'] . '_' . $join['relation'];
135 11
        if (in_array($needle, $this->joins)) {
136 11
            return true;
137 11
        }
138
139
        return false;
140 11
    }
141
142
    private function storeJoinDone($join)
143 11
    {
144
        $needle = $join['field'] . '_' . $join['relation'];
145 11
        $this->joins[] = $needle;
146
    }
147
148 11
    public function filter()
149 11
    {
150
        if (null === $this->andFilters && null === $this->orFilters) {
151 11
            throw new Exceptions\MissingFiltersException();
152 11
        }
153 11
154
        if (!$this->fields) {
155 11
            throw new Exceptions\MissingFieldsException();
156
        }
157 11
158
        if (null !== $this->andFilters) {
159 11
            $andFilterFactory = new AndFilterFactory($this->entityAlias, $this->fields, $this->joinFactory);
160
            $andFilterFactory->createFilter($this->andFilters);
161 11
162
            $conditions = $andFilterFactory->getConditions();
163 11
            $parameters = $andFilterFactory->getParameters();
164 11
            $innerJoins = $andFilterFactory->getInnerJoin();
165 6
166 5
            foreach($conditions as $condition) {
167 5
                $this->qBuilder->andWhere($condition);
168
            }
169
170
            foreach($parameters as $parameter) {
171
                $this->qBuilder->setParameter($parameter['field'], $parameter['value']);
172 11
            }
173
174
            foreach ($innerJoins as $join) {
175 11
                if (!$this->joinAlreadyDone($join)) {
176 11
                    $this->storeJoinDone($join);
177
                    $this->qBuilder->innerJoin($join['field'], $join['relation']);
178
                }
179 11
            }
180
        }
181
182 11
        if (null !== $this->orFilters) {
183
            $orFilterFactory = new OrFilterFactory($this->entityAlias, $this->fields, $this->joinFactory);
184
            $orFilterFactory->createFilter($this->orFilters);
185 25
186
            $conditions = $orFilterFactory->getConditions();
187 25
            $parameters = $orFilterFactory->getParameters();
188 1
            $leftJoins = $orFilterFactory->getLeftJoin();
189
190
            if ($conditions != '') {
191 24
                $this->qBuilder->andWhere($conditions);
192 1
193
                foreach ($parameters as $parameter) {
194
                    $this->qBuilder->setParameter($parameter['field'], $parameter['value']);
195 23
                }
196 12
197 12
                foreach ($leftJoins as $leftJoin) {
198 12
                    $needle = $leftJoin['field'] . '_' . $leftJoin['relation'];
199 12
                    if (!in_array($needle, $this->joins)) {
200 12
                        $this->joins[] = $needle;
201
                        $this->qBuilder->leftJoin($leftJoin['field'], $leftJoin['relation']);
202
                    }
203
                }
204
            }
205 23
        }
206 11
207 11
        if (null !== $this->orFilters) {
208 11
            $orFilterFactory = new OrFilterFactory($this->entityAlias, $this->fields, $this->joinFactory);
209
            $orFilterFactory->createFilter($this->orFilters);
210 11
211 11
            $conditions = $orFilterFactory->getConditions();
212 11
            $parameters = $orFilterFactory->getParameters();
213 11
            $leftJoins = $orFilterFactory->getLeftJoin();
214 11
215
            if ($conditions !== '') {
216
                $this->qBuilder->andWhere($conditions);
217
218 11
                foreach ($parameters as $parameter) {
219 11
                    $this->qBuilder->setParameter($parameter['field'], $parameter['value']);
220
                }
221 11
222 7
                foreach ($leftJoins as $join) {
223
                    if (!$this->joinAlreadyDone($join)) {
224
                        $this->storeJoinDone($join);
225
                        $this->qBuilder->leftJoin($join['field'], $join['relation']);
226
                    }
227 23
                }
228
            }
229
        }
230 12
        
231
        return $this;
232
    }
233
234
    public function sort()
235 12
    {
236 12
        if (!$this->fields) {
237
            throw new \RuntimeException(
238 12
                'Oops! Fields are not defined'
239 7
            );
240
        }
241 7
242 1
        if (null === $this->sorting) {
243 6
            throw new \RuntimeException(
244 1
                'Oops! Sorting is not defined'
245 5
            );
246 2
        }
247
248 3
        foreach ($this->sorting as $sort => $val) {
249
            $val = strtolower($val);
250
251 7
            $fieldName = $this->parser->camelize($sort);
252
253 7
            if (in_array($fieldName, $this->fields)) {
254 2
                $direction = ($val === self::DIRECTION_AZ) ? self::DIRECTION_AZ : self::DIRECTION_ZA;
255 1
                $this->ensureQueryBuilderIsDefined();
256
                $this->qBuilder->addOrderBy($this->entityAlias . '.' . $fieldName, $direction);
257 1
            }
258 1
259 1
            if (strstr($sort, '_embedded.')) {
260 1
                $this->join($sort);
261
                $relationEntityAlias = $this->joinFactory->getRelationEntityAlias();
262
263
                $embeddedFields = explode('.', $sort);
264
                $fieldName = $this->parser->camelize($embeddedFields[2]);
265 7
                $direction = ($val === self::DIRECTION_AZ) ? self::DIRECTION_AZ : self::DIRECTION_ZA;
266 7
267
                $this->qBuilder->addOrderBy($relationEntityAlias . '.' . $fieldName, $direction);
268
            }
269 5
270
        }
271
272
        return $this;
273
    }
274
275
    public function getQueryBuilder() :QueryBuilder
276
    {
277 12
        if (!$this->qBuilder) {
278
            throw new UnInitializedQueryBuilderException();
279 5
        }
280 5
281
        return $this->qBuilder;
282 5
    }
283 5
284
    public function setRel(array $rel)
285 5
    {
286
        $this->rel = $rel;
287 5
288 5
        return $this;
289
    }
290 5
291 1
    public function getRel() : array
292 4
    {
293 2
        return $this->rel;
294
    }
295 2
296
    public function addRel($relation)
297
    {
298 5
        array_push($this->rel, $relation);
299 5
    }
300 2
301 1
    public function setPrinting($printing)
302
    {
303 1
        $this->printing = $printing;
304 1
305 1
        return $this;
306 1
    }
307
308
    public function getPrinting()
309
    {
310
        return $this->printing;
311 5
    }
312 3
313
    public function setPage(int $page)
314
    {
315 12
        $this->page = $page;
316
317 11
        return $this;
318
    }
319
320
    public function getPage() :int
321
    {
322 11
        return $this->page;
323 11
    }
324
325
    public function setPageLength($pageLength)
326
    {
327 11
        $this->pageLength = $pageLength;
328 6
329
        return $this;
330 6
    }
331 1
332 5
    public function getPageLength()
333 1
    {
334 4
        return $this->pageLength;
335 2
    }
336
337 2
    public function setSelect($select) : QueryBuilderFactory
338
    {
339
        $this->select = $select;
340 6
341
        return $this;
342
    }
343 6
344
    public function getSelect()
345
    {
346 6
        return $this->select;
347 2
    }
348 1
349
    public function getEntityManager() : EntityManager
350 1
    {
351 1
        return $this->manager;
352 1
    }
353 1
354
    public function ensureQueryBuilderIsDefined()
355
    {
356
        if (!$this->qBuilder) {
357
            throw new \RuntimeException(
358 6
                'Oops! QueryBuilder was never initialized. '
359 4
                . "\n" . 'QueryBuilderFactory::createQueryBuilder()'
360 4
                . "\n" . 'QueryBuilderFactory::createSelectAndGroupBy()'
361 6
            );
362
        }
363
    }
364
}
365