| Total Complexity | 63 |
| Total Lines | 372 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 1 |
Complex classes like WebApiClient often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WebApiClient, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 49 | class WebApiClient |
||
| 50 | { |
||
| 51 | public static $cache_length = 1200; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The base URL to use for all the calls |
||
| 55 | * @var String |
||
| 56 | */ |
||
| 57 | protected $baseUrl; |
||
| 58 | |||
| 59 | public function setBaseUrl($u) |
||
| 60 | { |
||
| 61 | $this->baseUrl = $u; |
||
| 62 | } |
||
| 63 | |||
| 64 | public function getBaseUrl() |
||
| 65 | { |
||
| 66 | return $this->baseUrl; |
||
| 67 | } |
||
| 68 | |||
| 69 | protected $methods; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The methods to call |
||
| 73 | * |
||
| 74 | * @param array $v |
||
| 75 | */ |
||
| 76 | public function setMethods($v) |
||
| 77 | { |
||
| 78 | $this->methods = $v; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * An array of parameters that should ALWAYS be |
||
| 83 | * passed through on each request. |
||
| 84 | * |
||
| 85 | * @var array |
||
| 86 | */ |
||
| 87 | protected $globalParams = null; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Sets the global parameters |
||
| 91 | */ |
||
| 92 | public function setGlobalParams($v) |
||
| 93 | { |
||
| 94 | $this->globalParams = $v; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Set a single param |
||
| 99 | * |
||
| 100 | * @param string $key |
||
| 101 | * @param mixed $value |
||
| 102 | */ |
||
| 103 | public function setGlobalParam($key, $value) |
||
| 104 | { |
||
| 105 | $this->globalParams[$key] = $value; |
||
| 106 | } |
||
| 107 | |||
| 108 | protected $returnHandlers = array(); |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Adds a new return handler to the list of handlers. |
||
| 112 | * |
||
| 113 | * Handlers must implement the 'handleReturn' method, |
||
| 114 | * the result of which is returned to the caller |
||
| 115 | * |
||
| 116 | * @param $name |
||
| 117 | * The name of the handler, which should match the |
||
| 118 | * 'return' param of the method definition |
||
| 119 | * @param $object |
||
| 120 | * The object which will handle the return |
||
| 121 | * @return array |
||
| 122 | */ |
||
| 123 | public function addReturnHandler($name, ReturnHandler $object) |
||
| 124 | { |
||
| 125 | $this->returnHandlers[$name] = $object; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Whether or not to persist cookies (eg in a login situation |
||
| 130 | * |
||
| 131 | * @var boolean |
||
| 132 | */ |
||
| 133 | protected $useCookies = false; |
||
| 134 | |||
| 135 | public function setUseCookies($b) |
||
| 136 | { |
||
| 137 | $this->useCookies = $b; |
||
| 138 | } |
||
| 139 | |||
| 140 | protected $maintainSession = false; |
||
| 141 | |||
| 142 | public function setMaintainSession($b) |
||
| 143 | { |
||
| 144 | $this->maintainSession = true; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Basic HTTP Auth details |
||
| 149 | * |
||
| 150 | * @var array |
||
| 151 | */ |
||
| 152 | protected $authInfo; |
||
| 153 | |||
| 154 | public function setAuthInfo($username, $password) |
||
| 155 | { |
||
| 156 | $this->authInfo = array( |
||
| 157 | 'user' => $username, |
||
| 158 | 'pass' => $password, |
||
| 159 | ); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Create a new webapi client |
||
| 164 | * |
||
| 165 | */ |
||
| 166 | public function __construct($url, $methods=null, $globalParams=null) |
||
| 167 | { |
||
| 168 | $this->baseUrl = $url; |
||
| 169 | $this->methods = $methods; |
||
| 170 | $this->globalParams = $globalParams; |
||
| 171 | |||
| 172 | $this->addReturnHandler('xml', new XmlReturnHandler()); |
||
| 173 | $this->addReturnHandler('dom', new DomReturnHandler()); |
||
| 174 | $this->addReturnHandler('json', new JsonReturnHandler()); |
||
| 175 | } |
||
| 176 | |||
| 177 | public function __call($method, $args) |
||
| 178 | { |
||
| 179 | $arg = is_array($args) && count($args) ? $args[0] : null; |
||
| 180 | return $this->callMethod($method, $arg); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Call a method with the passed in arguments |
||
| 185 | * |
||
| 186 | * @param String $method |
||
| 187 | * @param array $args - a mapping of argumentName => argumentValue |
||
| 188 | * @param array $getParams |
||
| 189 | * Specific get params to add in |
||
| 190 | * @param array $postParams |
||
| 191 | * Specific post params to append |
||
| 192 | * @return mixed |
||
| 193 | */ |
||
| 194 | public function callMethod($method, $args) |
||
| 195 | { |
||
| 196 | $methodDetails = isset($this->methods[$method]) ? $this->methods[$method] : null; |
||
| 197 | if (!$methodDetails) { |
||
| 198 | throw new Exception("$method does not have an appropriate mapping"); |
||
| 199 | } |
||
| 200 | |||
| 201 | $body = null; |
||
| 202 | |||
| 203 | // use the method params to try caching the results |
||
| 204 | // need to add in the baseUrl we're connecting to, and any global params |
||
| 205 | // because the cache might be connecting via different users, and the |
||
| 206 | // different users will have different sessions. This might need to be |
||
| 207 | // tweaked to handle separate user logins at a later point in time |
||
| 208 | $uri = $this->baseUrl . (isset($methodDetails['url']) ? $methodDetails['url'] : ''); |
||
| 209 | // check for any replacements that are required |
||
| 210 | if (preg_match_all('/{(\w+)}/', $uri, $matches)) { |
||
| 211 | foreach ($matches[1] as $match) { |
||
| 212 | if (isset($args[$match])) { |
||
| 213 | $uri = str_replace('{'.$match.'}', $args[$match], $uri); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | } |
||
| 217 | |||
| 218 | $cacheKey = md5($uri . $method . var_export($args, true) . var_export($this->globalParams, true)); |
||
| 219 | |||
| 220 | $requestType = isset($methodDetails['method']) ? $methodDetails['method'] : 'GET'; |
||
| 221 | $cache = isset($methodDetails['cache']) ? $methodDetails['cache'] : self::$cache_length; |
||
| 222 | if (mb_strtolower($requestType) == 'get' && $cache) { |
||
| 223 | $body = CacheService::inst()->get($cacheKey); |
||
| 224 | } |
||
| 225 | |||
| 226 | if (!$body) { |
||
| 227 | |||
| 228 | |||
| 229 | // Note that case is important! Some servers won't respond correctly |
||
| 230 | // to get or Get requests |
||
| 231 | $requestType = isset($methodDetails['method']) ? $methodDetails['method'] : 'GET'; |
||
| 232 | |||
| 233 | $client = $this->getClient($uri); |
||
| 234 | $client->setMethod($requestType); |
||
| 235 | |||
| 236 | // set the encoding type |
||
| 237 | $client->setEncType(isset($methodDetails['enctype']) ? $methodDetails['enctype'] : Zend_Http_Client::ENC_URLENCODED); |
||
| 238 | |||
| 239 | $paramMethod = $requestType == 'GET' ? 'setParameterGet' : 'setParameterPost'; |
||
| 240 | if ($this->globalParams) { |
||
| 241 | foreach ($this->globalParams as $key => $value) { |
||
| 242 | $client->$paramMethod($key, $value); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | if (isset($methodDetails['params'])) { |
||
| 247 | $paramNames = $methodDetails['params']; |
||
| 248 | foreach ($paramNames as $index => $pname) { |
||
| 249 | if (isset($args[$pname])) { |
||
| 250 | $client->$paramMethod($pname, $args[$pname]); |
||
| 251 | } elseif (isset($args[$index])) { |
||
| 252 | $client->$paramMethod($pname, $args[$index]); |
||
| 253 | } |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | if (isset($methodDetails['get'])) { |
||
| 258 | foreach ($methodDetails['get'] as $k => $v) { |
||
| 259 | $client->setParameterGet($k, $v); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | if (isset($methodDetails['post'])) { |
||
| 264 | foreach ($methodDetails['post'] as $k => $v) { |
||
| 265 | $client->setParameterPost($k, $v); |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | if (isset($methodDetails['raw']) && $methodDetails['raw']) { |
||
| 270 | $client->setRawData($args['raw_body']); |
||
| 271 | } |
||
| 272 | |||
| 273 | // request away |
||
| 274 | $response = $client->request(); |
||
| 275 | |||
| 276 | if ($response->isSuccessful()) { |
||
| 277 | $body = $response->getBody(); |
||
| 278 | if ($cache) { |
||
| 279 | CacheService::inst()->store($cacheKey, $body, $cache); |
||
| 280 | } |
||
| 281 | } else { |
||
| 282 | if ($response->getStatus() == 500) { |
||
| 283 | error_log("Failure: ".$response->getBody()); |
||
| 284 | error_log(var_export($client, true)); |
||
| 285 | } |
||
| 286 | throw new FailedRequestException("Failed executing $method: ".$response->getMessage()." for request to $uri (".$client->getUri(true).')', $response->getBody()); |
||
| 287 | } |
||
| 288 | } |
||
| 289 | |||
| 290 | $returnType = isset($methodDetails['return']) ? $methodDetails['return'] : 'raw'; |
||
| 291 | |||
| 292 | // see what we need to do with it |
||
| 293 | if (isset($this->returnHandlers[$returnType])) { |
||
| 294 | $handler = $this->returnHandlers[$returnType]; |
||
| 295 | return $handler->handleReturn($body); |
||
| 296 | } else { |
||
| 297 | return $body; |
||
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Call a URL directly, without it being mapped to a configured web method. |
||
| 303 | * |
||
| 304 | * This differs from the above in that the caller already knows what |
||
| 305 | * URL is trying to be called, so we can bypass the business of mapping |
||
| 306 | * arguments all over the place. |
||
| 307 | * |
||
| 308 | * We still maintain the globalParams for this client though |
||
| 309 | * |
||
| 310 | * @param $url |
||
| 311 | * The URL to call |
||
| 312 | * @param $args |
||
| 313 | * Parameters to be passed on the call |
||
| 314 | * @return mixed |
||
| 315 | */ |
||
| 316 | public function callUrl($url, $args = array(), $returnType = 'raw', $requestType = 'GET', $cache = 300, $enctype = Zend_Http_Client::ENC_URLENCODED) |
||
| 317 | { |
||
| 318 | $body = null; |
||
| 319 | // use the method params to try caching the results |
||
| 320 | // need to add in the baseUrl we're connecting to, and any global params |
||
| 321 | // because the cache might be connecting via different users, and the |
||
| 322 | // different users will have different sessions. This might need to be |
||
| 323 | // tweaked to handle separate user logins at a later point in time |
||
| 324 | $cacheKey = md5($url . $requestType . var_export($args, true) . var_export($this->globalParams, true)); |
||
| 325 | |||
| 326 | $requestType = isset($methodDetails['method']) ? $methodDetails['method'] : 'GET'; |
||
| 327 | |||
| 328 | if (mb_strtolower($requestType) == 'get' && $cache) { |
||
| 329 | $body = CacheService::inst()->get($cacheKey); |
||
| 330 | } |
||
| 331 | |||
| 332 | if (!$body) { |
||
| 333 | $uri = $url; |
||
| 334 | $client = $this->getClient($uri); |
||
| 335 | $client->setMethod($requestType); |
||
| 336 | // set the encoding type |
||
| 337 | $client->setEncType($enctype); |
||
| 338 | $paramMethod = 'setParameter'.$requestType; |
||
| 339 | // make sure to add the alfTicket parameter |
||
| 340 | if ($this->globalParams) { |
||
| 341 | foreach ($this->globalParams as $key => $value) { |
||
| 342 | $client->$paramMethod($key, $value); |
||
| 343 | } |
||
| 344 | } |
||
| 345 | |||
| 346 | foreach ($args as $index => $pname) { |
||
| 347 | $client->$paramMethod($index, $pname); |
||
| 348 | } |
||
| 349 | |||
| 350 | // request away |
||
| 351 | $response = $client->request(); |
||
| 352 | |||
| 353 | if ($response->isSuccessful()) { |
||
| 354 | $body = $response->getBody(); |
||
| 355 | if ($cache) { |
||
| 356 | CacheService::inst()->store($cacheKey, $body, $cache); |
||
| 357 | } |
||
| 358 | } else { |
||
| 359 | if ($response->getStatus() == 500) { |
||
| 360 | error_log("Failure: ".$response->getBody()); |
||
| 361 | error_log(var_export($client, true)); |
||
| 362 | } |
||
| 363 | throw new FailedRequestException("Failed executing $url: ".$response->getMessage()." for request to $uri (".$client->getUri(true).')', $response->getBody()); |
||
| 364 | } |
||
| 365 | } |
||
| 366 | |||
| 367 | // see what we need to do with it |
||
| 368 | if (isset($this->returnHandlers[$returnType])) { |
||
| 369 | $handler = $this->returnHandlers[$returnType]; |
||
| 370 | return $handler->handleReturn($body); |
||
| 371 | } else { |
||
| 372 | return $body; |
||
| 373 | } |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * The HTTP Client being used during the life of this request |
||
| 378 | * |
||
| 379 | * @var Zend_Http_Client |
||
| 380 | */ |
||
| 381 | protected $httpClient = null; |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Create and return the http client, defined in a separate method |
||
| 385 | * for testing purposes |
||
| 386 | * |
||
| 387 | * @return Zend_Http_Client |
||
| 388 | */ |
||
| 389 | protected function getClient($uri) |
||
| 390 | { |
||
| 391 | // TODO For some reason the Alfresco client goes into an infinite loop when returning |
||
| 392 | // the children of an item (when you call getChildren on the company home) |
||
| 393 | // it returns itself as its own child, unless you recreate the client. It seems |
||
| 394 | // to maintain all the request body... or something weird. |
||
| 395 | if (!$this->httpClient || !$this->maintainSession) { |
||
| 396 | $this->httpClient = new Zend_Http_Client( |
||
| 397 | $uri, |
||
| 398 | array( |
||
| 399 | 'maxredirects' => 0, |
||
| 400 | 'timeout' => 10 |
||
| 401 | ) |
||
| 402 | ); |
||
| 403 | |||
| 404 | if ($this->useCookies) { |
||
| 405 | $this->httpClient->setCookieJar(); |
||
| 406 | } |
||
| 407 | } else { |
||
| 408 | $this->httpClient->setUri($uri); |
||
| 409 | } |
||
| 410 | |||
| 411 | // clear it out |
||
| 412 | if ($this->maintainSession) { |
||
| 413 | $this->httpClient->resetParameters(); |
||
| 414 | } |
||
| 415 | |||
| 416 | if ($this->authInfo) { |
||
| 417 | $this->httpClient->setAuth($this->authInfo['user'], $this->authInfo['pass']); |
||
| 418 | } |
||
| 419 | |||
| 420 | return $this->httpClient; |
||
| 421 | } |
||
| 494 |