SoapCurl::wsdl()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 30
rs 9.44
cc 3
nc 4
nop 1
1
<?php
2
3
namespace NFePHP\eFinanc\Common\Soap;
4
5
/**
6
 * SoapClient based in cURL class
7
 *
8
 * @category  API
9
 * @package   NFePHP\eFinanc
10
 * @copyright NFePHP Copyright (c) 2018
11
 * @license   http://www.gnu.org/licenses/lgpl.txt LGPLv3+
12
 * @license   https://opensource.org/licenses/MIT MIT
13
 * @license   http://www.gnu.org/licenses/gpl.txt GPLv3+
14
 * @author    Roberto L. Machado <linux.rlm at gmail dot com>
15
 * @link      http://github.com/nfephp-org/sped-efinanceira for the canonical source repository
16
 */
17
18
use NFePHP\eFinanc\Common\Soap\SoapBase;
19
use NFePHP\eFinanc\Common\Soap\SoapInterface;
20
use NFePHP\Common\Exception\SoapException;
21
use NFePHP\Common\Certificate;
22
use Psr\Log\LoggerInterface;
23
24
class SoapCurl extends SoapBase implements SoapInterface
25
{
26
    /**
27
     * Constructor
28
     * @param Certificate $certificate
29
     * @param LoggerInterface $logger
30
     */
31
    public function __construct(Certificate $certificate = null, LoggerInterface $logger = null)
32
    {
33
        parent::__construct($certificate, $logger);
34
    }
35
    
36
    /**
37
     * Send soap message to url
38
     * @param string $operation
39
     * @param string $url
40
     * @param string $action
41
     * @param string $envelope
42
     * @param array $parameters
43
     * @return string
44
     * @throws \NFePHP\Common\Exception\SoapException
45
     */
46
    public function send(
47
        $operation,
48
        $url,
49
        $action,
50
        $envelope,
51
        $parameters
52
    ) {
53
        $response = '';
54
        $this->requestHead = implode("\n", $parameters);
55
        $this->requestBody = $envelope;
56
        
57
        try {
58
            $this->saveTemporarilyKeyFiles();
59
            $oCurl = curl_init();
60
            $this->setCurlProxy($oCurl);
61
            curl_setopt($oCurl, CURLOPT_URL, $url);
62
            curl_setopt($oCurl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
63
            curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, $this->soaptimeout);
64
            curl_setopt($oCurl, CURLOPT_TIMEOUT, $this->soaptimeout + 20);
65
            curl_setopt($oCurl, CURLOPT_HEADER, 1);
66
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, 0);
67
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, 0);
68
            if (!$this->disablesec) {
69
                curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, 2);
70
                if (is_file($this->casefaz)) {
71
                    curl_setopt($oCurl, CURLOPT_CAINFO, $this->casefaz);
72
                }
73
            }
74
            curl_setopt($oCurl, CURLOPT_SSLVERSION, $this->soapprotocol);
75
            curl_setopt($oCurl, CURLOPT_SSLCERT, $this->tempdir . $this->certfile);
76
            curl_setopt($oCurl, CURLOPT_SSLKEY, $this->tempdir . $this->prifile);
77
            if (!empty($this->temppass)) {
78
                curl_setopt($oCurl, CURLOPT_KEYPASSWD, $this->temppass);
79
            }
80
            curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true);
81
            if (! empty($envelope)) {
82
                curl_setopt($oCurl, CURLOPT_POST, true);
83
                curl_setopt($oCurl, CURLOPT_POSTFIELDS, $envelope);
84
                curl_setopt($oCurl, CURLOPT_HTTPHEADER, $parameters);
85
            }
86
            $response = curl_exec($oCurl);
87
            $this->soaperror = curl_error($oCurl);
88
            $ainfo = curl_getinfo($oCurl);
89
            if (is_array($ainfo)) {
90
                $this->soapinfo = $ainfo;
91
            }
92
            $headsize = curl_getinfo($oCurl, CURLINFO_HEADER_SIZE);
93
            $httpcode = curl_getinfo($oCurl, CURLINFO_HTTP_CODE);
94
            curl_close($oCurl);
95
            $this->responseHead = trim(substr($response, 0, $headsize));
