Tracciato::generateXML()   B
last analyzed

Complexity

Conditions 7
Paths 15

Size

Total Lines 65
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 5
Bugs 2 Features 0
Metric Value
c 5
b 2
f 0
dl 0
loc 65
ccs 0
cts 47
cp 0
rs 7.1439
cc 7
eloc 39
nc 15
nop 0
crap 56

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Padosoft\TesseraSanitaria;
3
4
/**
5
 * Class Tracciato
6
 * @package Padosoft\TesseraSanitaria
7
 */
8
class Tracciato
9
{
10
    use traits\Errorable;
11
12
    private $strXML = "";
13
    private $codiceRegione = "";
14
    private $codiceAsl = "";
15
    private $codiceSSA = "";
16
    private $cfProprietario = "";
17
    private $pIva = "";
18
    private $arrSpesa = array();
19
    private $arrVociSpesa = array();
20
    private $objValidateHelper;
21
    private $objCleanHelper;
22
    private $objCryptoHelper;
23
24
    /**
25
     * Tracciato constructor.
26
     *
27
     * @param ValidateHelper $objValidateHelper
28
     * @param CleanHelper    $objCleanHelper
29
     * @param CryptoHelper   $objCryptoHelper
30
     */
31
    public function __construct(
32
        ValidateHelper $objValidateHelper,
33
        CleanHelper $objCleanHelper,
34
        CryptoHelper $objCryptoHelper
35
    ) {
36
        $this->objValidateHelper = $objValidateHelper;
37
        $this->objCleanHelper = $objCleanHelper;
38
        $this->objCryptoHelper = $objCryptoHelper;
39
    }
40
41
    /**
42
     * @return bool
43
     */
44
    public function getResult()
45
    {
46
        if (!is_array($this->getArrErrors())) {
47
            return true;
48
        }
49
        return (!$this->hasErrors());
50
    }
51
52
    /**
53
     * @param $codiceRegione
54
     * @param $codiceAsl
55
     * @param $codiceSSA
56
     * @param $cfProprietario
57
     * @param $pIva
58
     * @param $arrSpesa
59
     * @param $arrVociSpesa
60
     *
61
     * @return bool
62
     */
63
    public function doTracciato(
64
        $codiceRegione,
65
        $codiceAsl,
66
        $codiceSSA,
67
        $cfProprietario,
68
        $pIva,
69
        $arrSpesa,
70
        $arrVociSpesa
71
    ) {
72
        $this->resetVarTracciato();
73
74
        $this->codiceRegione = $codiceRegione;
75
        $this->codiceAsl = $codiceAsl;
76
        $this->codiceSSA = $codiceSSA;
77
        $this->cfProprietario = $cfProprietario;
78
        $this->pIva = $pIva;
79
        $this->arrSpesa = $arrSpesa;
80
        $this->arrVociSpesa = $arrVociSpesa;
81
82
        $this->validateParamTracciato();
83
        if (!$this->getResult()) {
84
            return false;
85
        }
86
87
        $this->strXML = $this->generateXML();
88
        if (!$this->getResult()) {
89
            return false;
90
        }
91
92
        return true;
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function getXml()
99
    {
100
        if (empty($this->strXML)) {
101
            $this->strXML = "";
102
        }
103
104
        return $this->strXML;
105
    }
106
107
    /**
108
     * @param int $numTab
109
     *
110
     * @return string
111
     */
112
    private function addTab($numTab = 1)
113
    {
114
        $numTab = (int)$numTab;
115
        $str = "";
116
        for ($i = 0; $i < $numTab; $i++) {
117
            $str .= "    ";
118
        }
119
        return $str;
120
    }
121
122
    /**
123
     *
124
     */
125
    private function resetVarTracciato()
126
    {
127
        $this->strXML = "";
128
        $this->resetErrors();
129
        $this->objValidateHelper->resetErrors();
130
        $this->objCryptoHelper->resetErrors();
131
132
        $this->codiceRegione = "";
133
        $this->codiceAsl = "";
134
        $this->codiceSSA = "";
135
        $this->cfProprietario = "";
136
        $this->pIva = "";
137
        $this->arrSpesa = array();
138
        $this->arrVociSpesa = array();
139
    }
140
141
    /**
142
     *
143
     */
144
    private function validateParamTracciato()
145
    {
146
        $this->objValidateHelper->checkCodiceRegione($this->codiceRegione);
147
        $this->objValidateHelper->checkCodiceAsl($this->codiceAsl);
148
        $this->objValidateHelper->checkCodiceSSA($this->codiceSSA);
149
        $this->objValidateHelper->checkCfProprietario($this->cfProprietario);
150
        $this->objValidateHelper->checkPIva($this->pIva);
151
        $this->objValidateHelper->checkArrSpesa($this->arrSpesa);
152
        $this->objValidateHelper->checkArrVociSpesa($this->arrVociSpesa);
153
154
        $this->addArrErrors($this->objValidateHelper->getArrErrors());
155
    }
156
157
    /**
158
     * @return string
159
     */
160
    private function generateXML()
161
    {
162
        // Cifra e rende il risultato in base64 per essere mostrato nell'XML
163
        $cfencrypted = $this->encrypt($this->cfProprietario, 'cfProprietario');
164
        if ($cfencrypted == '') {
165
            return '';
166
        }
167
168
        // Testata: dati proprietario (prima di <proprietario>, rimossi campi opzionali di esempio <opzionale1>text</opzionale1><opzionale2>text</opzionale2><opzionale3>text</opzionale3>)
169
        $xml = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
170
        $xml .= '<precompilata xsi:noNamespaceSchemaLocation="730_precompilata.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' . PHP_EOL;
171
172
        $xml .= $this->addTab(1) . '<proprietario>' . PHP_EOL;
173
        $xml .= $this->addTab(2) . '<codiceRegione>' . $this->objCleanHelper->clean($this->codiceRegione) . '</codiceRegione>' . PHP_EOL;
174
        $xml .= $this->addTab(2) . '<codiceAsl>' . $this->objCleanHelper->clean($this->codiceAsl) . '</codiceAsl>' . PHP_EOL;
175
        $xml .= $this->addTab(2) . '<codiceSSA>' . $this->objCleanHelper->clean($this->codiceSSA) . '</codiceSSA>' . PHP_EOL;
176
        $xml .= $this->addTab(2) . '<cfProprietario>' . $cfencrypted . '</cfProprietario>' . PHP_EOL;
177
        $xml .= $this->addTab(1) . '</proprietario>' . PHP_EOL;
178
179
        // Documento fiscale: dati identificativi della ricevuta/scontrino
180
        foreach ($this->arrSpesa as $key => $rigaSpesa) {
181
            $xml .= $this->addTab(1) . '<documentoSpesa>' . PHP_EOL;
182
183
            $flagOperazione = $rigaSpesa['flagOperazione'];
184
185
            // Rimborso
186
            if ($flagOperazione == FlagOperazione::RIMBORSO) {
187
                $xml .= $this->addTab(2) . '<idRimborso>' . PHP_EOL;
188
                $xml .= $this->getSpesaOrRimborsoContent($rigaSpesa);
189
                $xml .= $this->addTab(2) . '</idRimborso>' . PHP_EOL;
190
            }
191
192
            // Dati ricevuta/scontrino
193
            $cfCittadinoEncrypted = $this->encrypt($rigaSpesa['cfCittadino'], 'cfCittadino');
194
195
            $xml .= $this->addTab(2) . '<idSpesa>' . PHP_EOL;
196
            $xml .= $this->getSpesaOrRimborsoContent($rigaSpesa);
197
            $xml .= $this->addTab(2) . '</idSpesa>' . PHP_EOL;
198
199
            $xml .= $this->addTab(2) . '<dataPagamento>' . $this->objCleanHelper->clean($rigaSpesa['dataPagamento']) . '</dataPagamento>' . PHP_EOL;
200
            $xml .= $this->addTab(2) . '<flagPagamentoAnticipato>' . $this->objCleanHelper->clean($rigaSpesa['flagPagamentoAnticipato']) . '</flagPagamentoAnticipato>' . PHP_EOL;
201
            $xml .= $this->addTab(2) . '<flagOperazione>' . $this->objCleanHelper->clean($rigaSpesa['flagOperazione']) . '</flagOperazione>' . PHP_EOL;
202
            $xml .= $this->addTab(2) . '<cfCittadino>' . $cfCittadinoEncrypted . '</cfCittadino>' . PHP_EOL;
203
204
            // Singole voci della ricevuta/scontrino
205
            foreach ($this->arrVociSpesa as $rigaVociSpesa) {
206
                if (!empty($rigaVociSpesa[$key]['tipoSpesa'])) {
207
                    $xml .= $this->addTab(2) . '<voceSpesa>' . PHP_EOL;
208
                    $xml .= $this->addTab(3) . '<tipoSpesa>' . $this->objCleanHelper->clean($rigaVociSpesa[$key]['tipoSpesa']) . '</tipoSpesa>' . PHP_EOL;
209
                    $xml .= $this->addTab(3) . '<flagTipoSpesa>' . $this->objCleanHelper->clean($rigaVociSpesa[$key]['flagTipoSpesa']) . '</flagTipoSpesa>' . PHP_EOL;
210
                    $xml .= $this->addTab(3) . '<importo>' . $this->objCleanHelper->clean($rigaVociSpesa[$key]['importo']) . '</importo>' . PHP_EOL;
211
                    $xml .= $this->addTab(2) . '</voceSpesa>' . PHP_EOL;
212
                }
213
            }
214
215
            $xml .= $this->addTab(1) . '</documentoSpesa>' . PHP_EOL;
216
        }
217
        $xml .= '</precompilata>' . PHP_EOL;
218
219
        if (!$this->getResult()) {
220
            $xml = '';
221
        }
222
223
        return $xml;
224
    }
225
226
    /**
227
     * @param $cfProprietario
228
     * @param $cfProprietarioName
229
     *
230
     * @return string
231
     */
232
    private function encrypt($cfProprietario, $cfProprietarioName)
233
    {
234
        $cfencrypted = base64_encode($this->objCryptoHelper->rsaEncrypt($cfProprietario));
235
        if ($this->hasErrors()) {
236
            $this->addError("Errore di criptazione openssl sul $cfProprietarioName: " . $this->cfProprietario);
237
            $this->addArrErrors($this->objCryptoHelper->getArrErrors());
238
        }
239
        return $cfencrypted;
240
    }
241
242
    /**
243
     * @param $rigaSpesa
244
     *
245
     * @return string
246
     */
247
    private function getSpesaOrRimborsoContent($rigaSpesa)
248
    {
249
        $xml = '';
250
        $xml .= $this->addTab(3) . '<pIva>' . $this->objCleanHelper->clean($this->pIva) . '</pIva>' . PHP_EOL;
251
        $xml .= $this->addTab(3) . '<dataEmissione>' . $this->objCleanHelper->clean($rigaSpesa['dataEmissione']) . '</dataEmissione>' . PHP_EOL;
252
        $xml .= $this->addTab(3) . '<numDocumentoFiscale>' . PHP_EOL;
253
        $xml .= $this->addTab(4) . '<dispositivo>' . $this->objCleanHelper->clean($rigaSpesa['dispositivo']) . '</dispositivo>' . PHP_EOL;
254
        $xml .= $this->addTab(4) . '<numDocumento>' . $this->objCleanHelper->clean($rigaSpesa['numDocumento']) . '</numDocumento>' . PHP_EOL;
255
        $xml .= $this->addTab(3) . '</numDocumentoFiscale>' . PHP_EOL;
256
        return $xml;
257
    }
258
}
259