Passed
Push — master ( 38225e...e98f88 )
by Andrea
10:10
created

TabelleUtility   A

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