Passed
Pull Request — master (#921)
by
unknown
08:01
created

Webservices::__construct()   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
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * Reads and preprocesses WS parameters from xml storage file to json encode or stdClass
5
 *
6
 * @category  NFePHP
7
 * @package   NFePHP\NFe\Common\Webservices
8
 * @copyright NFePHP Copyright (c) 2008-2019
9
 * @license   http://www.gnu.org/licenses/lgpl.txt LGPLv3+
10
 * @license   https://opensource.org/licenses/MIT MIT
11
 * @license   http://www.gnu.org/licenses/gpl.txt GPLv3+
12
 * @author    Roberto L. Machado <linux.rlm at gmail dot com>
13
 * @link      http://github.com/nfephp-org/sped-nfe for the canonical source repository
14
 */
15
16
namespace NFePHP\NFe\Common;
17
18
use SimpleXMLElement;
19
20
class Webservices
21
{
22
    public $json;
23
    public $std;
24
25
    /**
26
     * Constructor
27
     * @param string $xml path or xml content from
28
     *               nfe_ws3_mod55 or nfe_ws3_mod65
29
     */
30 8
    public function __construct($xml)
31
    {
32 8
        $this->toStd($xml);
33 8
    }
34
35
    /**
36
     * Gets webservices parameters for specific conditions
37
     * @param string $sigla
38
     * @param int $amb 1-Produção ou 2-Homologação
39
     * @param int $modelo "55" ou "65"
40
     * @return \stdClass
41
     * @see storage/autorizadores.json
42
     */
43 7
    public function get($sigla, $amb, $modelo)
44
    {
45 7
        $autfile = realpath(__DIR__ . '/../../storage/autorizadores.json');
46 7
        $autorizadores = json_decode(file_get_contents($autfile), true);
47 7
        if (!key_exists($sigla, $autorizadores[$modelo])) {
48 1
            throw new \RuntimeException("Nao existe autorizador [$sigla] para os webservices do modelo [$modelo]");
49
        }
50 6
        $auto = $autorizadores[$modelo][$sigla];
51 6
        if (empty($auto) || empty($this->std)) {
52
            throw new \RuntimeException('Falhou autorizador, parece vazio');
53
        }
54 6
        if (empty($this->std->$auto)) {
55
            throw new \RuntimeException("Nao existem webservices cadastrados para [$sigla] no modelo [$modelo]");
56
        }
57 6
        $ambiente = $amb == 1 ? 'producao' : 'homologacao';
58 6
        $svw = $this->std->$auto->$ambiente;
59 6
        if ($auto == 'SVRS' || $auto == 'SVAN') {
60
            $pad = !empty($this->std->$sigla->$ambiente) ? $this->std->$sigla->$ambiente : '';
61
            $pad = json_decode(json_encode($pad), true);
62
            if (!empty($pad)) {
63
                foreach ($pad as $key => $p) {
64
                    if (empty($svw->$key)) {
65
                        $svw->$key = new \stdClass();
66
                        $svw->$key->method = $p['method'];
67
                        $svw->$key->operation = $p['operation'];
68
                        $svw->$key->version = $p['version'];
69
                        $svw->$key->url = $p['url'];
70
                    }
71
                }
72
            }
73
        }
74 6
        return $svw;
75
    }
76
77
    /**
78
     * Return WS parameters in a stdClass
79
     * @param string $xml
80
     * @return \stdClass
81
     */
82 8
    public function toStd($xml = '')
83
    {
84 8
        if (!empty($xml)) {
85 8
            $this->convert($xml);
86
        }
87 8
        return $this->std;
88
    }
89
90
    /**
91
     * Return WS parameters in json format
92
     * @return string
93
     */
94
    public function __toString()
95
    {
96
        return (string) $this->json;
97
    }
98
99
    /**
100
     * Read WS xml and convert to json and stdClass
101
     * @param string $xml
102
     */
103 8
    protected function convert($xml)
104
    {
105 8
        $resp = simplexml_load_string($xml, null, LIBXML_NOCDATA);
106 8
        $aWS = [];
107 8
        foreach ($resp->children() as $element) {
108 8
            $sigla = (string) $element->sigla;
109 8
            $aWS[$sigla] = [];
110 8
            if (isset($element->homologacao)) {
111 8
                $aWS[$sigla] += $this->extract($element->homologacao, 'homologacao');
112
            }
113 8
            if (isset($element->producao)) {
114 8
                $aWS[$sigla] += $this->extract($element->producao, 'producao');
115
            }
116
        }
117 8
        $this->json = json_encode($aWS);
118 8
        $this->std = json_decode(json_encode($aWS));
119 8
    }
120
121
    /**
122
     * Extract data from wbservices XML strorage to a array
123
     * @param SimpleXMLElement $node
124
     * @param string $environment
125
     * @return array
126
     */
127 8
    protected function extract(SimpleXMLElement $node, $environment)
128
    {
129 8
        $amb[$environment] = [];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$amb was never initialized. Although not strictly required by PHP, it is generally a good practice to add $amb = array(); before regardless.
Loading history...
130 8
        foreach ($node->children() as $children) {
131 8
            $name = (string) $children->getName();
132 8
            $method = (string) $children['method'];
133 8
            $operation = (string) $children['operation'];
134 8
            $version = (string) $children['version'];
135 8
            $url = (string) $children[0];
136
            $operations = [
137 8
                'method' => $method,
138 8
                'operation' => $operation,
139 8
                'version' => $version,
140 8
                'url' => $url
141
            ];
142 8
            $amb[$environment][$name] = $operations;
143
        }
144 8
        return $amb;
145
    }
146
}
147