Complex classes like OptimizelyApiClient 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 OptimizelyApiClient, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class OptimizelyApiClient |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Auth credentials. |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | private $authCredentials = array(); |
||
| 20 | |||
| 21 | /** |
||
| 22 | * API version. |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | private $apiVersion; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * CURL handle. |
||
| 29 | * @var resource |
||
| 30 | */ |
||
| 31 | private $curlHandle; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Instantiated services (used internally). |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private $services = array(); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Constructor. |
||
| 41 | * @param string $authCredentials Auth credentials. |
||
| 42 | * @param string $apiVersion Optional. Currently supported 'v2' only. |
||
| 43 | */ |
||
| 44 | 8 | public function __construct($authCredentials, $apiVersion='v2') |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Returns API version (currently it is always 'v2'). |
||
| 65 | */ |
||
| 66 | 1 | public function getApiVersion() |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Sets API version. |
||
| 73 | * @param string $apiVersion Currently, 'v2' only. |
||
| 74 | */ |
||
| 75 | 1 | public function setApiVersion($apiVersion) |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Returns auth credentials |
||
| 86 | * @return array |
||
| 87 | */ |
||
| 88 | 1 | public function getAuthCredentials() |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Sets Auth credentials. |
||
| 95 | * @param array $authCredentials |
||
| 96 | */ |
||
| 97 | 1 | public function setAuthCredentials($authCredentials) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Returns access token information as array. |
||
| 108 | * @return array |
||
| 109 | */ |
||
| 110 | 1 | public function getAccessToken() |
|
| 111 | { |
||
| 112 | 1 | $accessToken = array(); |
|
| 113 | 1 | if (is_array($this->authCredentials)) { |
|
| 114 | 1 | if (isset($this->authCredentials['access_token'])) { |
|
| 115 | 1 | $accessToken['access_token'] = $this->authCredentials['access_token']; |
|
| 116 | } |
||
| 117 | |||
| 118 | 1 | if (isset($this->authCredentials['access_token_timestamp'])) { |
|
| 119 | $accessToken['access_token_timestamp'] = $this->authCredentials['access_token_timestamp']; |
||
| 120 | } |
||
| 121 | |||
| 122 | 1 | if (isset($this->authCredentials['token_type'])) { |
|
| 123 | $accessToken['token_type'] = $this->authCredentials['token_type']; |
||
| 124 | } |
||
| 125 | |||
| 126 | 1 | if (isset($this->authCredentials['expires_in'])) { |
|
| 127 | $accessToken['expires_in'] = $this->authCredentials['expires_in']; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | 1 | return $accessToken; |
|
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Returns refresh token. |
||
| 136 | * @return string |
||
| 137 | */ |
||
| 138 | 1 | public function getRefreshToken() |
|
| 139 | { |
||
| 140 | 1 | if (is_array($this->authCredentials)) { |
|
| 141 | 1 | if (isset($this->authCredentials['refresh_token'])) { |
|
| 142 | return $this->authCredentials['refresh_token']; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | 1 | return null; |
|
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Sends an HTTP request to the given URL and returns response in form of array. |
||
| 151 | * @param string $url The URL of Optimizely endpoint (relative, without host and API version). |
||
| 152 | * @param array $queryParams The list of query parameters. |
||
| 153 | * @param string $method HTTP method (GET or POST). |
||
| 154 | * @param array $postData Data send in request body (only for POST method). |
||
| 155 | * @param array $expectedResponseCodes List of HTTP response codes treated as success. |
||
| 156 | * @return array Optimizely response in form of array. |
||
| 157 | * @throws \Exception |
||
| 158 | */ |
||
| 159 | 1 | public function sendApiRequest($url, $queryParams = array(), $method='GET', |
|
| 160 | $postData = array(), $expectedResponseCodes = array(200)) |
||
| 161 | { |
||
| 162 | // If access token has expired, try to get another one with refresh token. |
||
| 163 | 1 | if ($this->isAccessTokenExpired() && $this->getRefreshToken()!=null) { |
|
| 164 | $this->getAccessTokenByRefreshToken(); |
||
| 165 | } |
||
| 166 | |||
| 167 | // Produce absolute URL |
||
| 168 | 1 | $url = 'https://api.optimizely.com/' . $this->apiVersion . $url; |
|
| 169 | |||
| 170 | 1 | $result = $this->sendHttpRequest($url, $queryParams, $method, |
|
| 171 | $postData, $expectedResponseCodes); |
||
| 172 | |||
| 173 | return $result; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Sends an HTTP request to the given URL and returns response in form of array. |
||
| 178 | * @param string $url The URL of Optimizely endpoint. |
||
| 179 | * @param array $queryParams The list of query parameters. |
||
| 180 | * @param string $method HTTP method (GET or POST). |
||
| 181 | * @param array $postData Data send in request body (only for POST method). |
||
| 182 | * @param array $expectedResponseCodes List of HTTP response codes treated as success. |
||
| 183 | * @return array Optimizely response in form of array. |
||
| 184 | * @throws \Exception |
||
| 185 | */ |
||
| 186 | 1 | private function sendHttpRequest($url, $queryParams = array(), $method='GET', |
|
| 187 | $postData = array(), $expectedResponseCodes = array(200)) |
||
| 188 | { |
||
| 189 | // Check if CURL is initialized (it should have been initialized in |
||
| 190 | // constructor). |
||
| 191 | 1 | if ($this->curlHandle==false) { |
|
| 192 | throw new \Exception('CURL is not initialized', -1); |
||
| 193 | } |
||
| 194 | |||
| 195 | 1 | if ($method!='GET' && $method!='POST' && $method!='PUT' && |
|
| 196 | 1 | $method!='PATCH' && $method!='DELETE') { |
|
| 197 | throw new \Exception('Invalid HTTP method passed: ' . $method, -1); |
||
| 198 | } |
||
| 199 | |||
| 200 | 1 | if (!isset($this->authCredentials['access_token'])) { |
|
| 201 | throw new \Exception('OAuth access token is not set. You should pass ' . |
||
| 202 | 'it to the class constructor when initializing the Optimizely client.', -1); |
||
| 203 | } |
||
| 204 | |||
| 205 | // Append query parameters to URL. |
||
| 206 | 1 | if (count($queryParams)!=0) { |
|
| 207 | $query = http_build_query($queryParams); |
||
| 208 | $url .= '?' . $query; |
||
| 209 | } |
||
| 210 | |||
| 211 | $headers = array( |
||
| 212 | 1 | "Authorization: Bearer " . $this->authCredentials['access_token'], |
|
| 213 | 1 | "Content-Type: application/json" |
|
| 214 | ); |
||
| 215 | 1 | $content = ''; |
|
| 216 | 1 | if (count($postData)!=0) { |
|
| 217 | $content = json_encode($postData); |
||
| 218 | } |
||
| 219 | 1 | $headers[] = "Content-length:" . strlen($content); |
|
| 220 | |||
| 221 | // Set HTTP options. |
||
| 222 | 1 | curl_setopt($this->curlHandle, CURLOPT_URL, $url); |
|
| 223 | 1 | curl_setopt($this->curlHandle, CURLOPT_CUSTOMREQUEST, $method); |
|
| 224 | 1 | if (count($postData)!=0) { |
|
| 225 | curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, $content); |
||
| 226 | } |
||
| 227 | 1 | curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, true); |
|
| 228 | 1 | curl_setopt($this->curlHandle, CURLOPT_HEADER, false); |
|
| 229 | 1 | curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headers); |
|
| 230 | |||
| 231 | // Execute HTTP request and get response. |
||
| 232 | 1 | $result = curl_exec($this->curlHandle); |
|
| 233 | 1 | if ($result === false) { |
|
| 234 | $code = curl_errno($this->curlHandle); |
||
| 235 | $error = curl_error($this->curlHandle); |
||
| 236 | throw new \Exception("Failed to send HTTP request $method '$url', the error code was $code, error message was: '$error'", -1); |
||
| 237 | } |
||
| 238 | |||
| 239 | // Check HTTP response code. |
||
| 240 | 1 | $info = curl_getinfo($this->curlHandle); |
|
| 241 | 1 | if (!in_array($info['http_code'], $expectedResponseCodes)) { |
|
| 242 | 1 | throw new \Exception('Unexpected HTTP response code: ' . $info['http_code'] . |
|
| 243 | 1 | '. Request was ' . $method . ' "' . $url . '". Response was "' . $result . '"', |
|
| 244 | 1 | $info['http_code']); |
|
| 245 | } |
||
| 246 | |||
| 247 | // JSON-decode response. |
||
| 248 | $decodedResult = json_decode($result, true); |
||
| 249 | if (!is_array($decodedResult)) { |
||
| 250 | throw new \Exception('Could not JSON-decode the Optimizely response. Request was ' . |
||
| 251 | $method . ' "' . $url . '". The response was: "' . $result . '"', |
||
| 252 | $info['http_code']); |
||
| 253 | } |
||
| 254 | |||
| 255 | // Return the response in form of array. |
||
| 256 | return $decodedResult; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Determines whether the access token has expired or not. Returns true if |
||
| 261 | * token has expired; false if token is valid. |
||
| 262 | * @return boolean |
||
| 263 | */ |
||
| 264 | 1 | private function isAccessTokenExpired() |
|
| 286 | |||
| 287 | /** |
||
| 288 | * This method retrieves the access token by refresh token. |
||
| 289 | */ |
||
| 290 | private function getAccessTokenByRefreshToken() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Provides access to API services (experiments, campaigns, etc.) |
||
| 321 | */ |
||
| 322 | 1 | public function __call($name, $arguments) |
|
| 352 | } |