Completed
Push — master ( d3ae50...489be2 )
by Roberto
12s
created

src/Auxiliar/Response.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace NFePHP\MDFe\Auxiliar;
4
5
/**
6
 * Classe auxiliar com funções de DOM extendidas
7
 *
8
 * @category  NFePHP
9
 * @package   NFePHP\MDFe\Auxiliar\Response
10
 * @copyright Copyright (c) 2008-2015
11
 * @license   http://www.gnu.org/licenses/lesser.html LGPL v3
12
 * @author    Roberto L. Machado <linux.rlm at gmail dot com>
13
 * @link      http://github.com/nfephp-org/nfephp for the canonical source repository
14
 */
15
16
use NFePHP\Common\Dom\Dom;
17
18
class Response
19
{
20
    /**
21
     * readReturnSefaz
22
     * Trata o retorno da SEFAZ devolvendo o resultado em um array
23
     *
24
     * @param  string $method
25
     * @param  string $xmlResp
26
     * @param  mixed  $parametro
27
     * @return array
28
     */
29
    public static function readReturnSefaz($method, $xmlResp)
30
    {
31
        $dom = new Dom('1.0', 'utf-8');
32
        $dom->loadXMLString($xmlResp);
33
        //para cada $method tem um formato de retorno especifico
34
        switch ($method) {
35
            case 'MDFeRecepcao':
36
                return self::zReadRecepcaoLote($dom);
37
                break;
38
            case 'MDFeRetRecepcao':
39
                return self::zReadRetRecepcao($dom);
40
                break;
41
            case 'MDFeConsultaSituacao':
42
                return self::zReadConsultaMDF($dom);
43
                break;
44
            case 'MDFeStatusServico':
45
                return self::zReadStatusServico($dom);
46
                break;
47
            case 'MDFeRecepcaoEvento':
48
                return self::zReadRecepcaoEvento($dom);
49
                break;
50
            case 'MDFeConsNaoEnc':
51
                return self::zReadConsNaoEnc($dom);
52
                break;
53
        }
54
        return array();
55
    }
56
57
    /**
58
     * zReadRecepcaoLote
59
     *
60
     * @param  DOMDocument $dom
61
     * @return boolean
62
     */
63 View Code Duplication
    protected static function zReadRecepcaoLote($dom)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        //retorno da funçao
66
        $aResposta = array(
67
            'bStat' => false,
68
            'versao' => '',
69
            'tpAmb' => '',
70
            'cUF' => '',
71
            'cStat' => '',
72
            'verAplic' => '',
73
            'xMotivo' => '',
74
            'dhRecbto' => '',
75
            'tMed' => '',
76
            'nRec' => ''
77
        );
78
        $tag = $dom->getNode('retEnviMDFe');
79
        if (empty($tag)) {
80
            return $aResposta;
81
        }
82
        $infRec = $dom->getNode('infRec');
83
        $aResposta = array(
84
            'bStat' => true,
85
            'versao' => $tag->getAttribute('versao'),
86
            'tpAmb' => $dom->getValue($tag, 'tpAmb'),
87
            'cUF' => $dom->getValue($tag, 'cUF'),
88
            'cStat' => $dom->getValue($tag, 'cStat'),
89
            'verAplic' => $dom->getValue($tag, 'verAplic'),
90
            'xMotivo' => $dom->getValue($tag, 'xMotivo'),
91
            'dhRecbto' => $dom->getValue($infRec, 'dhRecbto'),
92
            'tMed' => $dom->getValue($infRec, 'tMed'),
93
            'nRec' => $dom->getValue($infRec, 'nRec')
94
        );
95
        return $aResposta;
96
    }
97
98
    /**
99
     * zReadRetRecepcao
100
     *
101
     * @param  DOMDocument $dom
102
     * @return array
103
     */
104 View Code Duplication
    protected static function zReadRetRecepcao($dom)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106
        //retorno da funçao
107
        $aResposta = array(
108
            'bStat'=>false,
109
            'versao' => '',
110
            'tpAmb' => '',
111
            'verAplic' => '',
112
            'cStat' => '',
113
            'xMotivo' => '',
114
            'cUF' => '',
115
            'nRec' => '',
116
            'aProt' => array()
117
        );
118
        $tag = $dom->getNode('retConsReciMDFe');
119
        if (empty($tag)) {
120
            return $aResposta;
121
        }
