Completed
Pull Request — master (#122)
by Roberto
02:26
created

SoapCurl::send()   F

Complexity

Conditions 9
Paths 338

Size

Total Lines 82
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 0
loc 82
ccs 0
cts 81
cp 0
rs 3.6943
c 0
b 0
f 0
cc 9
eloc 68
nc 338
nop 8
crap 90

How to fix   Long Method    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
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
            $namespaces,
63
            $soapver,
64
            $soapheader
65
        );
66
        $msgSize = strlen($envelope);
67
        $parameters = [
68
            "Content-Type: application/soap+xml;charset=utf-8;",
69
            "Content-length: $msgSize"
70
        ];
71
        if (!empty($action)) {
72
            $parameters[0] .= "action=$action";
73
        }
74
        $this->requestHead = implode("\n", $parameters);
75
        $this->requestBody = $envelope;
76
        
77
        try {
78
            $this->saveTemporarilyKeyFiles();
79
            $oCurl = curl_init();
80
            $this->setCurlProxy($oCurl);
81
            curl_setopt($oCurl, CURLOPT_URL, $url);
82
            curl_setopt($oCurl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
83
            curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, $this->soaptimeout);
84
            curl_setopt($oCurl, CURLOPT_TIMEOUT, $this->soaptimeout + 20);
85
            curl_setopt($oCurl, CURLOPT_HEADER, 1);
86
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, 0);
87
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, 0);
88
            if (!$this->disablesec) {
89
                curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, 2);
90
                if (is_file($this->casefaz)) {
91
                    curl_setopt($oCurl, CURLOPT_CAINFO, $this->casefaz);
92
                }
93
            }
94
            curl_setopt($oCurl, CURLOPT_SSLVERSION, $this->soapprotocol);
95
            curl_setopt($oCurl, CURLOPT_SSLCERT, $this->tempdir . $this->certfile);
96
            curl_setopt($oCurl, CURLOPT_SSLKEY, $this->tempdir . $this->prifile);
97
            curl_setopt($oCurl, CURLOPT_KEYPASSWD, $this->temppass);
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
            $ainfo = curl_getinfo($oCurl);
107
            if (is_array($ainfo)) {
108
                $this->soapinfo = $ainfo;
109
            }
110
            $headsize = curl_getinfo($oCurl, CURLINFO_HEADER_SIZE);
111
            $httpcode = curl_getinfo($oCurl, CURLINFO_HTTP_CODE);
112
            curl_close($oCurl);
113
            $this->responseHead = trim(substr($response, 0, $headsize));
114
            $this->responseBody = trim(substr($response, $headsize));
115
            $this->saveDebugFiles(
116
                $operation,
117
                $this->requestHead . "\n" . $this->requestBody,
118
                $this->responseHead . "\n" . $this->responseBody
119
            );
120
        } catch (\Exception $e) {
121
            throw SoapException::unableToLoadCurl($e->getMessage());
122
        }
123
        if ($this->soaperror != '') {
124
            throw SoapException::soapFault($this->soaperror);
125
        }
126
        if ($httpcode != 200) {
127
            throw SoapException::soapFault($this->responseHead);
128
        }
129
        return $this->responseBody;
130
    }
131
    
132
    /**
133
     * Set proxy into cURL parameters
134
     * @param resource $oCurl
135
     */
136
    private function setCurlProxy(&$oCurl)
137
    {
138
        if ($this->proxyIP != '') {
139
            curl_setopt($oCurl, CURLOPT_HTTPPROXYTUNNEL, 1);
140
            curl_setopt($oCurl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
141
            curl_setopt($oCurl, CURLOPT_PROXY, $this->proxyIP.':'.$this->proxyPort);
142
            if ($this->proxyUser != '') {
143
                curl_setopt($oCurl, CURLOPT_PROXYUSERPWD, $this->proxyUser.':'.$this->proxyPass);
144
                curl_setopt($oCurl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
145
            }
146
        }
147
    }
148
}
149