Passed
Push — master ( 88d705...44ae42 )
by Andrea
12:16
created

GrigliaRegoleUtils::campiesclusi()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 5.0729

Importance

Changes 0
Metric Value
cc 5
eloc 18
nc 5
nop 1
dl 0
loc 31
ccs 18
cts 21
cp 0.8571
crap 5.0729
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Fi\CoreBundle\DependencyInjection;
4
5
use Fi\CoreBundle\Controller\GestionepermessiController;
0 ignored issues
show
Bug introduced by
The type Fi\CoreBundle\Controller...tionepermessiController was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Fi\CoreBundle\Controller\FiUtilita;
7
8
class GrigliaRegoleUtils
9
{
10
11 1
    public static function getTipoRegola(&$tipo, &$regola, $parametri)
12
    {
13 1
        $doctrine = $parametri['doctrine'];
14 1
        $nometabella = $parametri['nometabella'];
15 1
        $entityName = $parametri['entityName'];
16 1
        $bundle = $parametri['bundle'];
17
18 1
        $elencocampi = $doctrine->getClassMetadata($entityName)->getFieldNames();
19
20 1
        if (strrpos($regola['field'], '.') == 0) {
21 1
            if (in_array($regola['field'], $elencocampi) === true) {
22 1
                $type = $doctrine->getClassMetadata($entityName)->getFieldMapping($regola['field']);
23 1
                $tipo = $type['type'];
24
25
                //Si aggiunge l'alias al campo altrimenti da Doctrine2 fallisce la query
26 1
                $regola['field'] = $nometabella . '.' . $regola['field'];
27 1
            }
28 1
        } else {
29
            //Altrimenti stiamo analizzando il campo di una tabella in leftjoin pertanto si cercano le informazioni sul tipo
30
            //dei campi nella tabella "joinata"
31 1
            $tablejoined = substr($regola['field'], 0, strrpos($regola['field'], '.'));
32 1
            $fieldjoined = substr($regola['field'], strrpos($regola['field'], '.') + 1);
33
34 1
            $entityNametablejoined = $bundle . ':' . $tablejoined;
35
36 1
            $type = $doctrine->getClassMetadata($entityNametablejoined)->getFieldMapping($fieldjoined);
37 1
            $tipo = $type['type'];
38
        }
39 1
    }
40
41 1
    public static function getRegolaPerData(&$regola)
42
    {
43 1
        if (isset($regola) && count($regola) > 0) {
44 1
            if ((substr($regola['data'], 0, 1) == "'") && (substr($regola['data'], strlen($regola['data']) - 1, 1) == "'")) {
45
                $regola['data'] = substr($regola['data'], 1, strlen($regola['data']) - 2);
46
            }
47 1
        }
48 1
    }
49
50 1
    public static function setRegole(&$q, &$primo, $parametri = array())
0 ignored issues
show
Unused Code introduced by
The parameter $primo is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

50
    public static function setRegole(&$q, /** @scrutinizer ignore-unused */ &$primo, $parametri = array())

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
51
    {
52 1
        $regole = $parametri['regole'];
53 1
        $tipof = $parametri['tipof'];
54 1
        $tipo = null;
55 1
        foreach ($regole as $regola) {
56
            //Se il campo non ha il . significa che è necessario aggiungere il nometabella
57 1
            self::getTipoRegola($tipo, $regola, $parametri);
58
59 1
            $regola = self::setSingolaRegola($tipo, $regola);
60 1
            if (!$regola) {
61
                continue;
62
            }
63 1
            if ($tipof == 'OR') {
64
                $condizioneOR = $regola['field'] . ' ' .
65
                        GrigliaUtils::$decodificaop[$regola['op']] . ' ' .
66
                        GrigliaUtils::$precarattere[$regola['op']] .
67
                        str_replace("'", "''", $regola['data']) .
68
                        GrigliaUtils::$postcarattere[$regola['op']];
69
                $q->orWhere($condizioneOR);
70
            } else {
71 1
                $condizioneAND = $regola['field'] . ' ' .
72 1
                        GrigliaUtils::$decodificaop[$regola['op']] . ' ' .
73 1
                        GrigliaUtils::$precarattere[$regola['op']] .
74 1
                        str_replace("'", "''", $regola['data']) .
75 1
                        GrigliaUtils::$postcarattere[$regola['op']];
76 1
                $q->andWhere($condizioneAND);
77
            }
78 1
        }
79 1
    }
80
81 1
    public static function setSingolaRegola($tipo, $regola)
82
    {
83 1
        if (!$tipo || $tipo == "integer" || $tipo == "float") {
84 1
            GrigliaUtils::setVettoriPerNumero();
85 1
        }
86 1
        if ($tipo == 'date' || $tipo == 'datetime') {
87 1
            GrigliaUtils::setVettoriPerData();
88 1
            $regola['data'] = FiUtilita::data2db($regola['data']);
89 1
        } elseif ($tipo == 'string') {
90 1
            GrigliaUtils::setVettoriPerStringa();
91 1
            $regola['field'] = 'lower(' . $regola['field'] . ')';
92 1
        }
93 1
        if ($tipo == 'boolean') {
94
            self::setTipoBoolean($regola, $tipo);
95
        }
96
97 1
        self::getRegolaPerData($regola);
98
99 1
        return $regola;
100
    }
101
102
    public static function setTipoBoolean(&$regola, $tipo)
103
    {
104
        if ($regola['data'] === 'false' || $regola['data'] === false) {
105
            GrigliaUtils::setVettoriPerBoolean();
106
            $regola['op'] = 'eq';
107
            $regola['data'] = "0";
108
        }
109
        if ($regola['data'] === 'true' || $regola['data'] === true) {
110
            GrigliaUtils::setVettoriPerBoolean();
111
            $regola['op'] = 'eq';
112
            $regola['data'] = "1";
113
        }
114
        if ($tipo == 'boolean' && $regola['data'] == 'null') {
115
            $regola = array();
116
        }
117
    }
118
119 13
    public static function campiesclusi($parametri = array())
120
    {
121 13
        if (!isset($parametri['nometabella'])) {
122
            return false;
123
        }
124
125 13
        $output = GrigliaParametriUtils::getOuputType($parametri);
126
127 13
        $nometabella = $parametri['nometabella'];
128
129 13
        $doctrine = GrigliaParametriUtils::getDoctrineByEm($parametri);
130 13
        $doctrineficore = GrigliaParametriUtils::getDoctrineFiCoreByEm($parametri, $doctrine);
131 13
        $container = $parametri['container'];
132
133 13
        $gestionepermessi = $container->get("ficorebundle.gestionepermessi");
134 13
        $operatorecorrente = $gestionepermessi->utentecorrente();
135
136 13
        $escludi = array();
137 13
        $q = GrigliaUtils::getUserCustomTableFields($doctrineficore, $nometabella, $operatorecorrente);
138 13
        if (!$q) {
139 9
            return $escludi;
140
        }
141
142 4
        foreach ($q as $riga) {
143 4
            $campo = GrigliaUtils::getCampiEsclusi($riga, $output);
144 4
            if ($campo) {
145
                $escludi[] = $campo;
146
            }
147 4
        }
148
149 4
        return $escludi;
150
    }
151
}
152