122
        $aResposta = array(
123
            'bStat'=>true,
124
            'versao' => $tag->getAttribute('versao'),
125
            'tpAmb' => $dom->getValue($tag, 'tpAmb'),
126
            'verAplic' => $dom->getValue($tag, 'verAplic'),
127
            'cStat' => $dom->getValue($tag, 'cStat'),
128
            'xMotivo' => $dom->getValue($tag, 'xMotivo'),
129
            'nRec' => $dom->getValue($tag, 'nRec'),
130
            'cUF' => $dom->getValue($tag, 'tpAmb'),
131
            'aProt' => self::zGetProt($dom, $tag)
132
        );
133
        return $aResposta;
134
    }
135
136
    /**
137
     * zReadConsultaMDF
138
     *
139
     * @param  DOMDocument $dom
140
     * @return string
141
     */
142
    protected static function zReadConsultaMDF($dom)
143
    {
144
        //retorno da funçao
145
        $aResposta = array(
146
            'bStat' => false,
147
            'versao' => '',
148
            'tpAmb' => '',
149
            'verAplic' => '',
150
            'cStat' => '',
151
            'xMotivo' => '',
152
            'cUF' => '',
153
            'aProt' => array(),
154
            'aEvent' => array()
155
        );
156
        $tag = $dom->getNode('retConsSitMDFe');
157
        if (! isset($tag)) {
158
            return $aResposta;
159
        }
160
        $aEvent = array();
161
        $procEventoMDFe = $tag->getElementsByTagName('procEventoMDFe');
162
        if (isset($procEventoMDFe)) {
163
            foreach ($procEventoMDFe as $evento) {
164
                $aEvent[] = self::zGetEvent($dom, $evento);
165
            }
166
        }
167
        $aResposta = array(
168
            'bStat' => true,
169
            'versao' => $tag->getAttribute('versao'),
170
            'tpAmb' => $dom->getValue($tag, 'tpAmb'),
171
            'verAplic' => $dom->getValue($tag, 'verAplic'),
172
            'cStat' => $dom->getValue($tag, 'cStat'),
173
            'xMotivo' => $dom->getValue($tag, 'xMotivo'),
174
            'cUF' => $dom->getValue($tag, 'cUF'),
175
            'aProt' => self::zGetProt($dom, $tag),
176
            'aEvent' => $aEvent
177
        );
178
        return $aResposta;
179
    }
180
181
    /**
182
     * zReadStatusServico
183
     *
184
     * @param  DOMDocument $dom
185
     * @return string|boolean
186
     */
187 View Code Duplication
    protected static function zReadStatusServico($dom)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
188
    {
189
        //retorno da funçao
190
        $aResposta = array(
191
            'bStat' => false,
192
            'versao' => '',
193
            'cStat' => '',
194
            'verAplic' => '',
195
            'xMotivo' => '',
196
            'dhRecbto' => '',
197
            'tMed' => '',
198
            'cUF' => '',
199
            'dhRetorno' => '',
200
            'xObs' => ''
201
        );
202
        $tag = $dom->getNode('retConsStatServMDFe');
203
        if (empty($tag)) {
204
            return $aResposta;
205
        }
206
        $aResposta = array(
207
            'bStat' => true,
208
            'versao' => $tag->getAttribute('versao'),
209
            'cStat' => $dom->getValue($tag, 'cStat'),
210
            'verAplic' => $dom->getValue($tag, 'verAplic'),
211
            'xMotivo' => $dom->getValue($tag, 'xMotivo'),
212
            'dhRecbto' => $dom->getValue($tag, 'dhRecbto'),
213
            'tMed' => $dom->getValue($tag, 'tMed'),
214
            'cUF' => $dom->getValue($tag, 'cUF'),
215
            'dhRetorno' => $dom->getValue($tag, 'dhRetorno'),
216
            'xObs' => $dom->getValue($tag, 'xObs')
217
        );
218
        return $aResposta;
219
    }
220
221
    /**
222
     * zReadRecepcaoEvento
223
     *
224
     * @param  DOMDocument $dom
225
     * @return string
226
     */
227 View Code Duplication
    protected static function zReadRecepcaoEvento($dom)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
228
    {
229
        //retorno da funçao
230
        $aResposta = array(
231
            'bStat' => false,
232
            'versao' => '',
233
            'verAplic' => '',
234
            'tpAmb' => '',
235
            'id' => '',
236
            'cOrgao' => '',
237
            'cStat' => '',
238
            'xMotivo' => '',
239
            'aEvent' => array()
240
        );
241
        $tag = $dom->getNode('retEventoMDFe');
242
        if (! isset($tag)) {
243
            return $aResposta;
244
        }
245
        $aResposta = array(
246
            'bStat' => true,
247
            'versao' => $tag->getAttribute('versao'),
248
            'id' => $dom->getValue($tag, 'id'),
249
            'tpAmb' => $dom->getValue($tag, 'tpAmb'),
250
            'verAplic' => $dom->getValue($tag, 'verAplic'),
251
            'cOrgao' => $dom->getValue($tag, 'cOrgao'),
252
            'cStat' => $dom->getValue($tag, 'cStat'),
253
            'xMotivo' => $dom->getValue($tag, 'xMotivo'),
254
            'aEvent' => self::zGetEvent($dom, $tag)
255
        );
256
        return $aResposta;
257
    }
