Completed
Pull Request — master (#2)
by
unknown
05:39
created

SoapOld   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
eloc 53
c 1
b 0
f 0
dl 0
loc 173
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A query() 0 14 4
A getRequest() 0 2 1
A getResponse() 0 2 1
A listSources() 0 2 1
A showError() 0 3 1
A __construct() 0 4 1
A connect() 0 14 3
A _parseConfig() 0 22 5
A close() 0 5 1
1
<?php
2
namespace VatNumberCheck\Soap;
3
4
use SoapClient;
5
use Cake\Core\Configure;
6
use Cake\Core\Exception\Exception;
7
use Cake\Core\InstanceConfigTrait;
8
use Cake\Log\Log;
9
10
/**
11
 * SOAP Client Wrapper.
12
 *
13
 */
14
class SoapOld {
15
16
    use InstanceConfigTrait;
17
18
/**
19
 * Description.
20
 *
21
 * @var string
22
 */
23
    public $description = 'Soap Client Wrapper';
24
25
/**
26
 * SoapClient instance.
27
 *
28
 * @var SoapClient|null
29
 */
30
    public $client = null;
31
32
/**
33
 * Connection status.
34
 *
35
 * @var bool
36
 */
37
    public $connected = false;
38
39
/**
40
 * Default configuration.
41
 *
42
 * @var array
43
 */
44
    protected $_defaultConfig = [
45
        'wsdl' => null,
46
        'location' => '',
47
        'uri' => '',
48
        'login' => '',
49
        'password' => '',
50
        'authentication' => 'SOAP_AUTHENTICATION_BASIC'
51
    ];
52
53
/**
54
 * Constructor.
55
 *
56
 * @param array $config An array defining the configuration settings
57
 */
58
    public function __construct($config = []) {
59
        $this->setConfig($config);
60
61
		$this->connected = $this->connect();
62
    }
63
64
/**
65
 * Setup Configuration options.
66
 *
67
 * @return array|bool Configuration options or false on failure
68
 */
69
    protected function _parseConfig() {
70
        if (!class_exists('SoapClient')) {
71
            $this->showError('Class SoapClient not found, please enable Soap extensions');
72
            return false;
73
        }
74
75
        $options = ['trace' => Configure::read('debug') > 0];
76
        if (!empty($this->getConfig('location'))) {
77
            $options['location'] = $this->getConfig('location');
78
        }
79
80
        if (!empty($this->getConfig('uri'))) {
81
            $options['uri'] = $this->getConfig('uri');
82
        }
83
84
        if (!empty($this->getConfig('login'))) {
85
            $options['login'] = $this->getConfig('login');
86
            $options['password'] = $this->getConfig('password');
87
            $options['authentication'] = $this->getConfig('authentication');
88
        }
89
90
        return $options;
91
    }
92
93
/**
94
 * Connects to the SOAP server using the WSDL in the configuration.
95
 *
96
 * @return bool True on success, false on failure
97
 */
98
    public function connect() {
99
        $options = $this->_parseConfig();
100
101
		$wsdl = $this->getConfig('wsdl');
102
        if (!empty($wsdl)) {
103
            try {
104
                $this->client = new SoapClient($wsdl, $options);
105
                return (bool)$this->client;
106
            } catch(SoapFault $fault) {
0 ignored issues
show
Bug introduced by
The type VatNumberCheck\Soap\SoapFault was not found. Did you mean SoapFault? If so, make sure to prefix the type with \.
Loading history...
107
                $this->showError($fault->faultstring);
108
            }
109
        }
110
111
        return false;
112
    }
113
114
/**
115
 * Sets the SoapClient instance to null.
116
 *
117
 * @return true
118
 */
119
    public function close() {
120
        $this->client = null;
121
        $this->connected = false;
122
123
        return true;
124
    }
125
126
/**
127
 * Returns the available SOAP methods.
128
 *
129
 * @param mixed $data Unused in this class.
130
 * @return array List of SOAP methods
131
 */
132
    public function listSources($data = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

132
    public function listSources(/** @scrutinizer ignore-unused */ $data = null) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
133
        return $this->client->__getFunctions();
0 ignored issues
show
Bug introduced by
The method __getFunctions() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

133
        return $this->client->/** @scrutinizer ignore-call */ __getFunctions();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
134
    }
135
136
/**
137
 * Query the SOAP server with the given method and parameters.
138
 *
139
 * @param string $method Name of method to call
140
 * @param array $queryData A list with parameters to pass
141
 * @return mixed Returns the result on success, false on failure
142
 */
143
    public function query($method, $queryData = []) {
144
        if (!$this->connected) {
145
            return false;
146
        }
147
148
        if (!empty($queryData)) {
149
            $queryData = [$queryData];
150
        }
151
152
        try {
153
            return $this->client->__soapCall($method, $queryData);
154
        } catch (SoapFault $fault) {
155
            $this->showError($fault->faultstring);
156
            return false;
157
        }
158
    }
159
160
/**
161
 * Returns the last SOAP response.
162
 *
163
 * @return string The last SOAP response
164
 */
165
    public function getResponse() {
166
        return $this->client->__getLastResponse();
167
    }
168
169
/**
170
 * Returns the last SOAP request.
171
 *
172
 * @return string The last SOAP request
173
 */
174
    public function getRequest() {
175
        return $this->client->__getLastRequest();
176
    }
177
178
/**
179
 * Writes an error message to log file.
180
 *
181
 * @param string $error Error message
182
 * @return void
183
 */
184
    public function showError($error) {
185
        $message = __d('vat_number_check', 'SOAP Error: %s', $error);
186
        Log::error($message);
187
    }
188
189
}
190