Passed
Pull Request — master (#921)
by
unknown
10:53 queued 08:00
created

Standardize   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Test Coverage

Coverage 62.9%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 19
eloc 87
dl 0
loc 188
ccs 39
cts 62
cp 0.629
rs 10
c 2
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A toStd() 0 20 3
B whichIs() 0 29 7
A toJson() 0 6 2
A simpleXml() 0 6 2
A __toString() 0 3 1
A toArray() 0 6 2
A getQRCode() 0 12 1
A __construct() 0 3 1
1
<?php
2
3
/**
4
 * Class for identification and convertion of eletronic documents in xml
5
 * for documents used in sped-nfe, sped-esocial, sped-cte, sped-mdfe, etc.
6
 *
7
 * @category  NFePHP
8
 * @package   NFePHP\Common\Standardize
9
 * @copyright NFePHP Copyright (c) 2008-2019
10
 * @license   http://www.gnu.org/licenses/lgpl.txt LGPLv3+
11
 * @license   https://opensource.org/licenses/MIT MIT
12
 * @license   http://www.gnu.org/licenses/gpl.txt GPLv3+
13
 * @author    Roberto L. Machado <linux.rlm at gmail dot com>
14
 * @link      http://github.com/nfephp-org/sped-nfe for the canonical source repository
15
 */
16
17
namespace NFePHP\NFe\Common;
18
19
use NFePHP\Common\Validator;
20
use NFePHP\NFe\Exception\DocumentsException;
21
use stdClass;
22
23
class Standardize
24
{
25
    /**
26
     * @var string
27
     */
28
    private $node = '';
29
    /**
30
     * @var string
31
     */
32
    private $json = '';
33
    /**
34
     * @var string
35
     */
36
    public $key = '';
37
    /**
38
     * @var object
39
     */
40
    private $sxml;
41
    /**
42
     * @var array
43
     */
44
    public $rootTagList = [
45
        'distDFeInt',
46
        'resNFe',
47
        'resEvento',
48
        'envEvento',
49
        'ConsCad',
50
        'consSitNFe',
51
        'consReciNFe',
52
        'downloadNFe',
53
        'enviNFe',
54
        'inutNFe',
55
        'admCscNFCe',
56
        'consStatServ',
57
        'retDistDFeInt',
58
        'retEnvEvento',
59
        'retConsCad',
60
        'retConsSitNFe',
61
        'retConsReciNFe',
62
        'retDownloadNFe',
63
        'retEnviNFe',
64
        'retInutNFe',
65
        'retAdmCscNFCe',
66
        'retConsStatServ',
67
        'procInutNFe',
68
        'procEventoNFe',
69
        'procNFe',
70
        'nfeProc',
71
        'NFe'
72
    ];
73
74
    /**
75
     * Constructor
76
     * @param string $xml
77
     */
78 13
    public function __construct($xml = null)
79
    {
80 13
        $this->toStd($xml);
81 13
    }
82
83
    /**
84
     * Identify node and extract from XML for convertion type
85
     * @param string $xml
86
     * @return string identificated node name
87
     * @throws \InvalidArgumentException
88
     */
89 13
    public function whichIs($xml)
90
    {
91 13
        if (!Validator::isXML($xml)) {
92
            //invalid document is not a XML
93 5
            throw DocumentsException::wrongDocument(6);
94
        }
95 8
        $dom = new \DOMDocument('1.0', 'UTF-8');
96 8
        $dom->preserveWhiteSpace = false;
97 8
        $dom->formatOutput = false;
98 8
        $dom->loadXML($xml);
99 8
        foreach ($this->rootTagList as $key) {
100 8
            $node = !empty($dom->getElementsByTagName($key)->item(0))
101 7
                ? $dom->getElementsByTagName($key)->item(0)
102 8
                : '';
103 8
            if (!empty($node)) {
104 7
                $this->node = $dom->saveXML($node);
0 ignored issues
show
Bug introduced by
It seems like $node can also be of type string; however, parameter $node of DOMDocument::saveXML() does only seem to accept DOMNode|null, maybe add an additional type check? ( Ignorable by Annotation )

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

104
                $this->node = $dom->saveXML(/** @scrutinizer ignore-type */ $node);
Loading history...
105 8
                return $key;
106
            }
107
        }
108 1
        $result = $dom->getElementsByTagName('nfeResultMsg')->item(0);
109 1
        if (!empty($result)) {
110
            $cont = $result->textContent;
111
            if (empty($cont)) {
112
                throw new DocumentsException('O retorno da SEFAZ veio em BRANCO, '
113
                    . 'ou seja devido a um erro ou instabilidade na própria SEFAZ.');
114
            }
115
        }
116
        //documento does not belong to the SPED-NFe project
117 1
        throw DocumentsException::wrongDocument(7);
118
    }
119
120
    /**
121
     * Returns extract node from XML
122
     * @return string
123
     */
124 1
    public function __toString()
125
    {
126 1
        return $this->node;
127
    }
128
129
    /**
130
     * Returns stdClass converted from xml
131
     * @param string $xml
132
     * @return stdClass
133
     */
134 13
    public function toStd($xml = null)
135
    {
136 13
        if (!empty($xml)) {
137 4
            $this->key = $this->whichIs($xml);
138
        }
139 13
        $this->sxml = simplexml_load_string($this->node);
140 13
        $this->json = str_replace(
141 13
            '@attributes',
142 13
            'attributes',
143 13
            json_encode($this->sxml, JSON_PRETTY_PRINT)
144
        );
145
146 13
        $std = json_decode($this->json);
147 13
        if (isset($std->infNFeSupl)) {
148
            $resp = $this->getQRCode();
149
            $std->infNFeSupl->qrCode = $resp['qrCode'];
150
            $std->infNFeSupl->urlChave = $resp['urlChave'];
151
            $this->json = json_encode($std);
152
        }
153 13
        return $std;
154
    }
155
156
    /**
157
     * Return QRCODE and urlChave from XML
158
     * @return array
159
     */
160
    private function getQRCode()
161
    {
162
        $dom = new \DOMDocument('1.0', 'UTF-8');
163
        $dom->preserveWhiteSpace = false;
164
        $dom->formatOutput = false;
165
        $dom->loadXML($this->node);
166
        $node = $dom->getElementsByTagName('infNFeSupl')->item(0);
167
        $resp = [
168
            'qrCode' => $node->getElementsByTagName('qrCode')->item(0)->nodeValue,
169
            'urlChave' => $node->getElementsByTagName('urlChave')->item(0)->nodeValue
170
        ];
171
        return $resp;
172
    }
173
174
    /**
175
     * Returns the SimpleXml Object
176
     * @param string $xml
177
     * @return object
178
     */
179
    public function simpleXml($xml = null)
180
    {
181
        if (!empty($xml)) {
182
            $this->toStd($xml);
183
        }
184
        return $this->sxml;
185
    }
186
187
    /**
188
     * Returns JSON string form XML
189
     * @param string $xml
190
     * @return string
191
     */
192 1
    public function toJson($xml = null)
193
    {
194 1
        if (!empty($xml)) {
195
            $this->toStd($xml);
196
        }
197 1
        return $this->json;
198
    }
199
200
    /**
201
     * Returns array from XML
202
     * @param string $xml
203
     * @return array
204
     */
205 1
    public function toArray($xml = null)
206
    {
207 1
        if (!empty($xml)) {
208
            $this->toStd($xml);
209
        }
210 1
        return json_decode($this->json, true);
211
    }
212
}
213