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 ( 0fdb8d...63a69f )
by Alessandro
07:04
created

QueryBuilderFactory::ensureQueryBuilderIsDefined()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
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 1
            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
            foreach ($conditions as $condition) {
190 11
                if ($condition !== '') {
191 11
                    $this->qBuilder->andWhere($condition);
192
193 11
                    foreach ($parameters as $parameter) {
194 7
                        $this->qBuilder->setParameter($parameter['field'], $parameter['value']);
195
                    }
196
197 11
                    foreach ($leftJoins as $join) {
198 5
                        if (!$this->joinAlreadyDone($join)) {
199 5
                            $this->storeJoin($join);
200 11
                            $this->qBuilder->leftJoin($join['field'], $join['relation']);
201
                        }
202
                    }
203
                }
204
            }
205
        }
206
207 24
        return $this;
208
    }
209
210 6
    public function sort()
211
    {
212 6
        if (!$this->fields) {
213 1
            throw new \RuntimeException(
214 1
                'Oops! Fields are not defined'
215
            );
216
        }
217
218 5
        if (null === $this->sorting) {
219 1
            throw new \RuntimeException(
220 1
                'Oops! Sorting is not defined'
221
            );
222
        }
223
224 4
        foreach ($this->sorting as $sort => $val) {
225 4
            $val = strtolower($val);
226
227 4
            $fieldName = $this->parser->camelize($sort);
228
229 4
            if (in_array($fieldName, $this->fields)) {
230 2
                $direction = ($val === self::DIRECTION_AZ) ? self::DIRECTION_AZ : self::DIRECTION_ZA;
231 2
                $this->ensureQueryBuilderIsDefined();
232 1
                $this->qBuilder->addOrderBy($this->entityAlias . '.' . $fieldName, $direction);
233
            }
234
235 3
            if (strstr($sort, '_embedded.')) {
236 2
                $this->join($sort);
237 2
                $relationEntityAlias = $this->joinFactory->getRelationEntityAlias();
238
239 2
                $embeddedFields = explode('.', $sort);
240 2
                $fieldName = $this->parser->camelize($embeddedFields[count($embeddedFields) - 1]);
241 2
                $direction = ($val === self::DIRECTION_AZ) ? self::DIRECTION_AZ : self::DIRECTION_ZA;
242
243 3
                $this->qBuilder->addOrderBy($relationEntityAlias . '.' . $fieldName, $direction);
244
            }
245
246
        }
247
248 3
        return $this;
249
    }
250
251 24
    public function getQueryBuilder() :QueryBuilder
252
    {
253 24
        if (!$this->qBuilder) {
254 1
            throw new UnInitializedQueryBuilderException();
255
        }
256
257 23
        return $this->qBuilder;
258
    }
259
260 12
    public function setRel(array $rel)
261
    {
262 12
        $this->rel = $rel;
263
264 12
        return $this;
265
    }
266
267 2
    public function getRel() : array
268
    {
269 2
        return $this->rel;
270
    }
271
272 1
    public function addRel($relation)
273
    {
274 1
        array_push($this->rel, $relation);
275 1
    }
276
277 2
    public function setPrinting($printing)
278
    {
279 2
        $this->printing = $printing;
280
281 2
        return $this;
282
    }
283
284 1
    public function getPrinting()
285
    {
286 1
        return $this->printing;
287
    }
288
289 2
    public function setPage(int $page)
290
    {
291 2
        $this->page = $page;
292
293 2
        return $this;
294
    }
295
296 1
    public function getPage() :int
297
    {
298 1
        return $this->page;
299
    }
300
301 2
    public function setPageLength($pageLength)
302
    {
303 2
        $this->pageLength = $pageLength;
304
305 2
        return $this;
306
    }
307
308 1
    public function getPageLength()
309
    {
310 1
        return $this->pageLength;
311
    }
312
313 2
    public function setSelect($select) : QueryBuilderFactory
314
    {
315 2
        $this->select = $select;
316
317 2
        return $this;
318
    }
319
320 1
    public function getSelect()
321
    {
322 1
        return $this->select;
323
    }
324
325 1
    public function getEntityManager() : EntityManager
326
    {
327 1
        return $this->manager;
328
    }
329
330 2
    public function ensureQueryBuilderIsDefined()
331
    {
332 2
        if (!$this->qBuilder) {
333 1
            throw new \RuntimeException(
334
                'Oops! QueryBuilder was never initialized. '
335
                . "\n" . 'QueryBuilderFactory::createQueryBuilder()'
336 1
                . "\n" . 'QueryBuilderFactory::createSelectAndGroupBy()'
337
            );
338
        }
339 1
    }
340
}
341