Test Failed
Push — master ( 07628d...adef33 )
by Roberto
03:31 queued 13s
created

SoapCurl::send()   F

Complexity

Conditions 12
Paths 746

Size

Total Lines 96

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
dl 0
loc 96
ccs 0
cts 91
cp 0
rs 2.6094
c 0
b 0
f 0
cc 12
nc 746
nop 8
crap 156

How to fix   Long Method    Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace NFePHP\Common\Soap;
4
5
/**
6
 * SoapClient based in cURL class
7
 *
8
 * @category  NFePHP
9
 * @package   NFePHP\Common\Soap\SoapCurl
10
 * @copyright NFePHP Copyright (c) 2016-2019
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-common for the canonical source repository
16
 */
17
18
use NFePHP\Common\Soap\SoapBase;
19
use NFePHP\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 $url
39
     * @param string $operation
40
     * @param string $action
41
     * @param int $soapver
42
     * @param array $parameters
43
     * @param array $namespaces
44
     * @param string $request
45
     * @param \SoapHeader $soapheader
46
     * @return string
47
     * @throws \NFePHP\Common\Exception\SoapException
48
     */
49
    public function send(
50
        $url,
51
        $operation = '',
52
        $action = '',
53
        $soapver = SOAP_1_2,
54
        $parameters = [],
55
        $namespaces = [],
56
        $request = '',
57
        $soapheader = null
58
    ) {
59
        //check or create key files
60
        //before send request
61
        $this->saveTemporarilyKeyFiles();
62
        $response = '';
63
        $envelope = $this->makeEnvelopeSoap(
64
            $request,
65
            $namespaces,
66
            $soapver,
67
            $soapheader
68
        );
69
        $msgSize = strlen($envelope);
70
        $parameters = [
71
            "Content-Type: application/soap+xml;charset=utf-8;",
72
            "Content-length: $msgSize"
73
        ];
74
        if (!empty($action)) {
75
            $parameters[0] .= "action=$action";
76
        }
77
        $this->requestHead = implode("\n", $parameters);
78
        $this->requestBody = $envelope;
79
        
80
        try {
81
            $oCurl = curl_init();
82
            $this->setCurlProxy($oCurl);
83
            curl_setopt($oCurl, CURLOPT_URL, $url);
84
            curl_setopt($oCurl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
85
            //curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT_MS, 1);
86
            //curl_setopt($oCurl, CURLOPT_TIMEOUT_MS, 1);
87
            curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, $this->soaptimeout);
88
            curl_setopt($oCurl, CURLOPT_TIMEOUT, $this->soaptimeout + 20);
89
            curl_setopt($oCurl, CURLOPT_HEADER, 1);
90
            curl_setopt($oCurl, CURLOPT_HTTP_VERSION, $this->httpver);
91
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, 0);
92
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, 0);
93
            if (!$this->disablesec) {
94
                curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, 2);
95
                if (is_file($this->casefaz)) {
96
                    curl_setopt($oCurl, CURLOPT_CAINFO, $this->casefaz);
97
                }
98
            }
99
            curl_setopt($oCurl, CURLOPT_SSLVERSION, $this->soapprotocol);
100
            curl_setopt($oCurl, CURLOPT_SSLCERT, $this->tempdir . $this->certfile);
101
            curl_setopt($oCurl, CURLOPT_SSLKEY, $this->tempdir . $this->prifile);
102
            if (!empty($this->temppass)) {
103
                curl_setopt($oCurl, CURLOPT_KEYPASSWD, $this->temppass);
104
            }
105
            curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
106
            if (!empty($envelope)) {
107
                curl_setopt($oCurl, CURLOPT_POST, 1);
108
                curl_setopt($oCurl, CURLOPT_POSTFIELDS, $envelope);
109
                curl_setopt($oCurl, CURLOPT_HTTPHEADER, $parameters);
110
            }
111
            $response = curl_exec($oCurl);
112
            $this->soaperror = curl_error($oCurl);
113
            $this->soaperror_code = curl_errno($oCurl);
114
            $ainfo = curl_getinfo($oCurl);
115
            if (is_array($ainfo)) {
116
                $this->soapinfo = $ainfo;
117
            }
118
            $headsize = curl_getinfo($oCurl, CURLINFO_HEADER_SIZE);
119
            $httpcode = curl_getinfo($oCurl, CURLINFO_HTTP_CODE);
120
            curl_close($oCurl);
121
            $this->responseHead = trim(substr($response, 0, $headsize));
122
            $this->responseBody = trim(substr($response, $headsize));
123
            $this->saveDebugFiles(
124
                $operation,
125
                $this->requestHead . "\n" . $this->requestBody,
126
                $this->responseHead . "\n" . $this->responseBody
127
            );
