TabelleUtility   A
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 36
eloc 128
c 2
b 0
f 0
dl 0
loc 201
ccs 112
cts 126
cp 0.8889
rs 9.52

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getListacampitabella() 0 35 5
A creaRecordTabelle() 0 28 6
A scriviDB() 0 16 4
A __construct() 0 3 1
B generaDB() 0 49 8
C listacampitabelladettagli() 0 53 12
1
<?php
2
3
namespace Fi\CoreBundle\DependencyInjection;
4
5
use Exception;
6
use Fi\CoreBundle\Entity\Operatori;
7
use Fi\CoreBundle\Entity\Tabelle;
8
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
9
10
class TabelleUtility
11
{
12
13
    private $container;
14
15 2
    public function __construct(Container $container)
16
    {
17 2
        $this->container = $container;
18 2
    }
19
20 1
    private function listacampitabelladettagli($nometabella, $escludiid, $colonne)
21
    {
22 1
        $risposta = array();
23 1
        if ($escludiid == 1) {
24 1
            $gestionepermessi = $this->container->get("ficorebundle.gestionepermessi");
25 1
            $operatore = $gestionepermessi->utentecorrente();
26 1
            foreach ($colonne as $colonna) {
27 1
                $nomecampo = trim(strtolower($colonna));
28 1
                if (($nomecampo !== 'id') && (strpos($colonna, '_id') === false)) {
29 1
                    $qb = $this->container->get("doctrine")->getRepository("FiCoreBundle:Tabelle")
30 1
                            ->createQueryBuilder('t')
31 1
                            ->where('LOWER(t.nometabella) = :nometabella')
32 1
                            ->andWhere('LOWER(t.nomecampo) = :nomecampo')
33 1
                            ->andWhere('t.operatori_id = :operatori_id')
34 1
                            ->setParameter('nometabella', $nometabella)
35 1
                            ->setParameter('nomecampo', $nomecampo)
36 1
                            ->setParameter('operatori_id', $operatore['id'])
37 1
                            ->getQuery();
38 1
                    $labeltrovata = $qb->getResult();
39 1
                    if (!$labeltrovata) {
40 1
                        $qb = $this->container->get("doctrine")->getRepository("FiCoreBundle:Tabelle")
41 1
                                ->createQueryBuilder('t')
42 1
                                ->where('LOWER(t.nometabella) = :nometabella')
43 1
                                ->andWhere('LOWER(t.nomecampo) = :nomecampo')
44 1
                                ->andWhere('t.operatori_id IS NULL')
45 1
                                ->setParameter('nometabella', $nometabella)
46 1
                                ->setParameter('nomecampo', $nomecampo)
47 1
                                ->getQuery();
48 1
                        $labeltrovata = $qb->getResult();
49 1
                        if (!$labeltrovata) {
50 1
                            $risposta[$colonna] = $colonna;
51
                        } else {
52
                            if (($labeltrovata[0]->getEtichettaindex()) && ($labeltrovata[0]->getEtichettaindex() != '')) {
53
                                $risposta[$colonna] = trim($labeltrovata[0]->getEtichettaindex());
54
                            } else {
55 1
                                $risposta[$colonna] = $colonna;
56
                            }
57
                        }
58
                    } else {
59
                        if (($labeltrovata[0]->getEtichettaindex()) && ($labeltrovata[0]->getEtichettaindex() != '')) {
60
                            $risposta[$colonna] = trim($labeltrovata[0]->getEtichettaindex());
61
                        } else {
62
                            $risposta[$colonna] = $colonna;
63
                        }
64
                    }
65
                }
66
            }
67
        } else {
68
            foreach ($colonne as $colonna) {
69
                $risposta[$colonna] = $colonna;
70
            }
71
        }
72 1
        return $risposta;
73
    }
74
75 1
    public function getListacampitabella($parametri)
76
    {
77 1
        $nometabella = $parametri['nometabella'];
78 1
        $escludiid = $parametri['escludiid'];
79 1
        $em = $this->container->get("doctrine")->getManager();
80 1
        $tableClassName = "";
81 1
        $entityClass = "";
82 1
        $bundles = $this->container->get('kernel')->getBundles();
83 1
        foreach ($bundles as $bundle) {
84 1
            $className = get_class($bundle);
85 1
            $entityClass = substr($className, 0, strrpos($className, '\\'));
86 1
            $tableClassName = '\\' . $entityClass . '\\Entity\\' . $nometabella;
87 1
            if (!class_exists($tableClassName)) {
88 1
                $tableClassName = '';
89 1
                continue;
90
            } else {
91 1
                break;
92
            }
93
        }
94
95 1
        if (!$tableClassName) {
96
            throw new Exception('Entity per la tabella ' . $nometabella . ' non trovata', '-1');
97
        }
98
99 1
        if (!$entityClass) {
100
            throw new Exception('Entity class per la tabella ' . $nometabella . ' non trovata', '-1');
101
        }
102
103 1
        $bundleClass = str_replace('\\', '', $entityClass);
104 1
        $c = $em->getClassMetadata($bundleClass . ':' . $nometabella);
105 1
        $colonne = $c->getColumnNames();
106 1
        $risposta = $this->listacampitabelladettagli($nometabella, $escludiid, $colonne);
107
        //natcasesort($risposta);
108 1
        asort($risposta, SORT_NATURAL | SORT_FLAG_CASE);
109 1
        return $risposta;
110
    }
111
    
112 1
    public function generaDB($parametri)
113
    {
114 1
        if (!isset($parametri['tabella'])) {
115
            return false;
116
        }
117 1
        if (!isset($parametri['namespace'])) {
118
            return false;
119
        }
120 1
        if (!isset($parametri['bundle'])) {
121
            return false;
122
        }
123
124
        //$namespace = $parametri['namespace'];
125 1
        $bundle = $parametri['bundle'];
0 ignored issues
show
Unused Code introduced by
The assignment to $bundle is dead and can be removed.
Loading history...
126
127
        //$nomebundle = $namespace . $bundle . 'Bundle';
128
129 1
        $nometabella = $parametri['tabella'];
130 1
        $em = $this->container->get("doctrine")->getManager();
131
132 1
        $bundles = $this->container->get('kernel')->getBundles();
133 1
        $tableClassName = "";
134 1
        $entityClass = "";
135 1
        foreach ($bundles as $bundle) {
136 1
            $className = get_class($bundle);
137 1
            $entityClass = substr($className, 0, strrpos($className, '\\'));
138 1
            $tableClassName = '\\' . $entityClass . '\\Entity\\' . $nometabella;
139 1
            if (!class_exists($tableClassName)) {
140 1
                $tableClassName = '';
141 1
                continue;
142
            } else {
143 1
                break;
144
            }
145
        }
146
147 1
        if (!$tableClassName) {
148
            throw new Exception('Entity per la tabella ' . $nometabella . ' non trovata', '-1');
149
        }
150
151 1
        if (!$entityClass) {
152
            throw new Exception('Entity class per la tabella ' . $nometabella . ' non trovata', '-1');
153
        }
154
155 1
        $bundleClass = str_replace('\\', '', $entityClass);
156
157 1
        $c = $em->getClassMetadata($bundleClass . ':' . $nometabella);
158
159 1
        $colonne = $c->getColumnNames();
160 1
        $this->scriviDB($colonne, $nometabella, $parametri);
161 1
    }
162
163 1
    private function scriviDB($colonne, $nometabella, $parametri)
164
    {
165 1
        foreach ($colonne as $colonna) {
166
            $vettorericerca = array(
167 1
                'nometabella' => $nometabella,
168 1
                'nomecampo' => $colonna,
169
            );
170
171 1
            if (isset($parametri['operatore'])) {
172 1
                $vettorericerca['operatori_id'] = $parametri['operatore'];
173
            }
174
175 1
            $trovato = $this->container->get("doctrine")->getRepository('FiCoreBundle:Tabelle')->findBy($vettorericerca, array());
176
177 1
            if (empty($trovato)) {
178 1
                $this->creaRecordTabelle($nometabella, $colonna, $vettorericerca, $parametri);
179
            }
180
        }
181 1
    }
182
183 1
    private function creaRecordTabelle($nometabella, $colonna, $vettorericerca, $parametri)
184
    {
185 1
        $crea = new Tabelle();
186 1
        $crea->setNometabella($nometabella);
187 1
        $crea->setNomecampo($colonna);
188
189 1
        if (isset($parametri['operatore'])) {
190 1
            $idOperatore = $parametri['operatore'];
191 1
            $creaoperatore = $this->container->get("doctrine")->getRepository('FiCoreBundle:Operatori')->find($idOperatore);
192 1
            if ($creaoperatore instanceof Operatori) {
193 1
                $crea->setOperatori($creaoperatore);
194
            }
195
196 1
            $vettorericerca['operatori_id'] = null;
197 1
            $ritrovato = $this->container->get("doctrine")->getRepository('FiCoreBundle:Tabelle')->findOneBy($vettorericerca);
198
199 1
            if (!empty($ritrovato)) {
200 1
                $crea->setMostrastampa($ritrovato->hasMostrastampa() ? true : false);
201 1
                $crea->setMostraindex($ritrovato->hasMostraindex() ? true : false);
202
            }
203
        } else {
204 1
            $crea->setMostrastampa(true);
205 1
            $crea->setMostraindex(true);
206
        }
207
208 1
        $ma = $this->container->get("doctrine")->getManager();
209 1
        $ma->persist($crea);
210 1
        $ma->flush();
211 1
    }
212
}
213