22// Copyright 1999-2020. Plesk International GmbH.
33
44namespace PleskX \Api ;
5+
56use SimpleXMLElement ;
67
78/**
8- * Client for Plesk XML-RPC API
9+ * Client for Plesk XML-RPC API.
910 */
1011class Client
1112{
@@ -28,7 +29,7 @@ class Client
2829 protected $ _verifyResponseCallback ;
2930
3031 /**
31- * Create client
32+ * Create client.
3233 *
3334 * @param string $host
3435 * @param int $port
@@ -42,7 +43,7 @@ public function __construct($host, $port = 8443, $protocol = 'https')
4243 }
4344
4445 /**
45- * Setup credentials for authentication
46+ * Setup credentials for authentication.
4647 *
4748 * @param string $login
4849 * @param string $password
@@ -54,7 +55,7 @@ public function setCredentials($login, $password)
5455 }
5556
5657 /**
57- * Define secret key for alternative authentication
58+ * Define secret key for alternative authentication.
5859 *
5960 * @param string $secretKey
6061 */
@@ -64,7 +65,7 @@ public function setSecretKey($secretKey)
6465 }
6566
6667 /**
67- * Set default version for requests
68+ * Set default version for requests.
6869 *
6970 * @param string $version
7071 */
@@ -74,7 +75,7 @@ public function setVersion($version)
7475 }
7576
7677 /**
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.
7879 *
7980 * @param callable|null $function
8081 */
@@ -84,7 +85,7 @@ public function setVerifyResponse(callable $function = null)
8485 }
8586
8687 /**
87- * Retrieve host used for communication
88+ * Retrieve host used for communication.
8889 *
8990 * @return string
9091 */
@@ -94,7 +95,7 @@ public function getHost()
9495 }
9596
9697 /**
97- * Retrieve port used for communication
98+ * Retrieve port used for communication.
9899 *
99100 * @return int
100101 */
@@ -104,7 +105,7 @@ public function getPort()
104105 }
105106
106107 /**
107- * Retrieve name of the protocol (http or https) used for communication
108+ * Retrieve name of the protocol (http or https) used for communication.
108109 *
109110 * @return string
110111 */
@@ -114,24 +115,27 @@ public function getProtocol()
114115 }
115116
116117 /**
117- * Retrieve XML template for packet
118+ * Retrieve XML template for packet.
118119 *
119120 * @param string|null $version
121+ *
120122 * @return SimpleXMLElement
121123 */
122124 public function getPacket ($ version = null )
123125 {
124126 $ protocolVersion = !is_null ($ version ) ? $ version : $ this ->_version ;
125127 $ content = "<?xml version='1.0' encoding='UTF-8' ?> " ;
126- $ content .= "<packet " . ("" === $ protocolVersion ? "" : " version=' $ protocolVersion' " ) . "/> " ;
128+ $ content .= '<packet ' .('' === $ protocolVersion ? '' : " version=' $ protocolVersion' " ).'/> ' ;
129+
127130 return new SimpleXMLElement ($ content );
128131 }
129132
130133 /**
131- * Perform API request
134+ * Perform API request.
132135 *
133136 * @param string|array|SimpleXMLElement $request
134137 * @param int $mode
138+ *
135139 * @return XmlResponse
136140 */
137141 public function request ($ request , $ mode = self ::RESPONSE_SHORT )
@@ -143,14 +147,14 @@ public function request($request, $mode = self::RESPONSE_SHORT)
143147
144148 if (is_array ($ request )) {
145149 $ request = $ this ->_arrayToXml ($ request , $ xml )->asXML ();
146- } else if (preg_match ('/^[a-z]/ ' , $ request )) {
150+ } elseif (preg_match ('/^[a-z]/ ' , $ request )) {
147151 $ request = $ this ->_expandRequestShortSyntax ($ request , $ xml );
148152 }
149153 }
150154
151155 if ('sdk ' == $ this ->_protocol ) {
152156 $ version = ('' == $ this ->_version ) ? null : $ this ->_version ;
153- $ requestXml = new SimpleXMLElement ((string )$ request );
157+ $ requestXml = new SimpleXMLElement ((string ) $ request );
154158 $ xml = \pm_ApiRpc::getService ($ version )->call ($ requestXml ->children ()[0 ]->asXml (), $ this ->_login );
155159 } else {
156160 $ xml = $ this ->_performHttpRequest ($ request );
@@ -164,11 +168,13 @@ public function request($request, $mode = self::RESPONSE_SHORT)
164168 }
165169
166170 /**
167- * Perform HTTP request to end-point
171+ * Perform HTTP request to end-point.
168172 *
169173 * @param string $request
170- * @return XmlResponse
174+ *
171175 * @throws Client\Exception
176+ *
177+ * @return XmlResponse
172178 */
173179 private function _performHttpRequest ($ request )
174180 {
@@ -191,20 +197,22 @@ private function _performHttpRequest($request)
191197 curl_close ($ curl );
192198
193199 $ xml = new XmlResponse ($ result );
200+
194201 return $ xml ;
195202 }
196203
197204 /**
198- * Perform multiple API requests using single HTTP request
205+ * Perform multiple API requests using single HTTP request.
199206 *
200207 * @param $requests
201208 * @param int $mode
202- * @return array
209+ *
203210 * @throws Client\Exception
211+ *
212+ * @return array
204213 */
205214 public function multiRequest ($ requests , $ mode = self ::RESPONSE_SHORT )
206215 {
207-
208216 $ requestXml = $ this ->getPacket ();
209217
210218 foreach ($ requests as $ request ) {
@@ -213,7 +221,7 @@ public function multiRequest($requests, $mode = self::RESPONSE_SHORT)
213221 } else {
214222 if (is_array ($ request )) {
215223 $ request = $ this ->_arrayToXml ($ request , $ requestXml )->asXML ();
216- } else if (preg_match ('/^[a-z]/ ' , $ request )) {
224+ } elseif (preg_match ('/^[a-z]/ ' , $ request )) {
217225 $ this ->_expandRequestShortSyntax ($ request , $ requestXml );
218226 }
219227 }
@@ -243,16 +251,16 @@ public function multiRequest($requests, $mode = self::RESPONSE_SHORT)
243251 }
244252
245253 /**
246- * Retrieve list of headers needed for request
254+ * Retrieve list of headers needed for request.
247255 *
248256 * @return array
249257 */
250258 protected function _getHeaders ()
251259 {
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+ ] ;
256264
257265 if ($ this ->_secretKey ) {
258266 $ headers [] = "KEY: $ this ->_secretKey " ;
@@ -265,29 +273,32 @@ protected function _getHeaders()
265273 }
266274
267275 /**
268- * Verify that response does not contain errors
276+ * Verify that response does not contain errors.
269277 *
270278 * @param XmlResponse $xml
279+ *
271280 * @throws Exception
272281 */
273282 protected function _verifyResponse ($ xml )
274283 {
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 );
277286 }
278287
279288 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+
282292 throw new Exception ($ errorMessage , $ errorCode );
283293 }
284294 }
285295
286296 /**
287- * Expand short syntax (some.method.call) into full XML representation
297+ * Expand short syntax (some.method.call) into full XML representation.
288298 *
289299 * @param string $request
290300 * @param SimpleXMLElement $xml
301+ *
291302 * @return string
292303 */
293304 protected function _expandRequestShortSyntax ($ request , SimpleXMLElement $ xml )
@@ -304,11 +315,12 @@ protected function _expandRequestShortSyntax($request, SimpleXMLElement $xml)
304315 }
305316
306317 /**
307- * Convert array to XML representation
318+ * Convert array to XML representation.
308319 *
309320 * @param array $array
310321 * @param SimpleXMLElement $xml
311322 * @param string $parentEl
323+ *
312324 * @return SimpleXMLElement
313325 */
314326 protected function _arrayToXml (array $ array , SimpleXMLElement $ xml , $ parentEl = null )
@@ -327,6 +339,7 @@ protected function _arrayToXml(array $array, SimpleXMLElement $xml, $parentEl =
327339
328340 /**
329341 * @param array $array
342+ *
330343 * @return bool
331344 */
332345 protected function _isAssocArray (array $ array )
@@ -336,12 +349,13 @@ protected function _isAssocArray(array $array)
336349
337350 /**
338351 * @param string $name
352+ *
339353 * @return \PleskX\Api\Operator
340354 */
341355 protected function _getOperator ($ name )
342356 {
343357 if (!isset ($ this ->_operatorsCache [$ name ])) {
344- $ className = '\\PleskX \\Api \\Operator \\' . $ name ;
358+ $ className = '\\PleskX \\Api \\Operator \\' . $ name ;
345359 $ this ->_operatorsCache [$ name ] = new $ className ($ this );
346360 }
347361
0 commit comments