Passed
Push — master ( 58bdb4...b93552 )
by Andrea
11:16 queued 14s
created

getQueryCriteria()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Cdf\BiCoreBundle\Utils\Tabella;
4
5
use DateTime;
6
use Doctrine\Common\Collections\Expr\Comparison;
7
8
class BaseParametriQueryTabellaDecoder
9
{
10
11
    protected $fieldname;
12
    protected $fieldoperator;
13
    protected $fieldvalue;
14
    protected $fieldqueryparameter;
15
    protected $criteria;
16
    protected $parameters;
17
18 2
    public function __construct($fieldname, $fieldoperator, $fieldvalue, $fieldqueryparameter, $fieldinfo)
19
    {
20 2
        $this->fieldname = $fieldname;
21 2
        $this->fieldoperator = $fieldoperator;
22 2
        if (is_string($fieldvalue)) {
23 2
            $this->fieldvalue = urldecode($fieldvalue);
24
        } else {
25 2
            $this->fieldvalue = $fieldvalue;
26
        }
27 2
        $this->fieldqueryparameter = $fieldqueryparameter;
28 2
        $this->fieldinfo = $fieldinfo;
0 ignored issues
show
Bug Best Practice introduced by
The property fieldinfo does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
29 2
        $this->parameters = array();
30 2
        $this->buildQuery();
0 ignored issues
show
introduced by
The method buildQuery() does not exist on Cdf\BiCoreBundle\Utils\T...etriQueryTabellaDecoder. Maybe you want to declare this class abstract? ( Ignorable by Annotation )

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

30
        $this->/** @scrutinizer ignore-call */ 
31
               buildQuery();
Loading history...
31 2
    }
32
33 2
    public function getQueryCriteria()
34
    {
35 2
        return $this->criteria;
36
    }
37
38 2
    public function getQueryParameters()
39
    {
40 2
        return $this->parameters;
41
    }
42
43 1
    public function getDescrizioneFiltro()
44
    {
45 1
        $descrizionevalore = '';
46
47
        switch (true) {
48 1
            case $this->getDescrizioneFiltroIsNull($descrizionevalore):
49 1
                break;
50 1
            case $this->getDescrizioneFiltroDecodifiche($descrizionevalore):
51
                break;
52 1
            case $this->getDescrizioneFiltroBoolean($descrizionevalore):
53
                break;
54 1
            case $this->getDescrizioneFiltroDate($descrizionevalore):
55 1
                break;
56 1
            case $this->getDescrizioneFiltroArray($descrizionevalore):
57 1
                break;
58 1
            case $this->getDescrizioneFiltroString($descrizionevalore):
59 1
                break;
60
            default:
61 1
                $this->getDescrizioneFiltroAltro($descrizionevalore);
62 1
                break;
63
        }
64 1
        $nomecampo = substr($this->fieldname, stripos($this->fieldname, '.') + 1);
65 1
        $filtro = $nomecampo . ' ' . $this->operatorToString($this->fieldoperator) . ' ' . $descrizionevalore;
66
67 1
        return $filtro;
68
    }
69
70 1
    protected function getDescrizioneFiltroAltro(&$descrizionevalore)
71
    {
72 1
        if ('' == $descrizionevalore) {
73 1
            $descrizionevalore = "'" . $this->fieldvalue . "'";
74
        }
75 1
    }
76
77 1
    protected function getDescrizioneFiltroDate(&$descrizionevalore)
78
    {
79 1
        $trovato = false;
80 1
        if ('date' == $this->fieldinfo['tipocampo']) {
81 1
            if (is_a($this->fieldvalue, "\DateTime")) {
82 1
                $descrizionevalore = $this->fieldvalue->format('d/m/Y');
83
            } else {
84 1
                $descrizionevalore = DateTime::createFromFormat('Y-m-d', $this->fieldvalue)->format('d/m/Y');
85
            }
86 1
            $trovato = true;
87
        }
88 1
        if ('datetime' == $this->fieldinfo['tipocampo']) {
89 1
            if (is_a($this->fieldvalue, "\DateTime")) {
90 1
                $descrizionevalore = $this->fieldvalue->format('d/m/Y H:i:s');
91
            } else {
92
                $descrizionevalore = DateTime::createFromFormat('Y-m-d', $this->fieldvalue)->format('d/m/Y');
93
            }
94 1
            $trovato = true;
95
        }
96
97 1
        return $trovato;
98
    }
99
100 1
    protected function getDescrizioneFiltroString(&$descrizionevalore)
101
    {
102 1
        $trovato = false;
103 1
        if (is_string($this->fieldvalue)) {
104 1
            $descrizionevalore = $descrizionevalore = "'" . $this->fieldvalue . "'";
105 1
            $trovato = true;
106
        }
107
108 1
        return $trovato;
109
    }
110
111 1
    protected function getDescrizioneFiltroDecodifiche(&$descrizionevalore)
112
    {
113 1
        $trovato = false;
114 1
        if (isset($this->fieldinfo['decodifiche'])) {
115
            $decodifiche = $this->fieldinfo['decodifiche'];
116
            if ($decodifiche) {
117
                if (is_array($this->fieldvalue)) {
118
                    foreach ($this->fieldvalue as $value) {
119
                        $descrizionevalore = $descrizionevalore . "'" . $decodifiche[$value] . "', ";
120
                    }
121
                } else {
122
                    if (isset($decodifiche[$this->fieldvalue])) {
123
                        $descrizionevalore = $descrizionevalore = "'" . $decodifiche[$this->fieldvalue] . "'";
124
                    } else {
125
                        $descrizionevalore = $descrizionevalore = "'" . $this->fieldvalue . "'";
126
                    }
127
                }
128
                $trovato = true;
129
            }
130
        }
131
132 1
        return $trovato;
133
    }
134
135 1
    protected function getDescrizioneFiltroIsNull(&$descrizionevalore)
136
    {
137 1
        $trovato = false;
138 1
        if (is_null($this->fieldvalue)) {
139 1
            $descrizionevalore = '(vuoto)';
140 1
            $trovato = true;
141
        }
142
143 1
        return $trovato;
144
    }
145
146 1
    protected function getDescrizioneFiltroBoolean(&$descrizionevalore)
147
    {
148 1
        $trovato = false;
149 1
        if (is_bool($this->fieldvalue)) {
150
            $descrizionevalore = $this->fieldvalue ? 'SI' : 'NO';
151
            $trovato = true;
152
        }
153
154 1
        return $trovato;
155
    }
156
157 1
    protected function getDescrizioneFiltroArray(&$descrizionevalore)
158
    {
159 1
        $trovato = false;
160 1
        if (is_array($this->fieldvalue)) {
161 1
            foreach ($this->fieldvalue as $value) {
162 1
                if (is_numeric($value)) {
163 1
                    $descrizionevalore = $descrizionevalore . ', ' . $value;
164
                } else {
165
                    $descrizionevalore = $descrizionevalore . "'" . $value . "', ";
166
                }
167 1
                $trovato = true;
168
            }
169 1
            $descrizionevalore = substr($descrizionevalore, 0, -2);
170
        }
171
172 1
        return $trovato;
173
    }
174
175 1
    protected function operatorToString($operator)
176
    {
177
        $decoder = array(
178 1
            Comparison::LT => 'minore di',
179 1
            Comparison::LTE => 'minore o uguale di',
180 1
            Comparison::GT => 'maggiore di',
181 1
            Comparison::GTE => 'maggiore o uguale di',
182 1
            Comparison::CONTAINS => 'contiene',
183 1
            Comparison::STARTS_WITH => 'inizia con',
184 1
            Comparison::ENDS_WITH => 'finisce con',
185 1
            Comparison::IN => 'compreso tra',
186 1
            Comparison::NIN => 'non compreso tra',
187 1
            Comparison::EQ => 'uguale a',
188 1
            Comparison::NEQ => 'diverso da',
189
        );
190
191 1
        return $decoder[$operator];
192
    }
193
}
194