Passed
Pull Request — master (#120)
by Roberto
02:29
created

SoapNative::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
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 SoapHeader;
23
use SoapFault;
24
use NFePHP\Common\Certificate;
25
use Psr\Log\LoggerInterface;
26
27
class SoapNative extends SoapBase implements SoapInterface
28
{
29
    /**
30
     * @var SoapClientExtended
31
     */
32
    protected $connection;
33
34
    /**
35
     * Constructor
36
     * @param Certificate $certificate
37
     * @param LoggerInterface $logger
38
     */
39 1
    public function __construct(Certificate $certificate = null, LoggerInterface $logger = null)
40
    {
41 1
        parent::__construct($certificate, $logger);
42 1
    }
43
44
    /**
45
     * Send soap message to url
46
     * @param string $url
47
     * @param string $operation
48
     * @param string $action
49
     * @param int $soapver
50
     * @param array $parameters
51
     * @param array $namespaces
52
     * @param string $request
53
     * @param SOAPHeader $soapheader
54
     * @return string
55
     * @throws SoapException
56
     */
57
    public function send(
58
        $url,
59
        $operation = '',
60
        $action = '',
61
        $soapver = SOAP_1_2,
62
        $parameters = [],
63
        $namespaces = [],
64
        $request = '',
65
        $soapheader = null
66
    ) {
67
        $this->prepare($url, $soapver);
68
        try {
69
            if (!empty($soapheader)) {
70
                $this->connection->__setSoapHeaders(array($soapheader));
71
            }
72
            $this->connection->$operation($parameters);
73
            $this->requestHead = $this->connection->__getLastRequestHeaders();
74
            $this->requestBody = $this->connection->__getLastRequest();
75
            $this->responseHead = $this->connection->__getLastResponseHeaders();
76
            $this->responseBody = $this->connection->__getLastResponse();
77
            $this->saveDebugFiles(
78
                $operation,
79
                $this->requestHead . "\n" . $this->requestBody,
80
                $this->responseHead . "\n" . $this->responseBody
81
            );
82
        } catch (SoapFault $e) {
83
            throw SoapException::soapFault($e->getMessage());
84
        } 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...
85
            throw SoapException::soapFault($e->getMessage());
86
        }
87
        return $this->responseBody;
88
    }
89
    
90
    /**
91
     * Prepare connection
92
     * @param string $url
93
     * @param int $soapver
94
     * @throws SoapException
95
     */
96
    protected function prepare($url, $soapver = SOAP_1_2)
97
    {
98
        $wsdl = "$url?WSDL";
99
        $verifypeer = true;
100
        $verifyhost = true;
101
        if ($this->disablesec) {
102
            $verifypeer = false;
103
            $verifyhost = false;
104
        }
105
        $params = [
106
            'local_cert' => $this->tempdir . $this->certfile,
107
            'passphrase' => $this->temppass,
108
            'connection_timeout' => $this->soaptimeout,
109
            'encoding' => 'UTF-8',
110
            'verifypeer' => $verifypeer,
111
            'verifyhost' => $verifyhost,
112
            'soap_version' => $soapver,
113
            'trace' => true,
114
            'cache_wsdl' => WSDL_CACHE_NONE
115
        ];
116
        $params = $this->setNativeProxy($params);
117
        try {
118
            $this->connection = new SoapClientExtended($wsdl, $params);
119
        } catch (SoapFault $e) {
120
            throw SoapException::soapFault($e->getMessage());
121
        } catch (\Exception $e) {
122
            throw SoapException::soapFault($e->getMessage());
123
        }
124
    }
125
    
126
    /**
127
     * Set parameters for proxy
128
     * @param array $params
129
     * @return array
130
     */
131
    private function setNativeProxy($params)
132
    {
133
        if ($this->proxyIP != '') {
134
            $pproxy1 = [
135
                'proxy_host' => $this->proxyIP,
136
                'proxy_port' => $this->proxyPort
137
            ];
138
            array_push($params, $pproxy1);
139
        }
140
        if ($this->proxyUser != '') {
141
            $pproxy2 = [
142
                'proxy_login' => $this->proxyUser,
143
                'proxy_password' => $this->proxyPass
144
            ];
145
            array_push($params, $pproxy2);
146
        }
147
        return $params;
148
    }
149
}
150