96
            $this->responseBody = trim(substr($response, $headsize));
97
            $this->saveDebugFiles(
98
                $operation,
99
                $this->requestHead . "\n" . $this->requestBody,
100
                $this->responseHead . "\n" . $this->responseBody
101
            );
102
        } catch (\Exception $e) {
103
            throw SoapException::unableToLoadCurl($e->getMessage());
104
        }
105
        if ($this->soaperror != '') {
106
            throw SoapException::soapFault($this->soaperror . " [$url]");
107
        }
108
        if ($httpcode != 200) {
109
            throw SoapException::soapFault(
110
                " [$url] HTTP Error code: $httpcode - "
111
                . $this->getFaultString($this->responseBody)
112
            );
113
        }
114
        return $this->responseBody;
115
    }
116
    
117
    /**
118
     * Recover WSDL form given URL
119
     * @param string $url
120
     * @return string
121
     */
122
    public function wsdl($url)
123
    {
124
        $response = '';
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
125
        $this->saveTemporarilyKeyFiles();
126
        $url .= '?singleWsdl';
127
        $oCurl = curl_init();
128
        curl_setopt($oCurl, CURLOPT_URL, $url);
129
        curl_setopt($oCurl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
130
        curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, $this->soaptimeout);
131
        curl_setopt($oCurl, CURLOPT_TIMEOUT, $this->soaptimeout + 20);
132
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, 0);
133
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, 0);
134
        curl_setopt($oCurl, CURLOPT_SSLVERSION, $this->soapprotocol);
135
        curl_setopt($oCurl, CURLOPT_SSLCERT, $this->tempdir . $this->certfile);
136
        curl_setopt($oCurl, CURLOPT_SSLKEY, $this->tempdir . $this->prifile);
137
        if (!empty($this->temppass)) {
138
            curl_setopt($oCurl, CURLOPT_KEYPASSWD, $this->temppass);
139
        }
140
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true);
141
        $response = curl_exec($oCurl);
142
        $soaperror = curl_error($oCurl);
0 ignored issues
show
Unused Code introduced by
$soaperror is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
143
        $ainfo = curl_getinfo($oCurl);
0 ignored issues
show
Unused Code introduced by
$ainfo is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
144
        $headsize = curl_getinfo($oCurl, CURLINFO_HEADER_SIZE);
0 ignored issues
show
Unused Code introduced by
$headsize is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
145
        $httpcode = curl_getinfo($oCurl, CURLINFO_HTTP_CODE);
146
        curl_close($oCurl);
147
        if ($httpcode != 200) {
148
            return '';
149
        }
150
        return $response;
151
    }
152
    
153
    /**
154
     * Set proxy into cURL parameters
155
     * @param resource $oCurl
156
     */
157
    private function setCurlProxy(&$oCurl)
158
    {
159
        if ($this->proxyIP != '') {
160
            curl_setopt($oCurl, CURLOPT_HTTPPROXYTUNNEL, 1);
161
            curl_setopt($oCurl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
162
            curl_setopt($oCurl, CURLOPT_PROXY, $this->proxyIP.':'.$this->proxyPort);
163
            if ($this->proxyUser != '') {
164
                curl_setopt($oCurl, CURLOPT_PROXYUSERPWD, $this->proxyUser.':'.$this->proxyPass);
165
                curl_setopt($oCurl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
166
            }
167
        }
168
    }
169
    
170
    /**
171
     * Extract faultstring form response if exists
172
     * @param string $body
173
     * @return string
174
     */
175
    private function getFaultString($body)
176
    {
177
        if (empty($body)) {
178
            return '';
179
        }
180
        $dom = new \DOMDocument('1.0', 'UTF-8');
181
        $dom->formatOutput = false;
182
        $dom->preserveWhiteSpace = false;
183
        $dom->loadXML($body);
184
        $faultstring = '';
185
        $nodefault = !empty($dom->getElementsByTagName('faultstring')->item(0))
186
            ? $dom->getElementsByTagName('faultstring')->item(0)
187
            : '';
188
        if (!empty($nodefault)) {
189
            $faultstring = $nodefault->nodeValue;
190
        }
191
        return htmlentities($faultstring, ENT_QUOTES, 'UTF-8');
192
    }
193
}
194