Passed
Push — master ( 463105...84cd59 )
by Roberto
04:47
created

SoapCurl   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 122
ccs 0
cts 94
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C send() 0 79 8
A setCurlProxy() 0 12 3
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_RETURNTRANSFER, 1);
99
            if (! empty($envelope)) {
100
                curl_setopt($oCurl, CURLOPT_POST, 1);
101
                curl_setopt($oCurl, CURLOPT_POSTFIELDS, $envelope);
102
                curl_setopt($oCurl, CURLOPT_HTTPHEADER, $parameters);
103
            }
104
            $response = curl_exec($oCurl);
105
            $this->soaperror = curl_error($oCurl);
106
            $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...
107
            $headsize = curl_getinfo($oCurl, CURLINFO_HEADER_SIZE);
108
            $httpcode = curl_getinfo($oCurl, CURLINFO_HTTP_CODE);
109
            curl_close($oCurl);
110
            $this->responseHead = trim(substr($response, 0, $headsize));
111
            $this->responseBody = trim(substr($response, $headsize));
112
            $this->saveDebugFiles(
113
                $operation,
114
                $this->requestHead . "\n" . $this->requestBody,
115
                $this->responseHead . "\n" . $this->responseBody
116
            );
117
        } 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...
118
            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...
119
        }
120
        if ($this->soaperror != '') {
121
            throw SoapException::soapFault($this->soaperror);
122
        }
123
        if ($httpcode != 200) {
124
            throw SoapException::soapFault($this->responseHead);
125
        }
126
        return $this->responseBody;
127
    }
128
    
129
    /**
130
     * Set proxy into cURL parameters
131
     * @param resource $oCurl
132
     */
133
    private function setCurlProxy(&$oCurl)
134
    {
135
        if ($this->proxyIP != '') {
136
            curl_setopt($oCurl, CURLOPT_HTTPPROXYTUNNEL, 1);
137
            curl_setopt($oCurl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
138
            curl_setopt($oCurl, CURLOPT_PROXY, $this->proxyIP.':'.$this->proxyPort);
139
            if ($this->proxyUser != '') {
140
                curl_setopt($oCurl, CURLOPT_PROXYUSERPWD, $this->proxyUser.':'.$this->proxyPass);
141
                curl_setopt($oCurl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
142
            }
143
        }
144
    }
145
}
146