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 — feature/refactor_and_or_filter ( f3f163...0a90ec )
by Alessandro
06:52 queued 04:52
created

QueryBuilderFactory::joinAlreadyDone()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

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