128
        } catch (\Exception $e) {
129
            throw SoapException::unableToLoadCurl($e->getMessage());
130
        }
131
        if ($this->soaperror != '') {
132
            if (intval($this->soaperror_code) == 0) {
133
                $this->soaperror_code = 7;
134
            }
135
            throw SoapException::soapFault($this->soaperror . " [$url]", $this->soaperror_code);
136
        }
137
        if ($httpcode != 200) {
138
            if (intval($httpcode) == 0) {
139
                $httpcode = 52;
140
            }
141
            throw SoapException::soapFault(" [$url]" . $this->responseHead, $httpcode);
142
        }
143
        return $this->responseBody;
144
    }
145
    
146
    /**
147
     * Set proxy into cURL parameters
148
     * @param resource $oCurl
149
     */
150
    private function setCurlProxy(&$oCurl)
151
    {
152
        if ($this->proxyIP != '') {
153
            curl_setopt($oCurl, CURLOPT_HTTPPROXYTUNNEL, 1);
154
            curl_setopt($oCurl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
155
            curl_setopt($oCurl, CURLOPT_PROXY, $this->proxyIP . ':' . $this->proxyPort);
156
            if ($this->proxyUser != '') {
157
                curl_setopt($oCurl, CURLOPT_PROXYUSERPWD, $this->proxyUser . ':' . $this->proxyPass);
158
                curl_setopt($oCurl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
159
            }
160
        }
161
    }
162
    
163
    /**
164
     * Verify if URL is active
165
     * @param string $url
166
     * @return boolean
167
     * @throws \NFePHP\Common\Exception\SoapException
168
     */
169
    public function checkWsdlActive($url)
170
    {
171
        $last = substr($url, -4);
0 ignored issues
show
Unused Code introduced by
$last 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...
172
        if (strtoupper(substr($url, -5)) != '?WSDL') {
173
            $url .= "?WSDL";
174
        }
175
        $this->saveTemporarilyKeyFiles();
176
        try {
177
            $oCurl = curl_init();
178
            $this->setCurlProxy($oCurl);
179
            curl_setopt($oCurl, CURLOPT_URL, $url);
180
            curl_setopt($oCurl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
181
            curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, $this->soaptimeout);
182
            curl_setopt($oCurl, CURLOPT_TIMEOUT, $this->soaptimeout + 20);
183
            curl_setopt($oCurl, CURLOPT_HEADER, 1);
184
            curl_setopt($oCurl, CURLOPT_HTTP_VERSION, $this->httpver);
185
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, 0);
186
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, 0);
187
            if (!$this->disablesec) {
188
                curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, 2);
189
                if (is_file($this->casefaz)) {
190
                    curl_setopt($oCurl, CURLOPT_CAINFO, $this->casefaz);
191
                }
192
            }
193
            curl_setopt($oCurl, CURLOPT_SSLVERSION, $this->soapprotocol);
194
            curl_setopt($oCurl, CURLOPT_SSLCERT, $this->tempdir . $this->certfile);
195
            curl_setopt($oCurl, CURLOPT_SSLKEY, $this->tempdir . $this->prifile);
196
            if (!empty($this->temppass)) {
197
                curl_setopt($oCurl, CURLOPT_KEYPASSWD, $this->temppass);
198
            }
199
            curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
200
            $response = curl_exec($oCurl);
201
            $this->soaperror = curl_error($oCurl);
202
            $this->soaperror_code = curl_errno($oCurl);
203
            $ainfo = curl_getinfo($oCurl);
204
            if (is_array($ainfo)) {
205
                $this->soapinfo = $ainfo;
206
            }
207
            $headsize = curl_getinfo($oCurl, CURLINFO_HEADER_SIZE);
208
            $httpcode = curl_getinfo($oCurl, CURLINFO_HTTP_CODE);
209
            curl_close($oCurl);
210
            $this->responseHead = trim(substr($response, 0, $headsize));
211
            $this->responseBody = trim(substr($response, $headsize));
212
        } catch (\Exception $e) {
213
            throw SoapException::unableToLoadCurl($e->getMessage());
214
        }
215
        if ($this->soaperror != '') {
216
            if (intval($this->soaperror_code) == 0) {
217
                $this->soaperror_code = 7;
218
            }
219
            throw SoapException::soapFault($this->soaperror . " [$url]", $this->soaperror_code);
220
        }
221
        if ($httpcode != 200) {
222
            if (intval($httpcode) == 0) {
223
                $httpcode = 52;
224
            }
225
            throw SoapException::soapFault(" [$url]" . $this->responseHead, $httpcode);
226
        }
227
        return true;
228
    }
229
}
230