Test Failed
Push — master ( 732787...772519 )
by Roberto
04:30 queued 02:30
created

SoapNative::send()   A

Complexity

Conditions 4
Paths 28

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 0
cts 18
cp 0
rs 9.408
c 0
b 0
f 0
cc 4
nc 28
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
            $this->connection->$operation($parameters);
74
            $this->requestHead = $this->connection->__getLastRequestHeaders();
75
            $this->requestBody = $this->connection->__getLastRequest();
76
            $this->responseHead = $this->connection->__getLastResponseHeaders();
77
            $this->responseBody = $this->connection->__getLastResponse();
78
            $this->saveDebugFiles(
79
                $operation,
80
                $this->requestHead . "\n" . $this->requestBody,
81
                $this->responseHead . "\n" . $this->responseBody
82
            );
83
        } catch (SoapFault $e) {
84
            throw SoapException::soapFault("[$url] " . $e->getMessage());
0 ignored issues
show
Bug introduced by
The call to soapFault() misses a required argument $code.

This check looks for function calls that miss required arguments.

Loading history...
85
        } catch (\Exception $e) {
86
            throw SoapException::soapFault("[$url] " . $e->getMessage());
0 ignored issues
show
Bug introduced by
The call to soapFault() misses a required argument $code.

This check looks for function calls that miss required arguments.

Loading history...
87
        }
88
        return $this->responseBody;
89
    }
90
    
91
    /**
92
     * Prepare connection
93
     * @param string $url
94
     * @param int $soapver
95
     * @throws SoapException
96
     */
97
    protected function prepare($url, $soapver = SOAP_1_2)
98
    {
99
        $wsdl = "$url?WSDL";
100
        $verifypeer = true;
101
        $verifyhost = true;
102
        if ($this->disablesec) {
103
            $verifypeer = false;
104
            $verifyhost = false;
105
        }
106
        $this->saveTemporarilyKeyFiles();
107
        $params = [
108
            'local_cert' => $this->tempdir . $this->certfile,
109
            'connection_timeout' => $this->soaptimeout,
110
            'encoding' => 'UTF-8',
111
            'verifypeer' => $verifypeer,
112
            'verifyhost' => $verifyhost,
113
            'soap_version' => $soapver,
114
            'trace' => true,
115
            'cache_wsdl' => WSDL_CACHE_NONE
116
        ];
117
        if (!empty($this->temppass)) {
118
            $params['passphrase'] = $this->temppass;
119
        }
120
        $params = $this->setNativeProxy($params);
121
        try {
122
            $this->connection = new SoapClientExtended($wsdl, $params);
123
        } catch (SoapFault $e) {
124
            throw SoapException::soapFault($e->getMessage());
0 ignored issues
show
Bug introduced by
The call to soapFault() misses a required argument $code.

This check looks for function calls that miss required arguments.

Loading history...
125
        } catch (\Exception $e) {
126
            throw SoapException::soapFault($e->getMessage());
0 ignored issues
show
Bug introduced by
The call to soapFault() misses a required argument $code.

This check looks for function calls that miss required arguments.

Loading history...
127
        }
128
    }
129
    
130
    /**
131
     * Set parameters for proxy
132
     * @param array $params
133
     * @return array
134
     */
135
    private function setNativeProxy($params)
136
    {
137
        if ($this->proxyIP != '') {
138
            $pproxy1 = [
139
                'proxy_host' => $this->proxyIP,
140
                'proxy_port' => $this->proxyPort
141
            ];
142
            array_push($params, $pproxy1);
143
        }
144
        if ($this->proxyUser != '') {
145
            $pproxy2 = [
146
                'proxy_login' => $this->proxyUser,
147
                'proxy_password' => $this->proxyPass
148
            ];
149
            array_push($params, $pproxy2);
150
        }
151
        return $params;
152
    }
153
}
154