Completed
Push — master ( 2ee6a7...b572ab )
by Andrea
08:32 queued 52s
created

TabelleController   B

Complexity

Total Complexity 48

Size/Duplication

Total Lines 418
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 46.3%

Importance

Changes 0
Metric Value
wmc 48
c 0
b 0
f 0
lcom 1
cbo 9
dl 0
loc 418
ccs 119
cts 257
cp 0.463
rs 8.4864

10 Methods

Rating   Name   Duplication   Size   Complexity  
B aggiornaAction() 0 49 5
A getRequestValue() 0 8 3
B configuraAction() 0 88 1
B generaDB() 0 45 6
D scriviDB() 0 45 9
B grigliapopupAction() 0 28 1
A grigliaAction() 0 7 1
B setParametriGriglia() 0 32 4
B listacampitabellaAction() 0 47 6
C listacampitabelladettagli() 0 56 12

How to fix   Complexity   

Complex Class

Complex classes like TabelleController 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 TabelleController, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Fi\CoreBundle\Controller;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\HttpFoundation\Response;
7
use Symfony\Component\HttpFoundation\JsonResponse;
8
use Fi\CoreBundle\Entity\Tabelle;
9
10
/**
11
 * Tabelle controller.
12
 */
13
class TabelleController extends FiCoreController
14
{
15
16
    public function aggiornaAction(Request $request)
17
    {
18
        if ($request->get('oper') == 'edit') {
19
            $gestionepermessi = $this->get("ficorebundle.gestionepermessi");
20
            $operatore = $gestionepermessi->utentecorrente();
21
22
            $id = $request->get('id');
23
24
            $em = $this->getDoctrine()->getManager();
25
            $tabelle = $em->getRepository('FiCoreBundle:Tabelle')->find($id);
26
            if (!$tabelle) {
27
                throw new AccessDeniedException("Oggetto non trovato");
28
            }
29
            $tabelle->setOperatoriId($operatore['id']);
30
            $nometabella = $this->getRequestValue($request, 'nometabella');
31
            if ($nometabella) {
32
                $tabelle->setNometabella($nometabella);
33
            }
34
            $nomecampo = $this->getRequestValue($request, 'nomecampo');
35
            if ($nomecampo) {
36
                $tabelle->setNomecampo($nomecampo);
37
            }
38
            $mostraindex = $this->getRequestValue($request, 'mostraindex');
39
            $tabelle->setMostraindex($mostraindex);
40
            $ordineindex = $this->getRequestValue($request, 'ordineindex');
41
            $tabelle->setOrdineindex($ordineindex);
42
            $etichettaindex = $this->getRequestValue($request, 'etichettaindex');
43
            $tabelle->setEtichettaindex($etichettaindex);
44
45
            $larghezzaindex = $this->getRequestValue($request, 'larghezzaindex');
46
            $tabelle->setLarghezzaindex($larghezzaindex);
47
48
            $mostrastampa = $this->getRequestValue($request, 'mostrastampa');
49
            $tabelle->setMostrastampa($mostrastampa);
50
51
            $ordinestampa = $this->getRequestValue($request, 'ordinestampa');
52
            $tabelle->setOrdinestampa($ordinestampa);
53
54
            $etichettastampa = $this->getRequestValue($request, 'etichettastampa');
55
            $tabelle->setEtichettastampa($etichettastampa);
56
57
            $larghezzastampa = $this->getRequestValue($request, 'larghezzastampa');
58
            $tabelle->setLarghezzastampa($larghezzastampa);
59
            $em->persist($tabelle);
60
            $em->flush();
61
        }
62
63
        return new Response('OK');
64
    }
65
66
    private function getRequestValue($request, $attribute)
67
    {
68
        if (($request->get($attribute) !== null) && ($request->get($attribute) !== '')) {
69
            return $request->get($attribute);
70
        } else {
71
            return null;
72
        }
73
    }
74
75 1
    public function configuraAction(Request $request, $nometabella)
76
    {
77 1
        $this->setup($request);
78 1
        $gestionepermessi = $this->get("ficorebundle.gestionepermessi");
79 1
        $operatore = $gestionepermessi->utentecorrente();
80 1
        $this->generaDB(array('tabella' => $nometabella), $request);
81 1
        $this->generaDB(array('tabella' => $nometabella, 'operatore' => $operatore['id']), $request);
82
83 1
        $namespace = $this->getNamespace();
84 1
        $bundle = $this->getBundle();
85 1
        $controller = $this->getController();
86 1
        $container = $this->container;
87
88 1
        $nomebundle = $namespace . $bundle . 'Bundle';
89
90 1
        $em = $this->getDoctrine()->getManager();
91 1
        $entities = $em->getRepository($nomebundle . ':' . $controller)->findAll();
92
93
        $dettaglij = array(
94 1
            'nomecampo' => array(
95
                array('nomecampo' => 'nomecampo', 'lunghezza' => '150', 'descrizione' => 'Campo', 'tipo' => 'text', 'editable' => false),),
96
            'mostraindex' => array(
97
                array('nomecampo' => 'mostraindex', 'lunghezza' => '100', 'descrizione' => 'Vedi in griglia', 'tipo' => 'boolean'),
98
            ),
99
            'ordineindex' => array(
100
                array('nomecampo' => 'ordineindex', 'lunghezza' => '100', 'descrizione' => 'Ordine in griglia', 'tipo' => 'text'),
101
            ),
102
            'etichettaindex' => array(
103
                array('nomecampo' => 'etichettaindex', 'lunghezza' => '150', 'descrizione' => 'Label in griglia', 'tipo' => 'text'),
104
            ),
105
            'larghezzaindex' => array(
106
                array('nomecampo' => 'larghezzaindex', 'lunghezza' => '100', 'descrizione' => 'Largh. in griglia', 'tipo' => 'text'),
107
            ),
108
            'mostrastampa' => array(
109
                array('nomecampo' => 'mostrastampa', 'lunghezza' => '100', 'descrizione' => 'Vedi in stampa', 'tipo' => 'boolean'),
110
            ),
111
            'ordinestampa' => array(
112
                array('nomecampo' => 'ordinestampa', 'lunghezza' => '100', 'descrizione' => 'Ordine in stampa', 'tipo' => 'text'),
113
            ),
114
            'etichettastampa' => array(
115
                array('nomecampo' => 'etichettastampa', 'lunghezza' => '150', 'descrizione' => 'Label in stampa', 'tipo' => 'text'),
116
            ),
117
            'larghezzastampa' => array(
118
                array('nomecampo' => 'larghezzastampa', 'lunghezza' => '100', 'descrizione' => 'Largh. in stampa', 'tipo' => 'text'),
119
            ),
120
        );
121
122
        $paricevuti = array(
123 1
            'doctrine' => $em,
124 1
            'nomebundle' => $nomebundle,
125 1
            'nometabella' => $controller,
126 1
            'dettaglij' => $dettaglij,
127 1
            'container' => $container,
128
        );
129
130 1
        $paricevuti['escludere'] = array('nometabella', 'operatori_id');
131
132 1
        $testata = Griglia::testataPerGriglia($paricevuti);
133
134 1
        $testata['titolo'] = "Configurazione colonne per tabella $nometabella";
135 1
        $testata['multisearch'] = 0;
136 1
        $testata['showdel'] = 0;
137 1
        $testata['showadd'] = 0;
138 1
        $testata['showedit'] = 0;
139 1
        $testata['showprint'] = 0;
140 1
        $testata['editinline'] = 1;
141 1
        $testata['nomelist'] = '#listconfigura';
142 1
        $testata['nomepager'] = '#pagerconfigura';
143 1
        $testata['tastochiudi'] = 1;
144 1
        $testata['div'] = '#dettaglioconf';
145 1
        $testata['chiamante'] = $nometabella;
146 1
        $testata['percorsogriglia'] = $nometabella . '/grigliapopup';
147 1
        $testata['altezzagriglia'] = '300';
148 1
        $testata['larghezzagriglia'] = '900';
149
150 1
        $testata['permessiedit'] = 1;
151 1
        $testata['permessidelete'] = 1;
152 1
        $testata['permessicreate'] = 1;
153 1
        $testata['permessiread'] = 1;
154
        $twigparm = array(
155 1
            'entities' => $entities,
156 1
            'nomecontroller' => $controller,
157 1
            'testata' => json_encode($testata),
158 1
            'chiamante' => $nometabella,
159
        );
160
161 1
        return $this->render($nomebundle . ':' . $controller . ':configura.html.twig', $twigparm);
162
    }
163
164 1
    public function generaDB($parametri, Request $request)
165
    {
166 1
        $this->setup($request);
167 1
        if (!isset($parametri['tabella'])) {
168
            return false;
169
        }
170
171 1
        $namespace = $this->getNamespace();
172 1
        $bundle = $this->getBundle();
173
174 1
        $nomebundle = $namespace . $bundle . 'Bundle';
175
176 1
        $nometabella = $parametri['tabella'];
177 1
        $em = $this->getDoctrine()->getManager();
178
179 1
        $bundles = $this->get('kernel')->getBundles();
180 1
        $tableClassName = "";
181 1
        $entityClass = "";
182 1
        foreach ($bundles as $bundle) {
183 1
            $className = get_class($bundle);
184 1
            $entityClass = substr($className, 0, strrpos($className, '\\'));
185 1
            $tableClassName = '\\' . $entityClass . '\\Entity\\' . $nometabella;
186 1
            if (!class_exists($tableClassName)) {
187 1
                $tableClassName = '';
188 1
                continue;
189
            } else {
190 1
                break;
191
            }
192
        }
193
194 1
        if (!$tableClassName) {
195
            throw new \Exception('Entity per la tabella ' . $nometabella . ' non trovata', '-1');
196
        }
197
198 1
        if (!$entityClass) {
199
            throw new \Exception('Entity class per la tabella ' . $nometabella . ' non trovata', '-1');
200
        }
201
202 1
        $bundleClass = str_replace('\\', '', $entityClass);
203
204 1
        $c = $em->getClassMetadata($bundleClass . ':' . $nometabella);
205
206 1
        $colonne = $c->getColumnNames();
207 1
        $this->scriviDB($colonne, $nometabella, $nomebundle, $parametri);
208 1
    }
209
210 1
    private function scriviDB($colonne, $nometabella, $nomebundle, $parametri)
211
    {
212 1
        foreach ($colonne as $colonna) {
213
            $vettorericerca = array(
214 1
                'nometabella' => $nometabella,
215 1
                'nomecampo' => $colonna,
216
            );
217
218 1
            if (isset($parametri['operatore'])) {
219 1
                $vettorericerca['operatori_id'] = $parametri['operatore'];
220
            }
221
222 1
            $trovato = $this->getDoctrine()->getRepository($nomebundle . ':Tabelle')->findBy($vettorericerca, array());
223
224 1
            if (empty($trovato)) {
225 1
                $crea = new Tabelle();
226 1
                $crea->setNometabella($nometabella);
227 1
                $crea->setNomecampo($colonna);
228
229 1
                if (isset($parametri['operatore'])) {
230 1
                    $creaoperatore = $this->getDoctrine()->getRepository($nomebundle . ':Operatori')->find($parametri['operatore']);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $creaoperatore is correct as $this->getDoctrine()->ge...parametri['operatore']) (which targets Doctrine\Common\Persiste...bjectRepository::find()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
231 1
                    if (!$creaoperatore) {
232
                        continue;
233
                    }
234 1
                    $crea->setOperatori($creaoperatore);
235
236 1
                    unset($vettorericerca['operatori_id']);
237 1
                    $vettorericerca['operatori_id'] = null;
238 1
                    $ritrovato = $this->getDoctrine()->getRepository($nomebundle . ':Tabelle')->findOneBy($vettorericerca);
239
240 1
                    if (!empty($ritrovato)) {
241 1
                        $crea->setMostrastampa($ritrovato->hasMostrastampa() ? true : false);
242 1
                        $crea->setMostraindex($ritrovato->hasMostraindex() ? true : false);
243
                    }
244
                } else {
245 1
                    $crea->setMostrastampa(true);
246 1
                    $crea->setMostraindex(true);
247
                }
248
249 1
                $ma = $this->getDoctrine()->getManager();
250 1
                $ma->persist($crea);
251 1
                $ma->flush();
252
            }
253
        }
254 1
    }
255
256
    public function grigliapopupAction(Request $request, $chiamante)
257
    {
258
        $this->setup($request);
259
        $namespace = $this->getNamespace();
260
        $bundle = $this->getBundle();
261
        $controller = $this->getController();
262
263
        $nomebundle = $namespace . $bundle . 'Bundle';
264
        $em = $this->getDoctrine()->getManager();
265
266
        $gestionepermessi = $this->get("ficorebundle.gestionepermessi");
267
        $operatore = $gestionepermessi->utentecorrente();
268
        $tabellej = array();
269
        $tabellej['operatori_id'] = array('tabella' => 'operatori', 'campi' => array('username', 'operatore'));
270
271
        $paricevuti = array(
272
            'request' => $request,
273
            'doctrine' => $em,
274
            'container' => $this->container,
275
            'nomebundle' => $nomebundle,
276
            'nometabella' => $controller,
277
            'tabellej' => $tabellej,);
278
279
        $paricevuti['escludere'] = array('nometabella', 'operatori_id');
280
        $paricevuti['precondizioni'] = array('Tabelle.nometabella' => $chiamante, 'Tabelle.operatori_id' => $operatore['id']);
281
282
        return new Response(Griglia::datiPerGriglia($paricevuti));
283
    }
284
285
    public function grigliaAction(Request $request)
286
    {
287
        $this->setParametriGriglia(array('request' => $request));
288
        $paricevuti = self::$parametrigriglia;
289
290
        return new Response(Griglia::datiPerGriglia($paricevuti));
291
    }
292
293 1
    protected function setParametriGriglia($prepar = array())
294
    {
295 1
        $this->setup($prepar['request']);
296 1
        $namespace = $this->getNamespace();
297 1
        $bundle = $this->getBundle();
298 1
        $controller = $this->getController();
299
300 1
        $gestionepermessi = $this->get("ficorebundle.gestionepermessi");
301 1
        $canRead = ($gestionepermessi->leggere(array('modulo' => $controller)) ? 1 : 0);
302 1
        if (!$canRead) {
303
            throw new AccessDeniedException("Non si hanno i permessi per visualizzare questo contenuto");
304
        }
305
306 1
        $nomebundle = $namespace . $bundle . 'Bundle';
307 1
        $tabellej = array();
308 1
        $tabellej['operatori_id'] = array('tabella' => 'operatori', 'campi' => array('username'));
309 1
        $escludi = array("operatori"); //'operatori_id'
310
311
        $paricevuti = array(
312 1
            'container' => $this->container,
313 1
            'nomebundle' => $nomebundle,
314 1
            'nometabella' => $controller,
315 1
            'tabellej' => $tabellej,
316 1
            'escludere' => $escludi
317
        );
318
319 1
        if (!empty($prepar)) {
320 1
            $paricevuti = array_merge($paricevuti, $prepar);
321
        }
322
323 1
        self::$parametrigriglia = $paricevuti;
324 1
    }
325
326
    public function listacampitabellaAction(Request $request)
327
    {
328
        $this->setup($request);
329
        $namespace = $this->getNamespace();
330
        $bundle = $this->getBundle();
331
        $controller = $this->getController();
332
        $nomebundle = $namespace . $bundle . 'Bundle';
333
334
        $nometabella = trim($request->get('tabella'));
335
        if (!isset($nometabella)) {
336
            return false;
337
        }
338
339
        $escludiid = $request->get('escludiid');
340
        if (!isset($escludiid)) {
341
            $escludiid = 0;
342
        }
343
344
        $em = $this->getDoctrine()->getManager();
345
346
        $bundles = $this->get('kernel')->getBundles();
347
        foreach ($bundles as $bundle) {
348
            $className = get_class($bundle);
349
            $entityClass = substr($className, 0, strrpos($className, '\\'));
350
            $tableClassName = '\\' . $entityClass . '\\Entity\\' . $nometabella;
351
            if (!class_exists($tableClassName)) {
352
                $tableClassName = '';
353
                continue;
354
            } else {
355
                break;
356
            }
357
        }
358
359
        if (!$tableClassName) {
0 ignored issues
show
Bug introduced by
The variable $tableClassName does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
360
            throw new \Exception('Entity per la tabella ' . $nometabella . ' non trovata', '-1');
361
        }
362
363
        $bundleClass = str_replace('\\', '', $entityClass);
0 ignored issues
show
Bug introduced by
The variable $entityClass does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
364
        $c = $em->getClassMetadata($bundleClass . ':' . $nometabella);
365
        $colonne = $c->getColumnNames();
366
367
        $risposta = $this->listacampitabelladettagli($escludiid, $colonne, $nomebundle, $controller);
368
        //natcasesort($risposta);
369
        asort($risposta, SORT_NATURAL | SORT_FLAG_CASE);
370
371
        return new JsonResponse($risposta);
372
    }
373
374
    private function listacampitabelladettagli($escludiid, $colonne, $nomebundle, $controller)
375
    {
376
        $risposta = array();
377
        $nometabella = $controller;
378
        if ($escludiid == 1) {
379
            $gestionepermessi = $this->get("ficorebundle.gestionepermessi");
380
            $operatore = $gestionepermessi->utentecorrente();
381
            foreach ($colonne as $colonna) {
382
                $nomecampo = trim(strtolower($colonna));
383
                if (($nomecampo !== 'id') && (strpos($colonna, '_id') === false)) {
384
                    $qb = $this->getDoctrine()->getRepository("$nomebundle:$controller")
385
                            ->createQueryBuilder('t')
386
                            ->where('LOWER(t.nometabella) = :nometabella')
387
                            ->andWhere('LOWER(t.nomecampo) = :nomecampo')
388
                            ->andWhere('t.operatori_id = :operatori_id')
389
                            ->setParameter('nometabella', $nometabella)
390
                            ->setParameter('nomecampo', $nomecampo)
391
                            ->setParameter('operatori_id', $operatore['id'])
392
                            ->getQuery();
393
                    $labeltrovata = $qb->getResult();
394
                    if (!$labeltrovata) {
395
                        $qb = $this->getDoctrine()->getRepository("$nomebundle:$controller")
396
                                ->createQueryBuilder('t')
397
                                ->where('LOWER(t.nometabella) = :nometabella')
398
                                ->andWhere('LOWER(t.nomecampo) = :nomecampo')
399
                                ->andWhere('t.operatori_id IS NULL')
400
                                ->setParameter('nometabella', $nometabella)
401
                                ->setParameter('nomecampo', $nomecampo)
402
                                ->getQuery();
403
                        $labeltrovata = $qb->getResult();
404
                        if (!$labeltrovata) {
405
                            $risposta[$colonna] = $colonna;
406
                        } else {
407
                            if (($labeltrovata[0]->getEtichettaindex()) && ($labeltrovata[0]->getEtichettaindex() != '')) {
408
                                $risposta[$colonna] = trim($labeltrovata[0]->getEtichettaindex());
409
                            } else {
410
                                $risposta[$colonna] = $colonna;
411
                            }
412
                        }
413
                    } else {
414
                        if (($labeltrovata[0]->getEtichettaindex()) && ($labeltrovata[0]->getEtichettaindex() != '')) {
415
                            $risposta[$colonna] = trim($labeltrovata[0]->getEtichettaindex());
416
                        } else {
417
                            $risposta[$colonna] = $colonna;
418
                        }
419
                    }
420
                }
421
            }
422
        } else {
423
            foreach ($colonne as $colonna) {
424
                $risposta[$colonna] = $colonna;
425
            }
426
        }
427
428
        return $risposta;
429
    }
430
}
431