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 ( 6babe8...0b25d2 )
by Alessandro
03:54
created

QueryBuilderFactory::getPage()   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 0
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
    private const AND_OPERATOR_LOGIC = 'AND';
20
21
    private 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 29
    public function setFields(array $fields = [])
56
    {
57 29
        $this->fields = $fields;
58
59 29
        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 14
    public function setAndFilters(array $andFilters = [])
75
    {
76 14
        $this->andFilters = $andFilters;
77
78 14
        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 4
    public function setSorting(array $sorting = [])
89
    {
90 4
        $this->sorting = $sorting;
91
92 4
        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 1
    public function join(String $relation, $logicOperator = self::AND_OPERATOR_LOGIC)
110
    {
111 1
        $this->joinFactory->join($relation, $logicOperator);
112
113 1
        $innerJoins = $this->joinFactory->getInnerJoin();
114 1
        $leftJoins = $this->joinFactory->getLeftJoin();
115
116 1
        foreach ($innerJoins as $join) {
117 1
            if (!$this->joinAlreadyDone($join)) {
118 1
                $this->storeJoin($join);
119 1
                $this->qBuilder->innerJoin($join['field'], $join['relation']);
120
            }
121
        }
122
123 1
        foreach ($leftJoins as $join) {
124
            if (!$this->joinAlreadyDone($join)) {
125
                $this->storeJoin($join);
126
                $this->qBuilder->leftJoin($join['field'], $join['relation']);
127
            }
128
        }
129 1
    }
130
131 11
    private function joinAlreadyDone($join) :bool
132
    {
133 11
        $needle = $join['field'] . '_' . $join['relation'];
134 11
        if (in_array($needle, $this->joins)) {
135
            return true;
136
        }
137
138 11
        return false;
139
    }
140
141 11
    private function storeJoin($join)
142
    {
143 11
        $needle = $join['field'] . '_' . $join['relation'];
144 11
        $this->joins[] = $needle;
145 11
    }
146
147 25
    public function filter()
148
    {
149 25
        if (null === $this->andFilters && null === $this->orFilters) {
150 1
            throw new Exceptions\MissingFiltersException();
151
        }
152
153 24
        if (!$this->fields) {
154 1
            throw new Exceptions\MissingFieldsException();
155
        }
156
157 23
        if (null !== $this->andFilters) {
158 12
            $andFilterFactory = new AndFilter($this->entityAlias, $this->fields, $this->joinFactory);
159 12
            $andFilterFactory->createFilter($this->andFilters);
160
161 12
            $conditions = $andFilterFactory->getConditions();
162 12
            $parameters = $andFilterFactory->getParameters();
163 12
            $innerJoins = $andFilterFactory->getInnerJoin();
164
165 12
            foreach($conditions as $condition) {
166 12
                $this->qBuilder->andWhere($condition);
167
            }
168
169 12
            foreach($parameters as $parameter) {
170 8
                $this->qBuilder->setParameter($parameter['field'], $parameter['value']);
171
            }
172
173 12
            foreach ($innerJoins as $join) {
174 5
                if (!$this->joinAlreadyDone($join)) {
175 5
                    $this->storeJoin($join);
176 5
                    $this->qBuilder->innerJoin($join['field'], $join['relation']);
177
                }
178
            }
179
        }
180
181 23
        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 23
        return $this;
206
    }
207
208 5
    public function sort()
209
    {
210 5
        if (!$this->fields) {
211 1
            throw new \RuntimeException(
212 1
                'Oops! Fields are not defined'
213
            );
214
        }
215
216 4
        if (null === $this->sorting) {
217 1
            throw new \RuntimeException(
218 1
                'Oops! Sorting is not defined'
219
            );
220
        }
221
222 3
        foreach ($this->sorting as $sort => $val) {
223 3
            $val = strtolower($val);
224
225 3
            $fieldName = $this->parser->camelize($sort);
226
227 3
            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 2
            if (strstr($sort, '_embedded.')) {
234 1
                $this->join($sort);
235 1
                $relationEntityAlias = $this->joinFactory->getRelationEntityAlias();
236
237 1
                $embeddedFields = explode('.', $sort);
238 1
                $fieldName = $this->parser->camelize($embeddedFields[2]);
239 1
                $direction = ($val === self::DIRECTION_AZ) ? self::DIRECTION_AZ : self::DIRECTION_ZA;
240
241 2
                $this->qBuilder->addOrderBy($relationEntityAlias . '.' . $fieldName, $direction);
242
            }
243
244
        }
245
246 2
        return $this;
247
    }
248
249 22
    public function getQueryBuilder() :QueryBuilder
250
    {
251 22
        if (!$this->qBuilder) {
252 1
            throw new UnInitializedQueryBuilderException();
253
        }
254
255 21
        return $this->qBuilder;
256
    }
257
258 10
    public function setRel(array $rel)
259
    {
260 10
        $this->rel = $rel;
261
262 10
        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