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 — 2.3 ( 8686ea...f49551 )
by Simone
04:54
created

QueryBuilderFactory::setPageLength()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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