SoapSource::connect()   A
last analyzed

Complexity

Conditions 3
Paths 15

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
nc 15
nop 0
dl 0
loc 17
rs 9.9
c 2
b 0
f 0
1
<?php
2
App::uses('DataSource', 'Model/Datasource');
3
4
/**
5
 * SOAP DataSource.
6
 *
7
 */
8
class SoapSource extends DataSource {
0 ignored issues
show
Bug introduced by
The type DataSource was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
/**
11
 * Description.
12
 *
13
 * @var string
14
 */
15
	public $description = 'Soap Client DataSource';
16
17
/**
18
 * SoapClient instance.
19
 *
20
 * @var SoapClient|null
21
 */
22
	public $client = null;
23
24
/**
25
 * Connection status.
26
 *
27
 * @var bool
28
 */
29
	public $connected = false;
30
31
/**
32
 * Configured value of default_socket_timeout.
33
 *
34
 * @var string
35
 */
36
	protected $_defaultSocketTimeout;
37
38
/**
39
 * Original value of default_socket_timeout.
40
 *
41
 * @var string
42
 */
43
	protected $_originalDefaultSocketTimeout;
44
45
/**
46
 * Default configuration.
47
 *
48
 * @var array
49
 */
50
	protected $_baseConfig = [
51
		'wsdl' => null,
52
		'location' => '',
53
		'uri' => '',
54
		'login' => '',
55
		'password' => '',
56
		'authentication' => 'SOAP_AUTHENTICATION_BASIC',
57
	];
58
59
/**
60
 * Constructor.
61
 *
62
 * @param array $config An array defining the configuration settings
63
 */
64
	public function __construct($config = []) {
65
		parent::__construct($config);
66
67
		$this->_originalDefaultSocketTimeout = strval(ini_get('default_socket_timeout'));
68
		$this->_defaultSocketTimeout = strval($this->config['default_socket_timeout'] ?? $this->_originalDefaultSocketTimeout);
69
70
		$this->connected = $this->connect();
71
	}
72
73
/**
74
 * Setup Configuration options.
75
 *
76
 * @return array|bool Configuration options or false on failure
77
 */
78
	protected function _parseConfig() {
79
		if (!class_exists('SoapClient')) {
80
			$this->showError('Class SoapClient not found, please enable Soap extensions');
81
82
			return false;
83
		}
84
85
		$options = ['trace' => Configure::read('debug') > 0];
0 ignored issues
show
Bug introduced by
The type Configure was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
86
		if (!empty($this->config['location'])) {
87
			$options['location'] = $this->config['location'];
88
		}
89
90
		if (!empty($this->config['uri'])) {
91
			$options['uri'] = $this->config['uri'];
92
		}
93
94
		if (!empty($this->config['connection_timeout'])) {
95
			$options['connection_timeout'] = $this->config['connection_timeout'];
96
		}
97
98
		if (!empty($this->config['login'])) {
99
			$options['login'] = $this->config['login'];
100
			$options['password'] = $this->config['password'];
101
			$options['authentication'] = $this->config['authentication'];
102
		}
103
104
		if (!empty($this->config['compression'])) {
105
			// SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP | SOAP_COMPRESSION_DEFLATE
106
			$options['compression'] = $this->config['compression'];
107
		}
108
		if (!empty($this->config['cache_wsdl'])) {
109
			// WSDL_CACHE_NONE
110
			$options['cache_wsdl'] = $this->config['cache_wsdl'];
111
		}
112
		if (!empty($this->config['keep_alive'])) {
113
			// false
114
			$options['keep_alive'] = $this->config['keep_alive'];
115
		}
116
117
		return $options;
118
	}
119
120
/**
121
 * Connects to the SOAP server using the WSDL in the configuration.
122
 *
123
 * @return bool True on success, false on failure
124
 */
125
	public function connect() {
126
		$options = $this->_parseConfig();
127
128
		if (!empty($this->config['wsdl'])) {
129
			try {
130
				ini_set('default_socket_timeout', $this->_defaultSocketTimeout);
131
				$this->client = new SoapClient($this->config['wsdl'], $options);
132
133
				return (bool)$this->client;
134
			} catch(SoapFault $fault) {
135
				$this->showError($fault->faultstring);
136
			} finally {
137
				ini_set('default_socket_timeout', $this->_originalDefaultSocketTimeout);
138
			}
139
		}
140
141
		return false;
142
	}
143
144
/**
145
 * Sets the SoapClient instance to null.
146
 *
147
 * @return true
148
 */
149
	public function close() {
150
		$this->client = null;
151
		$this->connected = false;
152
153
		return true;
154
	}
155
156
/**
157
 * Returns the available SOAP methods.
158
 *
159
 * @param mixed $data Unused in this class.
160
 * @return array List of SOAP methods
161
 */
162
	public function listSources($data = null) {
163
		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

163
		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...
164
	}
165
166
/**
167
 * Query the SOAP server with the given method and parameters.
168
 *
169
 * @param string $method Name of method to call
170
 * @param array $queryData A list with parameters to pass
171
 * @return mixed Returns the result on success, false on failure
172
 */
173
	public function query($method, $queryData = []) {
174
		if (!$this->connected) {
175
			return false;
176
		}
177
178
		if (!empty($queryData)) {
179
			$queryData = [$queryData];
180
		}
181
182
		try {
183
			ini_set('default_socket_timeout', $this->_defaultSocketTimeout);
184
185
			return $this->client->__soapCall($method, $queryData);
186
		} catch (SoapFault $fault) {
187
			$this->showError($fault->faultstring);
188
		} finally {
189
			ini_set('default_socket_timeout', $this->_originalDefaultSocketTimeout);
190
		}
191
192
		return false;
193
	}
194
195
/**
196
 * Returns the last SOAP response.
197
 *
198
 * @return string The last SOAP response
199
 */
200
	public function getResponse() {
201
		return $this->client->__getLastResponse();
202
	}
203
204
/**
205
 * Returns the last SOAP request.
206
 *
207
 * @return string The last SOAP request
208
 */
209
	public function getRequest() {
210
		return $this->client->__getLastRequest();
211
	}
212
213
/**
214
 * Writes an error message to log file.
215
 *
216
 * @param string $error Error message
217
 * @return void
218
 */
219
	public function showError($error) {
220
		$message = __d('vat_number_check', 'SOAP Error: %s', $error);
0 ignored issues
show
Bug introduced by
The function __d was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

220
		$message = /** @scrutinizer ignore-call */ __d('vat_number_check', 'SOAP Error: %s', $error);
Loading history...
221
		CakeLog::error($message);
0 ignored issues
show
Bug introduced by
The type CakeLog was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
222
	}
223
224
}
225