Completed
Push — master ( be8c82...d7ae84 )
by Roberto
04:38 queued 02:37
created

SoapCurl::send()   C

Complexity

Conditions 9
Paths 313

Size

Total Lines 69

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 0
loc 69
ccs 0
cts 69
cp 0
rs 5.324
c 0
b 0
f 0
cc 9
nc 313
nop 5
crap 90

How to fix   Long Method   

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:

1
<?php
2
3
namespace NFePHP\eSocial\Common\Soap;
4
5
/**
6
 * SoapClient based in cURL class
7
 *
8
 * @category  library
9
 * @package   NFePHP\eSocial
10
 * @copyright NFePHP Copyright (c) 2016
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\Certificate;
19
use NFePHP\Common\Exception\SoapException;
20
use Psr\Log\LoggerInterface;
21
22
class SoapCurl extends SoapBase implements SoapInterface
23
{
24
    /**
25
     * Constructor
26
     * @param Certificate $certificate
27
     * @param LoggerInterface $logger
28
     */
29
    public function __construct(Certificate $certificate = null, LoggerInterface $logger = null)
30
    {
31
        parent::__construct($certificate, $logger);
32
    }
33
    /**
34
     * Send soap message to url
35
     *
36
     * @param  string $operation
37
     * @param  string $url
38
     * @param  string $action
39
     * @param  string $envelope
40
     * @param  array $parameters
41
     * @return string
42
     * @throws \NFePHP\Common\Exception\SoapException
43
     */
44
    public function send(
45
        $operation,
46
        $url,
47
        $action,
48
        $envelope,
49
        $parameters
50
    ) {
51
        $response = '';
52
        $this->requestHead = implode("\n", $parameters);
53
        $this->requestBody = $envelope;
54
        try {
55
            $this->saveTemporarilyKeyFiles();
56
            $oCurl = curl_init();
57
            $this->setCurlProxy($oCurl);
58
            curl_setopt($oCurl, CURLOPT_URL, $url);
59
            curl_setopt($oCurl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
60
            curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, $this->soaptimeout);
61
            curl_setopt($oCurl, CURLOPT_TIMEOUT, $this->soaptimeout + 20);
62
            curl_setopt($oCurl, CURLOPT_HEADER, 1);
63
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, 0);
64
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, 0);
65
            if (! $this->disablesec) {
66
                curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, 2);
67
                if (is_file($this->casefaz)) {
68
                    curl_setopt($oCurl, CURLOPT_CAINFO, $this->casefaz);
69
                }
70
            }
71
            curl_setopt($oCurl, CURLOPT_SSLVERSION, $this->soapprotocol);
72
            curl_setopt($oCurl, CURLOPT_SSLCERT, $this->tempdir.$this->certfile);
73
            curl_setopt($oCurl, CURLOPT_SSLKEY, $this->tempdir.$this->prifile);
74
            if (! empty($this->temppass)) {
75
                curl_setopt($oCurl, CURLOPT_KEYPASSWD, $this->temppass);
76
            }
77
            curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true);
78
            if (! empty($envelope)) {
79
                curl_setopt($oCurl, CURLOPT_POST, true);
80
                curl_setopt($oCurl, CURLOPT_POSTFIELDS, $envelope);
81
                curl_setopt($oCurl, CURLOPT_HTTPHEADER, $parameters);
82
            }
83
            $response = curl_exec($oCurl);
84
            $this->soaperror = curl_error($oCurl);
85
            $ainfo = curl_getinfo($oCurl);
86
            if (is_array($ainfo)) {
87
                $this->soapinfo = $ainfo;
88
            }
89
            $headsize = curl_getinfo($oCurl, CURLINFO_HEADER_SIZE);
90
            $httpcode = curl_getinfo($oCurl, CURLINFO_HTTP_CODE);
91
            curl_close($oCurl);
92
            $this->responseHead = trim(substr($response, 0, $headsize));
93
            $this->responseBody = trim(substr($response, $headsize));
94
            $this->saveDebugFiles(
95
                $operation,
96
                $this->requestHead."\n".$this->requestBody,
97
                $this->responseHead."\n".$this->responseBody
98
            );
99
        } catch (\Exception $e) {
100
            throw SoapException::unableToLoadCurl($e->getMessage());
101
        }
102
        if ($this->soaperror != '') {
103
            throw SoapException::soapFault($this->soaperror." [$url]");
104
        }
105
        if ($httpcode != 200) {
106
            throw SoapException::soapFault(
107
                " [$url] HTTP Error code: $httpcode - "
108
                .$this->getFaultString($this->responseBody)
109
            );
110
        }
111
        return $this->responseBody;
112
    }
113
    /**
114
     * Set proxy into cURL parameters
115
     * @param resource $oCurl
116
     */
117
    private function setCurlProxy(&$oCurl)
118
    {
119
        if ($this->proxyIP != '') {
120
            curl_setopt($oCurl, CURLOPT_HTTPPROXYTUNNEL, 1);
121
            curl_setopt($oCurl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
122
            curl_setopt($oCurl, CURLOPT_PROXY, $this->proxyIP.':'.$this->proxyPort);
123
            if ($this->proxyUser != '') {
124
                curl_setopt($oCurl, CURLOPT_PROXYUSERPWD, $this->proxyUser.':'.$this->proxyPass);
125
                curl_setopt($oCurl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
126
            }
127
        }
128
    }
129
    /**
130
     * Extract faultstring form response if exists
131
     * @param  string $body
132
     * @return string
133
     */
134
    private function getFaultString($body)
135
    {
136
        if (empty($body)) {
137
            return '';
138
        }
139
        $dom = new \DOMDocument('1.0', 'UTF-8');
140
        $dom->formatOutput = false;
141
        $dom->preserveWhiteSpace = false;
142
        $dom->loadXML($body);
143
        $faultstring = '';
144
        $nodefault = ! empty($dom->getElementsByTagName('faultstring')->item(0))
145
            ? $dom->getElementsByTagName('faultstring')->item(0)
146
            : '';
147
        if (!empty($nodefault)) {
148
            $faultstring = $nodefault->nodeValue;
149
        }
150
        return htmlentities($faultstring, ENT_QUOTES, 'UTF-8');
151
    }
152
    /**
153
     * Recover WSDL form given URL
154
     * @param  string $url
155
     * @return string
156
     */
157
    public function wsdl($url)
158
    {
159
        $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...
160
        $this->saveTemporarilyKeyFiles();
161
        $url .= '?WSDL';
162
        $oCurl = curl_init();
163
        curl_setopt($oCurl, CURLOPT_URL, $url);
164
        curl_setopt($oCurl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
165
        curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, $this->soaptimeout);
166
        curl_setopt($oCurl, CURLOPT_TIMEOUT, $this->soaptimeout + 20);
167
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, 0);
168
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, 0);
169
        curl_setopt($oCurl, CURLOPT_SSLVERSION, $this->soapprotocol);
170
        curl_setopt($oCurl, CURLOPT_SSLCERT, $this->tempdir.$this->certfile);
171
        curl_setopt($oCurl, CURLOPT_SSLKEY, $this->tempdir.$this->prifile);
172
        if (! empty($this->temppass)) {
173
            curl_setopt($oCurl, CURLOPT_KEYPASSWD, $this->temppass);
174
        }
175
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true);
176
        $response  = curl_exec($oCurl);
177
        $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...
178
        $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...
179
        $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...
180
        $httpcode = curl_getinfo($oCurl, CURLINFO_HTTP_CODE);
181
        curl_close($oCurl);
182
        if ($httpcode != 200) {
183
            return '';
184
        }
185
        return $response;
186
    }
187
}