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

QueryBuilderFactory::addRel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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