Passed
Pull Request — master (#2)
by
unknown
05:11
created

Soap::close()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
namespace VatNumberCheck\Soap\Soap;
3
4
use SoapClient;
5
use Cake\Core\Configure;
6
use Cake\Core\Exception\Exception;
7
8
/**
9
 * SOAP Client Wrapper.
10
 *
11
 */
12
class Soap {
13
14
/**
15
 * Description.
16
 *
17
 * @var string
18
 */
19
	public $description = 'Soap Client Wrapper';
20
21
/**
22
 * SoapClient instance.
23
 *
24
 * @var SoapClient|null
25
 */
26
	public $client = null;
27
28
/**
29
 * Connection status.
30
 *
31
 * @var bool
32
 */
33
	public $connected = false;
34
35
/**
36
 * Default configuration.
37
 *
38
 * @var array
39
 */
40
	protected $_baseConfig = [
41
		'wsdl' => null,
42
		'location' => '',
43
		'uri' => '',
44
		'login' => '',
45
		'password' => '',
46
		'authentication' => 'SOAP_AUTHENTICATION_BASIC'
47
	];
48
49
/**
50
 * Constructor.
51
 *
52
 * @param array $config An array defining the configuration settings
53
 */
54
	public function __construct($config = []) {
55
		parent::__construct($config);
56
57
		$this->connected = $this->connect();
58
	}
59
60
/**
61
 * Setup Configuration options.
62
 *
63
 * @return array|bool Configuration options or false on failure
64
 */
65
	protected function _parseConfig() {
66
		if (!class_exists('SoapClient')) {
67
			$this->showError('Class SoapClient not found, please enable Soap extensions');
68
			return false;
69
		}
70
71
		$options = ['trace' => Configure::read('debug') > 0];
72
		if (!empty($this->getConfig('location'))) {
0 ignored issues
show
Bug introduced by
The method getConfig() does not exist on VatNumberCheck\Soap\Soap\Soap. ( Ignorable by Annotation )

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

72
		if (!empty($this->/** @scrutinizer ignore-call */ getConfig('location'))) {

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...
73
			$options['location'] = $this->getConfig('location');
74
		}
75
76
		if (!empty($this->getConfig('uri'))) {
77
			$options['uri'] = $this->getConfig('uri');
78
		}
79
80
		if (!empty($this->getConfig('login'))) {
81
			$options['login'] = $this->getConfig('login');
82
			$options['password'] = $this->getConfig('password');
83
			$options['authentication'] = $this->getConfig('authentication');
84
		}
85
86
		return $options;
87
	}
88
89
/**
90
 * Connects to the SOAP server using the WSDL in the configuration.
91
 *
92
 * @return bool True on success, false on failure
93
 */
94
	public function connect() {
95
		$options = $this->_parseConfig();
96
97
		if (!empty($this->config['wsdl'])) {
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist on VatNumberCheck\Soap\Soap\Soap. Did you maybe forget to declare it?
Loading history...
98
			try {
99
				$this->client = new SoapClient($this->getConfig('wsdl'), $options);
100
				return (bool)$this->client;
101
			} catch(SoapFault $fault) {
0 ignored issues
show
Bug introduced by
The type VatNumberCheck\Soap\Soap\SoapFault was not found. Did you mean SoapFault? If so, make sure to prefix the type with \.
Loading history...
102
				$this->showError($fault->faultstring);
103
			}
104
		}
105
106
		return false;
107
	}
108
109
/**
110
 * Sets the SoapClient instance to null.
111
 *
112
 * @return true
113
 */
114
	public function close() {
115
		$this->client = null;
116
		$this->connected = false;
117
118
		return true;
119
	}
120
121
/**
122
 * Returns the available SOAP methods.
123
 *
124
 * @param mixed $data Unused in this class.
125
 * @return array List of SOAP methods
126
 */
127
	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

127
	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...
128
		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

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