Passed
Pull Request — master (#118)
by Roberto
02:45
created

SoapCurl::setCurlProxy()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 0
cts 12
cp 0
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
crap 12
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
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
        $response = '';
60
        $envelope = $this->makeEnvelopeSoap(
61
            $request,
62
            $operation,
63
            $namespaces,
64
            $soapver,
65
            $soapheader
66
        );
67
        $msgSize = strlen($envelope);
68
        $parameters = [
69
            "Content-Type: application/soap+xml;charset=utf-8;",
70
            "Content-length: $msgSize"
71
        ];
72
        if (!empty($action)) {
73
            $parameters[0] .= "action=$action";
74
        }
75
        $this->requestHead = implode("\n", $parameters);
76
        $this->requestBody = $envelope;
77
        
78
        try {
79
            $this->saveTemporarilyKeyFiles();
80
            $oCurl = curl_init();
81
            $this->setCurlProxy($oCurl);
82
            curl_setopt($oCurl, CURLOPT_URL, $url);
83
            curl_setopt($oCurl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
84
            curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, $this->soaptimeout);
85
            curl_setopt($oCurl, CURLOPT_TIMEOUT, $this->soaptimeout + 20);
86
            curl_setopt($oCurl, CURLOPT_HEADER, 1);
87
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, 0);
88
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, 0);
89
            if (!$this->disablesec) {
90
                curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, 2);
91
                if (is_file($this->casefaz)) {
92
                    curl_setopt($oCurl, CURLOPT_CAINFO, $this->casefaz);
93
                }
94
            }
95
            curl_setopt($oCurl, CURLOPT_SSLVERSION, $this->soapprotocol);
96
            curl_setopt($oCurl, CURLOPT_SSLCERT, $this->tempdir . $this->certfile);
97
            curl_setopt($oCurl, CURLOPT_SSLKEY, $this->tempdir . $this->prifile);
98
            curl_setopt($oCurl, CURLOPT_KEYPASSWD, $this->temppass);
99
            curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
100
            if (! empty($envelope)) {
101
                curl_setopt($oCurl, CURLOPT_POST, 1);
102
                curl_setopt($oCurl, CURLOPT_POSTFIELDS, $envelope);
103
                curl_setopt($oCurl, CURLOPT_HTTPHEADER, $parameters);
104
            }
105
            $response = curl_exec($oCurl);
106
            $this->soaperror = curl_error($oCurl);
107
            $this->soapinfo = curl_getinfo($oCurl);
0 ignored issues
show
Documentation Bug introduced by
It seems like curl_getinfo($oCurl) of type * is incompatible with the declared type array of property $soapinfo.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
108
            $headsize = curl_getinfo($oCurl, CURLINFO_HEADER_SIZE);
109
            $httpcode = curl_getinfo($oCurl, CURLINFO_HTTP_CODE);
110
            curl_close($oCurl);
111
            $this->responseHead = trim(substr($response, 0, $headsize));
112
            $this->responseBody = trim(substr($response, $headsize));
113
            $this->saveDebugFiles(
114
                $operation,
115
                $this->requestHead . "\n" . $this->requestBody,
116
                $this->responseHead . "\n" . $this->responseBody
117
            );
118
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class NFePHP\Common\Soap\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
119
            throw SoapException::unableToLoadCurl($e->getMessage());
0 ignored issues
show
Unused Code introduced by
The call to SoapException::unableToLoadCurl() has too many arguments starting with $e->getMessage().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
120
        }
121
        if ($this->soaperror != '') {
122
            throw SoapException::soapFault($this->soaperror);
123
        }
124
        if ($httpcode != 200) {
125
            throw SoapException::soapFault($this->responseHead);
126
        }
127
        return $this->responseBody;
128
    }
129
    
130
    /**
131
     * Set proxy into cURL parameters
132
     * @param resource $oCurl
133
     */
134
    private function setCurlProxy(&$oCurl)
135
    {
136
        if ($this->proxyIP != '') {
137
            curl_setopt($oCurl, CURLOPT_HTTPPROXYTUNNEL, 1);
138
            curl_setopt($oCurl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
139
            curl_setopt($oCurl, CURLOPT_PROXY, $this->proxyIP.':'.$this->proxyPort);
140
            if ($this->proxyUser != '') {
141
                curl_setopt($oCurl, CURLOPT_PROXYUSERPWD, $this->proxyUser.':'.$this->proxyPass);
142
                curl_setopt($oCurl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
143
            }
144
        }
145
    }
146
}
147