Passed
Push — master ( 566fb8...876da0 )
by Andrea
08:56 queued 36s
created

TabellaQueryTrait   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Test Coverage

Coverage 95.92%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 93
c 1
b 0
f 0
dl 0
loc 153
ccs 94
cts 98
cp 0.9592
rs 10
wmc 24

5 Methods

Rating   Name   Duplication   Size   Complexity  
A orderByBuilder() 0 7 2
B recursiveJoin() 0 32 9
B buildWhere() 0 48 7
A biQueryBuilder() 0 12 1
A getRecordstabella() 0 36 5
1
<?php
2
3
namespace Cdf\BiCoreBundle\Utils\Tabella;
4
5
use Doctrine\ORM\Tools\Pagination\Paginator;
6
use function count;
7
8
trait TabellaQueryTrait
9
{
10 12
    protected function biQueryBuilder()
11
    {
12 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

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

80
                /** @scrutinizer ignore-call */ 
81
                $alias = $this->findAliasByTablename($tablename);
Loading history...
81 3
                $fieldname = $alias.'.'.(substr($filtrocorrente['nomecampo'], strripos($filtrocorrente['nomecampo'], '.') + 1));
82 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

82
                /** @scrutinizer ignore-call */ 
83
                $fieldvalue = $this->getFieldValue($filtrocorrente['valore']);
Loading history...
83 3
                $fieldoperator = $this->getOperator($filtrocorrente['operatore']);
0 ignored issues
show
Bug introduced by
It seems like getOperator() 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

83
                /** @scrutinizer ignore-call */ 
84
                $fieldoperator = $this->getOperator($filtrocorrente['operatore']);
Loading history...
84 3
                $fitrocorrenteqp = 'fitrocorrente'.$num;
85 3
                $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

85
                /** @scrutinizer ignore-call */ 
86
                $filtronomecampocorrente = $this->findFieldnameByAlias($filtrocorrente['nomecampo']);
Loading history...
86 2
                $criteria = new ParametriQueryTabellaDecoder(
87 2
                    $fieldname,
88 2
                    $fieldoperator,
89 2
                    $fieldvalue,
90 2
                    $fitrocorrenteqp,
91 2
                    $filtronomecampocorrente
92
                );
93
94 2
                $querycriteria = $criteria->getQueryCriteria();
95 2
                $queryparameter = $criteria->getQueryParameters();
96
97 2
                if ($querycriteria) {
98 2
                    $qb->andWhere($querycriteria);
99 2
                    $parametribag = array_merge($queryparameter, $parametribag);
100
                } else {
101 2
                    $qb->andWhere($fieldname.' '.$fieldoperator.' '.":$fitrocorrenteqp");
102 2
                    $parametribag = array_merge(array($fitrocorrenteqp => $fieldvalue), $parametribag);
103
                }
104 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

104
                $this->/** @scrutinizer ignore-call */ 
105
                       getDescrizioneFiltro($descrizionefiltri, $filtrocorrente, $criteria);
Loading history...
105
            }
106 2
            $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...
107
        }
108 12
        $qb->setParameters($parametribag);
109
110 12
        if (isset($this->wheremanuale)) {
111
            $qb->andWhere($this->wheremanuale);
112
        }
113 12
    }
114
115 12
    protected function orderByBuilder(&$qb)
116
    {
117 12
        foreach ($this->colonneordinamento as $nomecampo => $tipoordinamento) {
118 10
            $tablename = substr($nomecampo, 0, strripos($nomecampo, '.'));
119 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

119
            /** @scrutinizer ignore-call */ 
120
            $alias = $this->getAliasGenerato($tablename);
Loading history...
120 10
            $fieldname = $alias.'.'.(substr($nomecampo, strripos($nomecampo, '.') + 1));
121 10
            $qb->addOrderBy($fieldname, $tipoordinamento);
122
        }
123 12
    }
124
125 12
    public function getRecordstabella()
126
    {
127 12
        $qb = $this->biQueryBuilder();
128
129 12
        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...
130 12
            $paginator = new Paginator($qb, true);
131 12
            $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...
132 12
            $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...
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

132
            $this->paginetotali = (int) $this->calcolaPagineTotali($this->/** @scrutinizer ignore-call */ getRigheperpagina());
Loading history...
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

132
            $this->paginetotali = (int) $this->/** @scrutinizer ignore-call */ calcolaPagineTotali($this->getRigheperpagina());
Loading history...
133
            /* imposta l'offset, ovvero il record dal quale iniziare a visualizzare i dati */
134 12
            $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

134
            $offsetrecords = ($this->getRigheperpagina() * ($this->/** @scrutinizer ignore-call */ getPaginacorrente() - 1));
Loading history...
135
136
            /* Imposta il limite ai record da estrarre */
137 12
            if ($this->getRigheperpagina()) {
138 12
                $qb = $qb->setMaxResults($this->getRigheperpagina());
139
            }
140
            /* E imposta il primo record da visualizzare (per la paginazione) */
141 12
            if ($offsetrecords) {
142
                $qb = $qb->setFirstResult($offsetrecords);
143
            }
144
            /* Dall'oggetto querybuilder si ottiene la query da eseguire */
145 12
            $recordsets = $qb->getQuery()->getResult();
146
        } else {
147
            /* Dall'oggetto querybuilder si ottiene la query da eseguire */
148 4
            $recordsets = $qb->getQuery()->getResult();
149 4
            $this->righetotali = count($recordsets);
150 4
            $this->paginetotali = 1;
151
        }
152
153 12
        $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...
154 12
        $rigatabellahtml = array();
155 12
        foreach ($recordsets as $record) {
156 12
            $this->records[$record->getId()] = $record;
157 12
            unset($rigatabellahtml);
158
        }
159
160 12
        return $this->records;
161
    }
162
}
163