2
2
// Copyright 1999-2020. Plesk International GmbH.
3
3
4
4
namespace PleskX \Api ;
5
+
5
6
use SimpleXMLElement ;
6
7
7
8
/**
8
- * Client for Plesk XML-RPC API
9
+ * Client for Plesk XML-RPC API.
9
10
*/
10
11
class Client
11
12
{
@@ -28,7 +29,7 @@ class Client
28
29
protected $ _verifyResponseCallback ;
29
30
30
31
/**
31
- * Create client
32
+ * Create client.
32
33
*
33
34
* @param string $host
34
35
* @param int $port
@@ -42,7 +43,7 @@ public function __construct($host, $port = 8443, $protocol = 'https')
42
43
}
43
44
44
45
/**
45
- * Setup credentials for authentication
46
+ * Setup credentials for authentication.
46
47
*
47
48
* @param string $login
48
49
* @param string $password
@@ -54,7 +55,7 @@ public function setCredentials($login, $password)
54
55
}
55
56
56
57
/**
57
- * Define secret key for alternative authentication
58
+ * Define secret key for alternative authentication.
58
59
*
59
60
* @param string $secretKey
60
61
*/
@@ -64,7 +65,7 @@ public function setSecretKey($secretKey)
64
65
}
65
66
66
67
/**
67
- * Set default version for requests
68
+ * Set default version for requests.
68
69
*
69
70
* @param string $version
70
71
*/
@@ -74,7 +75,7 @@ public function setVersion($version)
74
75
}
75
76
76
77
/**
77
- * Set custom function to verify response of API call according your own needs. Default verifying will be used if it is not specified
78
+ * Set custom function to verify response of API call according your own needs. Default verifying will be used if it is not specified.
78
79
*
79
80
* @param callable|null $function
80
81
*/
@@ -84,7 +85,7 @@ public function setVerifyResponse(callable $function = null)
84
85
}
85
86
86
87
/**
87
- * Retrieve host used for communication
88
+ * Retrieve host used for communication.
88
89
*
89
90
* @return string
90
91
*/
@@ -94,7 +95,7 @@ public function getHost()
94
95
}
95
96
96
97
/**
97
- * Retrieve port used for communication
98
+ * Retrieve port used for communication.
98
99
*
99
100
* @return int
100
101
*/
@@ -104,7 +105,7 @@ public function getPort()
104
105
}
105
106
106
107
/**
107
- * Retrieve name of the protocol (http or https) used for communication
108
+ * Retrieve name of the protocol (http or https) used for communication.
108
109
*
109
110
* @return string
110
111
*/
@@ -114,24 +115,27 @@ public function getProtocol()
114
115
}
115
116
116
117
/**
117
- * Retrieve XML template for packet
118
+ * Retrieve XML template for packet.
118
119
*
119
120
* @param string|null $version
121
+ *
120
122
* @return SimpleXMLElement
121
123
*/
122
124
public function getPacket ($ version = null )
123
125
{
124
126
$ protocolVersion = !is_null ($ version ) ? $ version : $ this ->_version ;
125
127
$ content = "<?xml version='1.0' encoding='UTF-8' ?> " ;
126
- $ content .= "<packet " . ("" === $ protocolVersion ? "" : " version=' $ protocolVersion' " ) . "/> " ;
128
+ $ content .= '<packet ' .('' === $ protocolVersion ? '' : " version=' $ protocolVersion' " ).'/> ' ;
129
+
127
130
return new SimpleXMLElement ($ content );
128
131
}
129
132
130
133
/**
131
- * Perform API request
134
+ * Perform API request.
132
135
*
133
136
* @param string|array|SimpleXMLElement $request
134
137
* @param int $mode
138
+ *
135
139
* @return XmlResponse
136
140
*/
137
141
public function request ($ request , $ mode = self ::RESPONSE_SHORT )
@@ -143,14 +147,14 @@ public function request($request, $mode = self::RESPONSE_SHORT)
143
147
144
148
if (is_array ($ request )) {
145
149
$ request = $ this ->_arrayToXml ($ request , $ xml )->asXML ();
146
- } else if (preg_match ('/^[a-z]/ ' , $ request )) {
150
+ } elseif (preg_match ('/^[a-z]/ ' , $ request )) {
147
151
$ request = $ this ->_expandRequestShortSyntax ($ request , $ xml );
148
152
}
149
153
}
150
154
151
155
if ('sdk ' == $ this ->_protocol ) {
152
156
$ version = ('' == $ this ->_version ) ? null : $ this ->_version ;
153
- $ requestXml = new SimpleXMLElement ((string )$ request );
157
+ $ requestXml = new SimpleXMLElement ((string ) $ request );
154
158
$ xml = \pm_ApiRpc::getService ($ version )->call ($ requestXml ->children ()[0 ]->asXml (), $ this ->_login );
155
159
} else {
156
160
$ xml = $ this ->_performHttpRequest ($ request );
@@ -164,11 +168,13 @@ public function request($request, $mode = self::RESPONSE_SHORT)
164
168
}
165
169
166
170
/**
167
- * Perform HTTP request to end-point
171
+ * Perform HTTP request to end-point.
168
172
*
169
173
* @param string $request
170
- * @return XmlResponse
174
+ *
171
175
* @throws Client\Exception
176
+ *
177
+ * @return XmlResponse
172
178
*/
173
179
private function _performHttpRequest ($ request )
174
180
{
@@ -191,20 +197,22 @@ private function _performHttpRequest($request)
191
197
curl_close ($ curl );
192
198
193
199
$ xml = new XmlResponse ($ result );
200
+
194
201
return $ xml ;
195
202
}
196
203
197
204
/**
198
- * Perform multiple API requests using single HTTP request
205
+ * Perform multiple API requests using single HTTP request.
199
206
*
200
207
* @param $requests
201
208
* @param int $mode
202
- * @return array
209
+ *
203
210
* @throws Client\Exception
211
+ *
212
+ * @return array
204
213
*/
205
214
public function multiRequest ($ requests , $ mode = self ::RESPONSE_SHORT )
206
215
{
207
-
208
216
$ requestXml = $ this ->getPacket ();
209
217
210
218
foreach ($ requests as $ request ) {
@@ -213,7 +221,7 @@ public function multiRequest($requests, $mode = self::RESPONSE_SHORT)
213
221
} else {
214
222
if (is_array ($ request )) {
215
223
$ request = $ this ->_arrayToXml ($ request , $ requestXml )->asXML ();
216
- } else if (preg_match ('/^[a-z]/ ' , $ request )) {
224
+ } elseif (preg_match ('/^[a-z]/ ' , $ request )) {
217
225
$ this ->_expandRequestShortSyntax ($ request , $ requestXml );
218
226
}
219
227
}
@@ -243,16 +251,16 @@ public function multiRequest($requests, $mode = self::RESPONSE_SHORT)
243
251
}
244
252
245
253
/**
246
- * Retrieve list of headers needed for request
254
+ * Retrieve list of headers needed for request.
247
255
*
248
256
* @return array
249
257
*/
250
258
protected function _getHeaders ()
251
259
{
252
- $ headers = array (
253
- " Content-Type: text/xml " ,
254
- " HTTP_PRETTY_PRINT: TRUE " ,
255
- ) ;
260
+ $ headers = [
261
+ ' Content-Type: text/xml ' ,
262
+ ' HTTP_PRETTY_PRINT: TRUE ' ,
263
+ ] ;
256
264
257
265
if ($ this ->_secretKey ) {
258
266
$ headers [] = "KEY: $ this ->_secretKey " ;
@@ -265,29 +273,32 @@ protected function _getHeaders()
265
273
}
266
274
267
275
/**
268
- * Verify that response does not contain errors
276
+ * Verify that response does not contain errors.
269
277
*
270
278
* @param XmlResponse $xml
279
+ *
271
280
* @throws Exception
272
281
*/
273
282
protected function _verifyResponse ($ xml )
274
283
{
275
- if ($ xml ->system && $ xml ->system ->status && 'error ' == (string )$ xml ->system ->status ) {
276
- throw new Exception ((string )$ xml ->system ->errtext , (int )$ xml ->system ->errcode );
284
+ if ($ xml ->system && $ xml ->system ->status && 'error ' == (string ) $ xml ->system ->status ) {
285
+ throw new Exception ((string ) $ xml ->system ->errtext , (int ) $ xml ->system ->errcode );
277
286
}
278
287
279
288
if ($ xml ->xpath ('//status[text()="error"] ' ) && $ xml ->xpath ('//errcode ' ) && $ xml ->xpath ('//errtext ' )) {
280
- $ errorCode = (int )$ xml ->xpath ('//errcode ' )[0 ];
281
- $ errorMessage = (string )$ xml ->xpath ('//errtext ' )[0 ];
289
+ $ errorCode = (int ) $ xml ->xpath ('//errcode ' )[0 ];
290
+ $ errorMessage = (string ) $ xml ->xpath ('//errtext ' )[0 ];
291
+
282
292
throw new Exception ($ errorMessage , $ errorCode );
283
293
}
284
294
}
285
295
286
296
/**
287
- * Expand short syntax (some.method.call) into full XML representation
297
+ * Expand short syntax (some.method.call) into full XML representation.
288
298
*
289
299
* @param string $request
290
300
* @param SimpleXMLElement $xml
301
+ *
291
302
* @return string
292
303
*/
293
304
protected function _expandRequestShortSyntax ($ request , SimpleXMLElement $ xml )
@@ -304,11 +315,12 @@ protected function _expandRequestShortSyntax($request, SimpleXMLElement $xml)
304
315
}
305
316
306
317
/**
307
- * Convert array to XML representation
318
+ * Convert array to XML representation.
308
319
*
309
320
* @param array $array
310
321
* @param SimpleXMLElement $xml
311
322
* @param string $parentEl
323
+ *
312
324
* @return SimpleXMLElement
313
325
*/
314
326
protected function _arrayToXml (array $ array , SimpleXMLElement $ xml , $ parentEl = null )
@@ -327,6 +339,7 @@ protected function _arrayToXml(array $array, SimpleXMLElement $xml, $parentEl =
327
339
328
340
/**
329
341
* @param array $array
342
+ *
330
343
* @return bool
331
344
*/
332
345
protected function _isAssocArray (array $ array )
@@ -336,12 +349,13 @@ protected function _isAssocArray(array $array)
336
349
337
350
/**
338
351
* @param string $name
352
+ *
339
353
* @return \PleskX\Api\Operator
340
354
*/
341
355
protected function _getOperator ($ name )
342
356
{
343
357
if (!isset ($ this ->_operatorsCache [$ name ])) {
344
- $ className = '\\PleskX \\Api \\Operator \\' . $ name ;
358
+ $ className = '\\PleskX \\Api \\Operator \\' . $ name ;
345
359
$ this ->_operatorsCache [$ name ] = new $ className ($ this );
346
360
}
347
361
0 commit comments