Completed
Push — develop ( 7865e0...678432 )
by
unknown
65:19
created

TabellaQueryTrait   B

Complexity

Total Complexity 46

Size/Duplication

Total Lines 333
Duplicated Lines 0 %

Test Coverage

Coverage 95.92%

Importance

Changes 7
Bugs 1 Features 0
Metric Value
eloc 194
dl 0
loc 333
ccs 94
cts 98
cp 0.9592
rs 8.72
c 7
b 1
f 0
wmc 46

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getApiRecordstabella() 0 40 3
A orderByBuilder() 0 7 2
B recursiveJoin() 0 32 9
A appendFilterString() 0 17 5
A getApiOperator() 0 18 2
A orderByApiBuilder() 0 13 3
B buildWhere() 0 48 7
A biQueryBuilder() 0 12 1
A getRecordstabella() 0 37 5
B filterByApiBuilder() 0 64 9

How to fix   Complexity   

Complex Class

Complex classes like TabellaQueryTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use TabellaQueryTrait, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Cdf\BiCoreBundle\Utils\Tabella;
4
5
use Doctrine\ORM\Tools\Pagination\Paginator;
6
use Doctrine\Common\Collections\Expr\Comparison;
7
use Cdf\BiCoreBundle\Utils\FieldType\FieldTypeUtils;
8
use function count;
9
10 12
trait TabellaQueryTrait
11
{
12 12
    protected function biQueryBuilder()
13 12
    {
14 12
        $nometabellaalias = $this->generaAlias($this->tablename);
0 ignored issues
show
Bug introduced by
It seems like generaAlias() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

14
        /** @scrutinizer ignore-call */ 
15
        $nometabellaalias = $this->generaAlias($this->tablename);
Loading history...
15 12
        $qb = $this->em->createQueryBuilder()
16 12
                ->select(array($nometabellaalias))
17 12
                ->from($this->entityname, $nometabellaalias);
18 12
        $campi = array_keys($this->em->getMetadataFactory()->getMetadataFor($this->entityname)->reflFields);
19 12
        $this->recursiveJoin($qb, $campi, $this->tablename, $nometabellaalias);
20
        $this->buildWhere($qb);
21 12
        $this->orderByBuilder($qb);
22
23
        return $qb;
24 12
    }
25
26 12
    protected function recursiveJoin(&$qb, $campi, $nometabella, $alias, $ancestors = array())
27 12
    {
28
        foreach ($campi as $campo) {
29
            if (false !== strpos(strtolower($campo), 'relatedby')) {
30 12
                continue;
31 12
            }
32
            if (!in_array($nometabella, $ancestors)) {
33
                $ancestors[] = $nometabella;
34 12
            }
35 12
36 12
            $configurazionecampo = isset($this->configurazionecolonnetabella[ucfirst(implode('.', $ancestors)).'.'.$campo]) ?
37
                    $this->configurazionecolonnetabella[ucfirst(implode('.', $ancestors)).'.'.$campo] : false;
38 6
            if ($configurazionecampo && true === $configurazionecampo['association']) {
39 6
                // crea la relazione con $padre = $nometabella in corso e figlio = $nomecampo con $alias generato
40 6
                if ((isset($configurazionecampo['sourceentityclass'])) && (null !== $configurazionecampo['sourceentityclass'])) {
41
                    $entitysrc = $configurazionecampo['sourceentityclass'];
42
                    $nometabellasrc = $this->em->getClassMetadata($entitysrc)->getTableName();
43
                } else {
44
                    $nometabellasrc = $nometabella;
45 6
                }
46 6
47 6
                $entitytarget = $configurazionecampo['associationtable']['targetEntity'];
48
                $nometabellatarget = $this->em->getClassMetadata($entitytarget)->getTableName();
49
                $aliastarget = $this->generaAlias($nometabellatarget, $nometabellasrc, $ancestors);
50 6
                //$qb->leftJoin($alias . "." . $configurazionecampo["nomecampo"], $aliastarget);
51
                //$camporelazionejoin = strtolower(substr($configurazionecampo["nomecampo"], strpos($configurazionecampo["nomecampo"], ".") + 1));
52 6
                $parti = explode('.', $configurazionecampo['nomecampo']);
53 6
54 6
                $camporelazionejoin = strtolower($parti[count($parti) - 1]);
55 6
                $qb->leftJoin($alias.'.'.$camporelazionejoin, $aliastarget);
56
                $campitarget = array_keys($this->em->getMetadataFactory()->getMetadataFor($entitytarget)->reflFields);
57
                $this->recursiveJoin($qb, $campitarget, $nometabellatarget, $aliastarget, $ancestors);
58
59
                // lancia rescursiveJoin su questo campo con padre = $aliasgenerato
60
                // --- figlio = $nomecampo
61
                // --- alias = alias generato nuovo
62 12
            }
63
        }
64 12
    }
65
66 12
    protected function buildWhere(&$qb)
67 12
    {
68 12
        $filtro = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $filtro is dead and can be removed.
Loading history...
69 2
        $prefiltro = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $prefiltro is dead and can be removed.
Loading history...
70
        foreach ($this->prefiltri as $key => $prefiltro) {
71 12
            $this->prefiltri[$key]['prefiltro'] = true;
0 ignored issues
show
Bug Best Practice introduced by
The property prefiltri does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
72 2
        }
73
        foreach ($this->filtri as $key => $filtro) {
74 12
            $this->filtri[$key]['prefiltro'] = false;
0 ignored issues
show
Bug Best Practice introduced by
The property filtri does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
75 12
        }
76 12
        $tuttifiltri = array_merge($this->filtri, $this->prefiltri);
77 3
        $parametribag = array();
78 3
        if (count($tuttifiltri)) {
79 3
            $descrizionefiltri = '';
80 3
            foreach ($tuttifiltri as $num => $filtrocorrente) {
81 3
                $tablename = substr($filtrocorrente['nomecampo'], 0, strripos($filtrocorrente['nomecampo'], '.'));
82 3
                $alias = $this->findAliasByTablename($tablename);
0 ignored issues
show
Bug introduced by
It seems like findAliasByTablename() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
                /** @scrutinizer ignore-call */ 
83
                $alias = $this->findAliasByTablename($tablename);
Loading history...
83 3
                $fieldname = $alias.'.'.(substr($filtrocorrente['nomecampo'], strripos($filtrocorrente['nomecampo'], '.') + 1));
84 3
                $fieldvalue = $this->getFieldValue($filtrocorrente['valore']);
0 ignored issues
show
Bug introduced by
It seems like getFieldValue() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
                /** @scrutinizer ignore-call */ 
85
                $fieldvalue = $this->getFieldValue($filtrocorrente['valore']);
Loading history...
85 3
                $fieldoperator = $this->getOperator($filtrocorrente['operatore']);
0 ignored issues
show
Bug introduced by
The method getOperator() does not exist on Cdf\BiCoreBundle\Utils\Tabella\TabellaQueryTrait. Did you maybe mean getApiOperator()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
                /** @scrutinizer ignore-call */ 
86
                $fieldoperator = $this->getOperator($filtrocorrente['operatore']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86 2
                $fitrocorrenteqp = 'fitrocorrente'.$num;
87 2
                $filtronomecampocorrente = $this->findFieldnameByAlias($filtrocorrente['nomecampo']);
0 ignored issues
show
Bug introduced by
It seems like findFieldnameByAlias() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
                /** @scrutinizer ignore-call */ 
88
                $filtronomecampocorrente = $this->findFieldnameByAlias($filtrocorrente['nomecampo']);
Loading history...
88 2
                $criteria = new ParametriQueryTabellaDecoder(
89 2
                    $fieldname,
90 2
                    $fieldoperator,
91 2
                    $fieldvalue,
92
                    $fitrocorrenteqp,
93
                    $filtronomecampocorrente
94 2
                );
95 2
96
                $querycriteria = $criteria->getQueryCriteria();
97 2
                $queryparameter = $criteria->getQueryParameters();
98 2
99 2
                if ($querycriteria) {
100
                    $qb->andWhere($querycriteria);
101 2
                    $parametribag = array_merge($queryparameter, $parametribag);
102 2
                } else {
103
                    $qb->andWhere($fieldname.' '.$fieldoperator.' '.":$fitrocorrenteqp");
104 2
                    $parametribag = array_merge(array($fitrocorrenteqp => $fieldvalue), $parametribag);
105
                }
106 2
                $this->getDescrizioneFiltro($descrizionefiltri, $filtrocorrente, $criteria);
0 ignored issues
show
Bug introduced by
It seems like getDescrizioneFiltro() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
                $this->/** @scrutinizer ignore-call */ 
107
                       getDescrizioneFiltro($descrizionefiltri, $filtrocorrente, $criteria);
Loading history...
107
            }
108 12
            $this->traduzionefiltri = substr($descrizionefiltri, 2);
0 ignored issues
show
Bug Best Practice introduced by
The property traduzionefiltri does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
109
        }
110 12
        $qb->setParameters($parametribag);
111
112
        if (isset($this->wheremanuale)) {
113 12
            $qb->andWhere($this->wheremanuale);
114
        }
115 12
    }
116
117 12
    protected function orderByBuilder(&$qb)
118 10
    {
119 10
        foreach ($this->colonneordinamento as $nomecampo => $tipoordinamento) {
120 10
            $tablename = substr($nomecampo, 0, strripos($nomecampo, '.'));
121 10
            $alias = $this->getAliasGenerato($tablename);
0 ignored issues
show
Bug introduced by
It seems like getAliasGenerato() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
            /** @scrutinizer ignore-call */ 
122
            $alias = $this->getAliasGenerato($tablename);
Loading history...
122
            $fieldname = $alias.'.'.(substr($nomecampo, strripos($nomecampo, '.') + 1));
123 12
            $qb->addOrderBy($fieldname, $tipoordinamento);
124
        }
125 12
    }
126
127 12
128
    /**
129 12
     * It appends the new filter string part to the given filter string ($filterString)
130 12
     */
131 12
    private function appendFilterString(String &$filterString, $swaggerType, $fieldvalue)
132 12
    {
133
        if ($swaggerType == null /*|| $swaggerFormats[ $nomeCampo ] == 'datetime'*/) {
134 12
            $filterString .= '"%'.$fieldvalue.'%"';
135
        } elseif ($swaggerType == 'datetime' || $swaggerType == 'date') {
136
            $fieldvalue = \str_replace("/", "-", $fieldvalue);
137 12
            //does it contain an hour ?
138 12
            $hour = strpos($fieldvalue, ":");
139
            $time = strtotime($fieldvalue);
140
            $backend_format = FieldTypeUtils::getEnvVar("BE_DATETIME_FORMAT", "Y-m-d\TH:i:sP");
141 12
            if ($hour === false) {
142
                $backend_format = FieldTypeUtils::getEnvVar("BE_DATE_FORMAT", "Y-m-d");
143
            }
144
            $filter = date($backend_format, $time);
145 12
            $filterString .= $filter;
146
        } else {
147
            $filterString .= $fieldvalue;
148 4
        }
149 4
    }
150 4
151
    /**
152
     * It composes filtering string to be used with api rest services
153 12
     */
154 12
    protected function filterByApiBuilder(): ?String
155 12
    {
156 12
        $filterString = null;
157 12
        $filtro = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $filtro is dead and can be removed.
Loading history...
158
        $prefiltro = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $prefiltro is dead and can be removed.
Loading history...
159
        foreach ($this->prefiltri as $key => $prefiltro) {
160 12
            $this->prefiltri[$key]['prefiltro'] = true;
0 ignored issues
show
Bug Best Practice introduced by
The property prefiltri does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
161
        }
162
        foreach ($this->filtri as $key => $filtro) {
163
            $this->filtri[$key]['prefiltro'] = false;
0 ignored issues
show
Bug Best Practice introduced by
The property filtri does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
164
        }
165
        $tuttifiltri = array_merge($this->filtri, $this->prefiltri);
166
        //$parametribag = array();
167
        if (count($tuttifiltri)) {
168
            $attributeMap = $this->entityname::attributeMap();
169
            $swaggerFormats = $this->entityname::swaggerFormats();
170
            //compose the string
171
            $descrizionefiltri = '';
172
            foreach ($tuttifiltri as $num => $filtrocorrente) {
173
                $nomeCampo = substr($filtrocorrente['nomecampo'], strripos($filtrocorrente['nomecampo'], '.') + 1);
174
                $fieldname = ' '.$nomeCampo;
175
                $filteringValues = $this->getFieldValue($filtrocorrente['valore']);
176
177
178
                $fieldoperator = $this->getOperator($filtrocorrente['operatore']);
179
                $fitrocorrenteqp = 'fitrocorrente'.$num;
180
                $filtronomecampocorrente = $this->findFieldnameByAlias($filtrocorrente['nomecampo']);
181
182
                $criteria = new ParametriQueryTabellaDecoder(
183
                    $fieldname,
184
                    $fieldoperator,
185
                    $filteringValues,
186
                    $fitrocorrenteqp,
187
                    $filtronomecampocorrente
188
                );
189
190
                if (is_array($filteringValues)) {
191
                    $fieldstring =  '( ';
192
                    foreach ($filteringValues as $num => $filterValue) {
0 ignored issues
show
Comprehensibility Bug introduced by
$num is overwriting a variable from outer foreach loop.
Loading history...
193
                        $fieldstring .= $attributeMap[ $nomeCampo ];
194
                        $fieldstring .= ' '.$this->getApiOperator($filtrocorrente['operatore']).' ';
195
                        $fieldvalue = urldecode($filterValue);
196
                        $this->appendFilterString($fieldstring, $swaggerFormats[ $nomeCampo ], $fieldvalue);
197
                        if ($num < count($filteringValues)-1) {
198
                            $fieldstring .= ' OR ';
199
                        }
200
                    }
201
                    $fieldstring .=  ' )';
202
                } else {
203
                    $fieldstring = $attributeMap[ $nomeCampo ];
204
                    $fieldstring .= ' '.$this->getApiOperator($filtrocorrente['operatore']).' ';
205
                    $fieldvalue = urldecode($filteringValues);
0 ignored issues
show
Unused Code introduced by
The assignment to $fieldvalue is dead and can be removed.
Loading history...
206
                    $this->appendFilterString($fieldstring, $swaggerFormats[ $nomeCampo ], $filteringValues);
207
                }
208
209
                if ($filterString != null) {
210
                    $filterString .= ' AND ';
211
                }
212
                $filterString .= $fieldstring;
213
                $this->getDescrizioneFiltro($descrizionefiltri, $filtrocorrente, $criteria);
214
            }
215
            $this->traduzionefiltri = substr($descrizionefiltri, 2);
0 ignored issues
show
Bug Best Practice introduced by
The property traduzionefiltri does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
216
        }
217
        return $filterString;
218
    }
219
220
    /**
221
     * Return the operator to be used
222
     */
223
    private function getApiOperator($operator)
224
    {
225
        switch (strtoupper($operator)) {
226
            case Comparison::CONTAINS:
227
                $operator = '~';
228
                break;
229
            /*case 'IN':
230
                $operator = Comparison::IN;
231
                break;
232
            case 'NOT IN':
233
                $operator = Comparison::NIN;
234
                break;*/
235
            default:
236
                $operator = '=';
237
                break;
238
        }
239
240
        return $operator;
241
    }
242
243
    /**
244
     * Build the ordering string compliant with API REST services
245
     */
246
    protected function orderByApiBuilder(): ?String
247
    {
248
        $attributeMap = $this->entityname::attributeMap();
249
        $orderingString = null;
250
        foreach ($this->colonneordinamento as $nomecampo => $tipoordinamento) {
251
            $fieldname = $attributeMap[ substr($nomecampo, strripos($nomecampo, '.') + 1) ];
252
            $fieldname .= ':'.$tipoordinamento;
253
            if ($orderingString != null) {
254
                $orderingString .= ',';
255
            }
256
            $orderingString .= $fieldname;
257
        }
258
        return $orderingString;
259
    }
260
261
    public function getRecordstabella()
262
    {
263
        //Look for all tables
264
        $qb = $this->biQueryBuilder();
265
266
        if (false === $this->estraituttirecords) {
0 ignored issues
show
Bug introduced by
The property estraituttirecords does not exist on Cdf\BiCoreBundle\Utils\Tabella\TabellaQueryTrait. Did you mean records?
Loading history...
267
            $paginator = new Paginator($qb, true);
268
            $this->righetotali = count($paginator);
0 ignored issues
show
Bug Best Practice introduced by
The property righetotali does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
269
            $this->paginetotali = (int) $this->calcolaPagineTotali($this->getRigheperpagina());
0 ignored issues
show
Bug introduced by
It seems like calcolaPagineTotali() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

269
            $this->paginetotali = (int) $this->/** @scrutinizer ignore-call */ calcolaPagineTotali($this->getRigheperpagina());
Loading history...
Bug introduced by
It seems like getRigheperpagina() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

269
            $this->paginetotali = (int) $this->calcolaPagineTotali($this->/** @scrutinizer ignore-call */ getRigheperpagina());
Loading history...
Bug Best Practice introduced by
The property paginetotali does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
270
            /* imposta l'offset, ovvero il record dal quale iniziare a visualizzare i dati */
271
            $offsetrecords = ($this->getRigheperpagina() * ($this->getPaginacorrente() - 1));
0 ignored issues
show
Bug introduced by
It seems like getPaginacorrente() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

271
            $offsetrecords = ($this->getRigheperpagina() * ($this->/** @scrutinizer ignore-call */ getPaginacorrente() - 1));
Loading history...
272
273
            /* Imposta il limite ai record da estrarre */
274
            if ($this->getRigheperpagina()) {
275
                $qb = $qb->setMaxResults($this->getRigheperpagina());
276
            }
277
            /* E imposta il primo record da visualizzare (per la paginazione) */
278
            if ($offsetrecords) {
279
                $qb = $qb->setFirstResult($offsetrecords);
280
            }
281
            /* Dall'oggetto querybuilder si ottiene la query da eseguire */
282
            $recordsets = $qb->getQuery()->getResult();
283
        } else {
284
            /* Dall'oggetto querybuilder si ottiene la query da eseguire */
285
            $recordsets = $qb->getQuery()->getResult();
286
            $this->righetotali = count($recordsets);
287
            $this->paginetotali = 1;
288
        }
289
290
        $this->records = array();
0 ignored issues
show
Bug Best Practice introduced by
The property records does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
291
        $rigatabellahtml = array();
292
        foreach ($recordsets as $record) {
293
            $this->records[$record->getId()] = $record;
294
            unset($rigatabellahtml);
295
        }
296
297
        return $this->records;
298
    }
299
300
    /**
301
     * Read the API in order to obtains the pages features
302
     */
303
    public function getApiRecordstabella()
304
    {
305
        $newApi = $this->apiController;
306
        $apiController = new $newApi();
307
308
        if (false === $this->estraituttirecords) {
0 ignored issues
show
Bug introduced by
The property estraituttirecords does not exist on Cdf\BiCoreBundle\Utils\Tabella\TabellaQueryTrait. Did you mean records?
Loading history...
309
            $countMethod = $this->apiBook->getCount();
310
            
311
            $count = $apiController->$countMethod();
312
            $this->righetotali = $count;
0 ignored issues
show
Bug Best Practice introduced by
The property righetotali does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
313
            $this->paginetotali = (int) $this->calcolaPagineTotali($this->getRigheperpagina());
0 ignored issues
show
Bug Best Practice introduced by
The property paginetotali does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
314
            /* imposta l'offset, ovvero il record dal quale iniziare a visualizzare i dati */
315
            $offsetrecords = ($this->getRigheperpagina() * ($this->getPaginacorrente() - 1));
316
317
            /*$offset = null, $limit = null, $sort = null, $condition = null*/
318
            $paginationMethod = $this->apiBook->getAll();
319
            //dump($this->filterByApiBuilder());
320
321
            $recordsets = $apiController->$paginationMethod(
322
                $offsetrecords,
323
                $this->getRigheperpagina(),
324
                $this->orderByApiBuilder(),
325
                $this->filterByApiBuilder()
326
            );
327
        //dump($recordsets);
328
        } else {
329
            /* Dall'oggetto querybuilder si ottiene la query da eseguire */
330
            $paginationMethod = $this->apiBook->getAll();
331
            $recordsets = $apiController->$paginationMethod(0, null, $this->orderByApiBuilder(), $this->filterByApiBuilder());
332
            $this->paginetotali = 1;
333
            $this->righetotali = count($recordsets);
334
        }
335
        $this->records = array();
0 ignored issues
show
Bug Best Practice introduced by
The property records does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
336
        $rigatabellahtml = array();
337
        foreach ($recordsets as $record) {
338
            $this->records[$record->getId()] = $record;
339
            unset($rigatabellahtml);
340
        }
341
342
        return $this->records;
343
    }
344
}
345