Passed
Pull Request — master (#210)
by Thiago
03:27
created

SoapNative::send()   A

Complexity

Conditions 4
Paths 32

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 0
cts 18
cp 0
rs 9.392
c 0
b 0
f 0
cc 4
nc 32
nop 8
crap 20

How to fix   Many Parameters   

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 native PHP SoapClient class
7
 *
8
 * @category  NFePHP
9
 * @package   NFePHP\Common\Soap\SoapNative
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\SoapClientExtended;
19
use NFePHP\Common\Soap\SoapBase;
20
use NFePHP\Common\Soap\SoapInterface;
21
use NFePHP\Common\Exception\SoapException;
22
use Exception;
23
use SoapHeader;
24
use SoapFault;
25
use NFePHP\Common\Certificate;
26
use Psr\Log\LoggerInterface;
27
28
class SoapNative extends SoapBase implements SoapInterface
29
{
30
    /**
31
     * @var SoapClientExtended
32
     */
33
    protected $connection;
34
35
    /**
36
     * Constructor
37
     * @param Certificate $certificate
38
     * @param LoggerInterface $logger
39
     */
40 1
    public function __construct(Certificate $certificate = null, LoggerInterface $logger = null)
41
    {
42 1
        parent::__construct($certificate, $logger);
43 1
    }
44
45
    /**
46
     * Send soap message to url
47
     * @param string $url
48
     * @param string $operation
49
     * @param string $action
50
     * @param int $soapver
51
     * @param array $parameters
52
     * @param array $namespaces
53
     * @param string $request
54
     * @param \SoapHeader $soapheader
55
     * @return string
56
     * @throws SoapException
57
     */
58
    public function send(
59
        $url,
60
        $operation = '',
61
        $action = '',
62
        $soapver = SOAP_1_2,
63
        $parameters = [],
64
        $namespaces = [],
65
        $request = '',
66
        $soapheader = null
67
    ) {
68
        $this->prepare($url, $soapver);
69
        try {
70
            if (!empty($soapheader)) {
71
                $this->connection->__setSoapHeaders(array($soapheader));
72
            }
73
            $params = new \SoapVar(str_replace([':v1', 'v1:'], '', $request), XSD_ANYXML);
74
            $this->connection->$operation($params);
75
            $this->requestHead = $this->connection->__getLastRequestHeaders();
76
            $this->requestBody = $this->connection->__getLastRequest();
77
            $this->responseHead = $this->connection->__getLastResponseHeaders();
78
            $this->responseBody = $this->connection->__getLastResponse();
79
            $this->saveDebugFiles(
80
                $operation,
81
                $this->requestHead . "\n" . $this->requestBody,
82
                $this->responseHead . "\n" . $this->responseBody
83
            );
84
        } catch (SoapFault $e) {
85
            throw SoapException::soapFault("[$url] " . $e->getMessage());
86
        } catch (\Exception $e) {
87
            throw SoapException::soapFault("[$url] " . $e->getMessage());
88
        }
89
        return $this->responseBody;
90
    }
91
92
    /**
93
     * @return resource
94
     */
95
    private function createSslStreamContext()
96
    {
97
        return stream_context_create([
98
            'ssl' => [
99
                'verify_peer' => false,
100
                'verify_peer_name' => false,
101
                'allow_self_signed' => true,
102
            ]
103
        ]);
104
    }
105
106
    /**
107
     * Prepare connection
108
     * @param string $url
109
     * @param int $soapver
110
     * @throws SoapException
111
     */
112
    protected function prepare($url, $soapver = SOAP_1_2)
113
    {
114
        $wsdl = "$url?WSDL";
115
        $verifypeer = true;
116
        $verifyhost = true;
117
        if ($this->disablesec) {
118
            $verifypeer = false;
119
            $verifyhost = false;
120
        }
121
        $this->saveTemporarilyKeyFiles();
122
        $params = [
123
            'stream_context' => $this->createSslStreamContext(),
124
            'local_cert' => $this->tempdir . $this->certfile,
125
            'connection_timeout' => $this->soaptimeout,
126
            'encoding' => 'UTF-8',
127
            'verifypeer' => $verifypeer,
128
            'verifyhost' => $verifyhost,
129
            'soap_version' => $soapver,
130
            'trace' => true,
131
            'cache_wsdl' => WSDL_CACHE_NONE
132
        ];
133
        if (!empty($this->temppass)) {
134
            $params['passphrase'] = $this->temppass;
135
        }
136
        $params = $this->setNativeProxy($params);
137
        try {
138
            $this->connection = new SoapClientExtended($wsdl, $params);
139
        } catch (SoapFault $e) {
140
            throw SoapException::soapFault($e->getMessage());
141
        } catch (\Exception $e) {
142
            throw SoapException::soapFault($e->getMessage());
143
        }
144
    }
145
    
146
    /**
147
     * Set parameters for proxy
148
     * @param array $params
149
     * @return array
150
     */
151
    private function setNativeProxy($params)
152
    {
153
        if ($this->proxyIP != '') {
154
            $pproxy1 = [
155
                'proxy_host' => $this->proxyIP,
156
                'proxy_port' => $this->proxyPort
157
            ];
158
            array_push($params, $pproxy1);
159
        }
160
        if ($this->proxyUser != '') {
161
            $pproxy2 = [
162
                'proxy_login' => $this->proxyUser,
163
                'proxy_password' => $this->proxyPass
164
            ];
165
            array_push($params, $pproxy2);
166
        }
167
        return $params;
168
    }
169
}
170