258
259
    /**
260
     * zReadConsNaoEnc
261
     *
262
     * @param  DOMDocument $dom
263
     * @return boolean
264
     */
265
    protected static function zReadConsNaoEnc($dom)
266
    {
267
        //retorno da funçao
268
        $aResposta = array(
269
            'bStat' => false,
270
            'versao' => '',
271
            'verAplic' => '',
272
            'tpAmb' => '',
273
            'cStat' => '',
274
            'xMotivo' => '',
275
            'cUF' => '',
276
            'MDFe' => array()
277
        );
278
        $tag = $dom->getNode('retConsMDFeNaoEnc');
279
        if (empty($tag)) {
280
            return $aResposta;
281
        }
282
        $lista = $tag->getElementsByTagName('infMDFe');
283
        $aMDFe = array();
284
        if (isset($lista)) {
285
            foreach ($lista as $infMDFe) {
286
                $aMDFe[] = array(
287
                    'chMDFe' => $dom->getValue($infMDFe, 'chMDFe'),
288
                    'nProt' => $dom->getValue($infMDFe, 'chMDFe')
289
                );
290
            }
291
        }
292
        $aResposta = array(
293
            'bStat' => true,
294
            'versao' => $tag->getAttribute('versao'),
295
            'verAplic' => $dom->getValue($tag, 'verAplic'),
296
            'tpAmb' => $dom->getValue($tag, 'tpAmb'),
297
            'cStat' => $dom->getValue($tag, 'cStat'),
298
            'xMotivo' => $dom->getValue($tag, 'xMotivo'),
299
            'cUF' => $dom->getValue($tag, 'cUF'),
300
            'MDFe' => $aMDFe
301
        );
302
        return $aResposta;
303
    }
304
305
    /**
306
     * zGetProt
307
     *
308
     * @param  DOMDocument $dom
309
     * @param  DOMDocument $tag
310
     * @return array
311
     */
312
    private static function zGetProt($dom, $tag)
313
    {
314
        $aProt = array();
315
        $protMDFe = $tag->getElementsByTagName('protMDFe')->item(0);
316
        $infProt = $dom->getNode('infProt');
317
        if (empty($infProt)) {
318
            return $aProt;
319
        }
320
        $aProt = array(
321
            'versao' => $protMDFe->getAttribute('versao'),
322
            'tpAmb' => $dom->getValue($infProt, 'tpAmb'),
323
            'verAplic' => $dom->getValue($infProt, 'verAplic'),
324
            'chMDFe' => $dom->getValue($infProt, 'chMDFe'),
325
            'dhRecbto' => $dom->getValue($infProt, 'dhRecbto'),
326
            'nProt' => $dom->getValue($infProt, 'nProt'),
327
            'digVal' => $dom->getValue($infProt, 'digVal'),
328
            'cStat' => $dom->getValue($infProt, 'cStat'),
329
            'xMotivo' => $dom->getValue($infProt, 'xMotivo')
330
        );
331
        return $aProt;
332
    }
333
334
    /**
335
     * zGetEvent
336
     *
337
     * @param  DOMDocument $dom
338
     * @param  DOMDocument $tag
339
     * @return array
340
     */
341
    private static function zGetEvent($dom, $tag)
342
    {
343
        $aEvent = array();
344
        $infEvento = $tag->getElementsByTagName('infEvento')->item(0);
345
        if (! empty($infEvento)) {
346
            $aEvent = array(
347
                'chMDFe' => $dom->getValue($infEvento, 'chMDFe'),
348
                'tpEvento' => $dom->getValue($infEvento, 'tpEvento'),
349
                'xEvento' => $dom->getValue($infEvento, 'xEvento'),
350
                'nSeqEvento' => $dom->getValue($infEvento, 'nSeqEvento'),
351
                'dhRegEvento' => $dom->getValue($infEvento, 'dhRegEvento'),
352
                'nProt' => $dom->getValue($infEvento, 'nProt')
353
            );
354
        }
355
        return $aEvent;
356
    }
357
}
358