TabellaOpzioniTrait   A
last analyzed

Complexity

Total Complexity 37

Size/Duplication

Total Lines 224
Duplicated Lines 0 %

Test Coverage

Coverage 99.12%

Importance

Changes 0
Metric Value
eloc 102
dl 0
loc 224
ccs 112
cts 113
cp 0.9912
rs 9.44
c 0
b 0
f 0
wmc 37

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setLarghezzaColonneTabella() 0 10 5
A setOrdinaColonneTabella() 0 11 3
A getLarghezzaColonneTabellaTotalePercentuale() 0 10 3
A getLarghezzaColonneTabellaPercentualeFinale() 0 10 2
A setOpzioniTabellaDefault() 0 12 4
A getAllOpzioniTabella() 0 13 2
A bonificaNomeCampo() 0 13 3
A elaboraJoin() 0 30 5
A getOpzionitabellaCampiExtra() 0 17 3
B elaboraColonneOpzioniTabellaMancanti() 0 17 7
1
<?php
2
3
namespace Cdf\BiCoreBundle\Utils\Tabella;
4
5
use Cdf\BiCoreBundle\Utils\Arrays\ArrayUtils;
6
use Cdf\BiCoreBundle\Utils\Entity\EntityUtils;
7
use function count;
8
9
trait TabellaOpzioniTrait
10
{
11
    use TabellaOpzioniFromCoreTrait, TabellaOpzioniFromModelloColonneTrait;
12
13
    /**
14
     *
15
     * @return array<mixed>
16
     */
17 12
    protected function getAllOpzioniTabella() : array
18
    {
19 12
        $opzionibuilder = array();
20 12
        foreach ($this->colonnedatabase as $colonnadatabase) {
21
            // Inserire dati da definizione entity
22 12
            $this->setOpzioniTabellaDefault($colonnadatabase, $opzionibuilder, null, false);
23
        }
24 12
        $this->setOpzioniTabellaFromCore($opzionibuilder);
25 12
        $this->setOpzioniTabellaFromModellocolonne($opzionibuilder);
26 12
        $this->setOrdinaColonneTabella($opzionibuilder);
27 12
        $this->setLarghezzaColonneTabella($opzionibuilder);
28
29 12
        return $opzionibuilder;
30
    }
31
32
    /**
33
     *
34
     * @param string $campo
35
     * @param array<mixed> $modellocolonna
36
     * @param array<mixed> $opzionibuilder
37
     * @return void
38
     */
39 5
    private function getOpzionitabellaCampiExtra(string $campo, array $modellocolonna, array &$opzionibuilder) : void
40
    {
41 5
        if ((isset($modellocolonna['campoextra']) && true == $modellocolonna['campoextra'])) {
42 1
            $opzionibuilder[$campo] = array(
43 1
                'tipocampo' => $modellocolonna['tipocampo'],
44 1
                'nomecampo' => $campo,
45 1
                'nometabella' => $modellocolonna['nometabella'],
46 1
                'entityclass' => null,
47 1
                'sourceentityclass' => null,
48 1
                'ordine' => null,
49 1
                'etichetta' => $campo,
50 1
                'larghezza' => 5,
51 1
                'editabile' => false,
52 1
                'campoextra' => true,
53 1
                'association' => null,
54 1
                'associationtable' => null,
55 1
                'escluso' => false,
56 1
            );
57
        }
58
    }
59
60
    /**
61
     *
62
     * @param array<mixed> $infoentity
63
     * @param array<mixed> $opzionibuilder
64
     * @param string|null $jointable
65
     * @param bool $ricursione
66
     * @param array<mixed> $ancestors
67
     * @return void
68
     */
69 12
    protected function setOpzioniTabellaDefault($infoentity, &$opzionibuilder, $jointable = null, $ricursione = false, $ancestors = []) : void
70
    {
71 12
        $nometabella = ((isset($jointable)) ? $jointable : $this->tablename);
72 12
        if (!in_array($nometabella, $ancestors)) {
73 12
            $ancestors[] = $nometabella;
74
        }
75 12
        $nomecolonna = ucfirst(implode('.', $ancestors)).'.'.$infoentity['fieldName'];
76
77 12
        $this->elaboraColonneOpzioniTabellaMancanti($opzionibuilder, $infoentity, $nometabella, $nomecolonna, $ricursione);
78
79 12
        if (isset($infoentity['association'])) {
80 6
            $this->elaboraJoin($opzionibuilder, $infoentity, $ancestors);
81
        }
82
    }
83
84
    /**
85
     *
86
     * @param array<mixed> $opzionibuilder
87
     * @param array<mixed> $colonnadatabase
88
     * @param string $nometabella
89
     * @param string $nomecolonna
90
     * @param bool $ricursione
91
     * @return void
92
     */
93 12
    private function elaboraColonneOpzioniTabellaMancanti(&$opzionibuilder, $colonnadatabase, $nometabella, $nomecolonna, $ricursione) : void
94
    {
95 12
        $opzionibuilder[$nomecolonna] = array(
96 12
            'tipocampo' => isset($colonnadatabase['association']) ? 'join' : $colonnadatabase['type'],
97 12
            'nomecampo' => $nomecolonna,
98 12
            'nometabella' => $nometabella,
99 12
            'entityclass' => $colonnadatabase['entityClass'],
100 12
            'sourceentityclass' => isset($colonnadatabase['sourceEntityClass']) ? $colonnadatabase['sourceEntityClass'] : null,
101 12
            'ordine' => null,
102 12
            'etichetta' => ucfirst($colonnadatabase['columnName']),
103 12
            'larghezza' => 10,
104 12
            'editabile' => true,
105 12
            'campoextra' => false,
106 12
            'association' => isset($colonnadatabase['association']) ? $colonnadatabase['association'] : false,
107 12
            'associationtable' => isset($colonnadatabase['associationtable']) ? $colonnadatabase['associationtable'] : null,
108 12
            'decodifiche' => null,
109 12
            'escluso' => (true === $ricursione ? true : ('_id' == substr($colonnadatabase['fieldName'], -3) ? true : false)),
110 12
        );
111
    }
112
113
    /**
114
     *
115
     * @param array<mixed> $opzionibuilder
116
     * @return int
117
     */
118 12
    private function getLarghezzaColonneTabellaTotalePercentuale(array $opzionibuilder) : int
119
    {
120 12
        $larghezzatotalepercentuale = 1;
121 12
        foreach ($opzionibuilder as $opzione) {
122 12
            if (false === $opzione['escluso']) {
123 12
                $larghezzatotalepercentuale += $opzione['larghezza'];
124
            }
125
        }
126
127 12
        return $larghezzatotalepercentuale;
128
    }
129
130 12
    private function getLarghezzaColonneTabellaPercentualeFinale() : int
131
    {
132
        // il 5% si lascia per la ruzzolina in fondo alla riga, il 3% si lascia per il checkbox in testa alla riga,
133
        // quindi per le colonne dati resta il 92%
134 12
        $percentualefinale = 95; // il 5% si lascia per la ruzzolina in fondo
135 12
        if (true === $this->getTabellaParameter('multiselezione')) {
0 ignored issues
show
Bug introduced by
It seems like getTabellaParameter() 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

135
        if (true === $this->/** @scrutinizer ignore-call */ getTabellaParameter('multiselezione')) {
Loading history...
136
            $percentualefinale -= 3;
137
        }
138
139 12
        return $percentualefinale;
140
    }
141
142
    /**
143
     *
144
     * @param array<mixed> $opzionibuilder
145
     * @return void
146
     */
147 12
    private function setLarghezzaColonneTabella(array &$opzionibuilder) : void
148
    {
149 12
        $larghezzatotalepercentuale = $this->getLarghezzaColonneTabellaTotalePercentuale($opzionibuilder);
150 12
        $percentualefinale = $this->getLarghezzaColonneTabellaPercentualeFinale();
151 12
        $percentualerelativatotale = $percentualefinale * 100 / $larghezzatotalepercentuale;
152 12
        if (0 != $percentualefinale - $larghezzatotalepercentuale) {
153 12
            foreach ($opzionibuilder as $key => $opzione) {
154 12
                if (false === $opzione['escluso']) {
155 12
                    $larghezzapercentualericalcolata = ceil($opzione['larghezza'] * $percentualerelativatotale / 100);
156 12
                    $opzionibuilder[$key]['larghezza'] = ($larghezzapercentualericalcolata < 5 ? 5 : $larghezzapercentualericalcolata);
157
                }
158
            }
159
        }
160
    }
161
162
    /**
163
     *
164
     * @param array<mixed> $opzionibuilder
165
     * @param array<mixed> $colonnadatabase
166
     * @param array<mixed> $ancestors
167
     */
168 6
    private function elaboraJoin(array &$opzionibuilder, array $colonnadatabase, array $ancestors) : void
169
    {
170 6
        $entitycollegata = $colonnadatabase['associationtable']['targetEntity'];
171 6
        $utils = new EntityUtils($this->em);
172 6
        $tablecollegataname = $this->em->getClassMetadata($entitycollegata)->getTableName();
173 6
        $colonnecollegate = $utils->getEntityColumns($entitycollegata);
174 6
        foreach ($colonnecollegate as $colonnacorrente) {
175 6
            if (!isset($colonnacorrente['type'])) {
176 5
                $this->setOpzioniTabellaDefault($colonnacorrente, $opzionibuilder, $tablecollegataname, true, $ancestors);
177 5
                continue;
178
            }
179
180 6
            if (!in_array($tablecollegataname, $ancestors)) {
181 6
                $ancestors[] = $tablecollegataname;
182
            }
183 6
            $nomecampo = ucfirst(implode('.', $ancestors)).'.'.$colonnacorrente['fieldName'];
184 6
            $opzionibuilder[$nomecampo] = array(
185 6
                'tipocampo' => $colonnacorrente['type'],
186 6
                'nomecampo' => $nomecampo,
187 6
                'nometabella' => $tablecollegataname,
188 6
                'entityclass' => $colonnadatabase['entityClass'],
189 6
                'sourceentityclass' => isset($colonnadatabase['sourceEntityClass']) ? $colonnadatabase['sourceEntityClass'] : null,
190 6
                'ordine' => null,
191 6
                'etichetta' => ucfirst($colonnacorrente['columnName']),
192 6
                'larghezza' => 0,
193 6
                'editabile' => false,
194 6
                'campoextra' => false,
195 6
                'association' => null,
196 6
                'associationtable' => null,
197 6
                'escluso' => true,
198 6
            );
199
        }
200
    }
201
202
    /**
203
     *
204
     * @param array<mixed> $opzionibuilder
205
     * @return void
206
     */
207 12
    protected function setOrdinaColonneTabella(array &$opzionibuilder) : void
208
    {
209 12
        foreach ($opzionibuilder as $key => $opzione) {
210 12
            if (null === $opzione['ordine']) {
211 12
                $newordine = $this->getMaxOrdine() + 10;
0 ignored issues
show
Bug introduced by
It seems like getMaxOrdine() 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

211
                $newordine = $this->/** @scrutinizer ignore-call */ getMaxOrdine() + 10;
Loading history...
212 12
                $opzionibuilder[$key]['ordine'] = $newordine;
213 12
                $this->setMaxOrdine($newordine);
0 ignored issues
show
Bug introduced by
It seems like setMaxOrdine() 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

213
                $this->/** @scrutinizer ignore-call */ 
214
                       setMaxOrdine($newordine);
Loading history...
214
            }
215
        }
216
        // Ordinamento per colonna ordine
217 12
        ArrayUtils::sortMultiAssociativeArray($opzionibuilder, 'ordine', true);
218
    }
219
220 6
    private function bonificaNomeCampo(string $nomecampo) : string
221
    {
222 6
        $parti = explode('.', $nomecampo);
223 6
        $campo = '';
224 6
        for ($index = 0; $index < count($parti); ++$index) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
225 6
            if ($index == count($parti) - 1) {
226 6
                $campo .= '.'.lcfirst($parti[$index]);
227
            } else {
228 6
                $campo .= '.'.ucfirst($parti[$index]);
229
            }
230
        }
231
232 6
        return substr($campo, 1);
233
    }
234
}
235