| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* |
| 8 |
*/ |
| 9 |
class CJTServicesClient { |
| 10 |
|
| 11 |
const BODY_ENCODING_JSON = 'json'; |
| 12 |
const BODY_ENCODING_PSQ = 'psq'; |
| 13 |
|
| 14 |
/** |
| 15 |
* put your comment there... |
| 16 |
* |
| 17 |
* @var mixed |
| 18 |
*/ |
| 19 |
protected static $instances = array(); |
| 20 |
|
| 21 |
/** |
| 22 |
* put your comment there... |
| 23 |
* |
| 24 |
* @var mixed |
| 25 |
*/ |
| 26 |
private $sslVerify = false; |
| 27 |
|
| 28 |
/** |
| 29 |
* put your comment there... |
| 30 |
* |
| 31 |
* @var mixed |
| 32 |
*/ |
| 33 |
private $timeOut = 10; |
| 34 |
|
| 35 |
/** |
| 36 |
* put your comment there... |
| 37 |
* |
| 38 |
* @var mixed |
| 39 |
*/ |
| 40 |
private $url; |
| 41 |
|
| 42 |
/** |
| 43 |
* put your comment there... |
| 44 |
* |
| 45 |
* @param string API Entry point URL to be used as server base address for any API call |
| 46 |
* @return CJTServicesClient |
| 47 |
*/ |
| 48 |
public function __construct($uri) |
| 49 |
{ |
| 50 |
|
| 51 |
$this->url = "{$uri}/cjtservices-api"; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* put your comment there... |
| 56 |
* |
| 57 |
* @param mixed $uri |
| 58 |
* @return CJTServicesClient |
| 59 |
*/ |
| 60 |
public static function & getInstance($uri) |
| 61 |
{ |
| 62 |
|
| 63 |
# Maintain one instance for every base address |
| 64 |
if (!isset(self::$instances[$uri])) |
| 65 |
{ |
| 66 |
self::$instances[$uri] = new CJTServicesClient($uri); |
| 67 |
} |
| 68 |
|
| 69 |
return self::$instances[$uri]; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* put your comment there... |
| 74 |
* |
| 75 |
*/ |
| 76 |
public function getSSLVerify() |
| 77 |
{ |
| 78 |
return $this->sslVerify; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* put your comment there... |
| 83 |
* |
| 84 |
*/ |
| 85 |
public function getTimeOut() |
| 86 |
{ |
| 87 |
return $this->timeOut; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* put your comment there... |
| 92 |
* |
| 93 |
*/ |
| 94 |
public function getUrl() |
| 95 |
{ |
| 96 |
return $this->url; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* put your comment there... |
| 101 |
* |
| 102 |
* @param mixed $module |
| 103 |
* @param mixed $method |
| 104 |
* @param mixed $params |
| 105 |
* @param mixed $postData |
| 106 |
* @param mixed $bodyEncoding |
| 107 |
* @return WP_Error |
| 108 |
*/ |
| 109 |
public function makeCall( $module, |
| 110 |
$method, |
| 111 |
$params = null, |
| 112 |
$postData = null, |
| 113 |
$bodyEncoding = self::BODY_ENCODING_JSON) |
| 114 |
{ |
| 115 |
# Prepare method name by replacing all UPPER Letters to Lower |
| 116 |
# precedence by _ |
| 117 |
while ( preg_match( '/[A-Z]/', $method, $upperLetter, PREG_OFFSET_CAPTURE ) ) |
| 118 |
{ |
| 119 |
$method = substr_replace |
| 120 |
( |
| 121 |
$method, |
| 122 |
( '_' . strtolower( $upperLetter[ 0 ][ 0 ] ) ), |
| 123 |
$upperLetter[ 0 ][ 1 ], 1 |
| 124 |
); |
| 125 |
}; |
| 126 |
|
| 127 |
# Lowercase module name |
| 128 |
$module = strtolower( $module ); |
| 129 |
|
| 130 |
# Construct method call uri |
| 131 |
$methodUri = "{$this->url}/{$module}/{$method}"; |
| 132 |
|
| 133 |
# Defaults and E_ALL Complains |
| 134 |
if ( !$params ) |
| 135 |
{ |
| 136 |
$params = array(); |
| 137 |
} |
| 138 |
|
| 139 |
# Encode parameters |
| 140 |
foreach ($params as $name => $value) |
| 141 |
{ |
| 142 |
$params[ $name ] = urlencode( $value ); |
| 143 |
} |
| 144 |
|
| 145 |
# Add query string parameters |
| 146 |
$methodUri = add_query_arg( $params, $methodUri ); |
| 147 |
|
| 148 |
# Body Encoding |
| 149 |
# I personally like to have encoders in separated classes |
| 150 |
# but we don't have time fot this now!! |
| 151 |
switch ($bodyEncoding) |
| 152 |
{ |
| 153 |
|
| 154 |
case self::BODY_ENCODING_JSON: |
| 155 |
|
| 156 |
$postData = json_encode($postData); |
| 157 |
|
| 158 |
break; |
| 159 |
|
| 160 |
case self::BODY_ENCODING_PSQ: |
| 161 |
|
| 162 |
$postData = http_build_query($postData); |
| 163 |
|
| 164 |
break; |
| 165 |
} |
| 166 |
|
| 167 |
# Request parameters |
| 168 |
$requestParams = array |
| 169 |
( |
| 170 |
'timeout' => $this->getTimeOut(), |
| 171 |
'sslverify' => $this->getSSLVerify(), |
| 172 |
'body' => $postData, |
| 173 |
); |
| 174 |
|
| 175 |
# POST Server |
| 176 |
$postRequest = wp_remote_post($methodUri, $requestParams); |
| 177 |
|
| 178 |
if (!$postRequest || ($postRequest instanceof WP_Error)) |
| 179 |
{ |
| 180 |
throw new CJTServicesAPICallException('CJTStore: Unable to call CJT Services API'); |
| 181 |
} |
| 182 |
|
| 183 |
# Make sure its CJT Services Response (E.g CJT Services is not activated or the returned |
| 184 |
# content is not belong to CJT Services response) |
| 185 |
if (!wp_remote_retrieve_header($postRequest, 'cjt-store')) |
| 186 |
{ |
| 187 |
throw new CJTServicesAPICallException('CJTStore: Invalid response!!!'); |
| 188 |
} |
| 189 |
|
| 190 |
# Exception thrown by API |
| 191 |
if (wp_remote_retrieve_header($postRequest, 'cjtservices-exception') == 'true') |
| 192 |
{ |
| 193 |
|
| 194 |
$excMsg = wp_remote_retrieve_header($postRequest, 'cjtservices-exception-message'); |
| 195 |
$excCode = (int) wp_remote_retrieve_header($postRequest, 'cjtservices-exception-code'); |
| 196 |
|
| 197 |
throw new CJTServicesAPICallException("API Service Exception<br /> Details: <br /><br /> {$excMsg}:{$excCode}"); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
# Return response Wordpress Handle |
| 202 |
return $postRequest; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* put your comment there... |
| 207 |
* |
| 208 |
* @param mixed $value |
| 209 |
* @return {CJTServicesClient|mixed} |
| 210 |
*/ |
| 211 |
public function & setSSLVerify($value) { |
| 212 |
# Set |
| 213 |
$this->sslVerify = $value; |
| 214 |
# Chaining |
| 215 |
return $this; |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* put your comment there... |
| 220 |
* |
| 221 |
* @param mixed $value |
| 222 |
* @return CJTServicesClient |
| 223 |
*/ |
| 224 |
public function & setTimeOut($value) { |
| 225 |
# Set |
| 226 |
$this->timeOut = $value; |
| 227 |
# Chaining |
| 228 |
return $this; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
} |