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 array $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 | * @return string |
||
| 66 | */ |
||
| 67 | 1 | public function getApiVersion() |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Sets API version. |
||
| 74 | * @param string $apiVersion Currently, 'v2' only. |
||
| 75 | */ |
||
| 76 | 1 | public function setApiVersion($apiVersion) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Returns auth credentials |
||
| 87 | * @return array |
||
| 88 | */ |
||
| 89 | 1 | public function getAuthCredentials() |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Sets Auth credentials. |
||
| 96 | * @param array $authCredentials |
||
| 97 | */ |
||
| 98 | 1 | public function setAuthCredentials($authCredentials) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Returns access token information as array. |
||
| 109 | * @return array |
||
| 110 | */ |
||
| 111 | 1 | public function getAccessToken() |
|
| 112 | { |
||
| 113 | 1 | $accessToken = array(); |
|
| 114 | 1 | if (is_array($this->authCredentials)) { |
|
| 115 | 1 | if (isset($this->authCredentials['access_token'])) { |
|
| 116 | 1 | $accessToken['access_token'] = $this->authCredentials['access_token']; |
|
| 117 | } |
||
| 118 | |||
| 119 | 1 | if (isset($this->authCredentials['access_token_timestamp'])) { |
|
| 120 | $accessToken['access_token_timestamp'] = $this->authCredentials['access_token_timestamp']; |
||
| 121 | } |
||
| 122 | |||
| 123 | 1 | if (isset($this->authCredentials['token_type'])) { |
|
| 124 | $accessToken['token_type'] = $this->authCredentials['token_type']; |
||
| 125 | } |
||
| 126 | |||
| 127 | 1 | if (isset($this->authCredentials['expires_in'])) { |
|
| 128 | $accessToken['expires_in'] = $this->authCredentials['expires_in']; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | 1 | return $accessToken; |
|
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Returns refresh token. |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | 1 | public function getRefreshToken() |
|
| 140 | { |
||
| 141 | 1 | if (is_array($this->authCredentials)) { |
|
| 142 | 1 | if (isset($this->authCredentials['refresh_token'])) { |
|
| 143 | return $this->authCredentials['refresh_token']; |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | 1 | return null; |
|
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Sends an HTTP request to the given URL and returns response in form of array. |
||
| 152 | * @param string $url The URL of Optimizely endpoint (relative, without host and API version). |
||
| 153 | * @param array $queryParams The list of query parameters. |
||
| 154 | * @param string $method HTTP method (GET or POST). |
||
| 155 | * @param array $postData Data send in request body (only for POST method). |
||
| 156 | * @param array $expectedResponseCodes List of HTTP response codes treated as success. |
||
| 157 | * @return array Optimizely response in form of array. |
||
| 158 | * @throws \Exception |
||
| 159 | */ |
||
| 160 | 1 | public function sendApiRequest($url, $queryParams = array(), $method='GET', |
|
| 161 | $postData = array(), $expectedResponseCodes = array(200)) |
||
| 162 | { |
||
| 163 | // If access token has expired, try to get another one with refresh token. |
||
| 164 | 1 | if ($this->isAccessTokenExpired() && $this->getRefreshToken()!=null) { |
|
| 165 | $this->getAccessTokenByRefreshToken(); |
||
| 166 | } |
||
| 167 | |||
| 168 | // Produce absolute URL |
||
| 169 | 1 | $url = 'https://api.optimizely.com/' . $this->apiVersion . $url; |
|
| 170 | |||
| 171 | 1 | $result = $this->sendHttpRequest($url, $queryParams, $method, |
|
| 172 | $postData, $expectedResponseCodes); |
||
| 173 | |||
| 174 | return $result; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Sends an HTTP request to the given URL and returns response in form of array. |
||
| 179 | * @param string $url The URL of Optimizely endpoint. |
||
| 180 | * @param array $queryParams The list of query parameters. |
||
| 181 | * @param string $method HTTP method (GET or POST). |
||
| 182 | * @param array $postData Data send in request body (only for POST method). |
||
| 183 | * @param array $expectedResponseCodes List of HTTP response codes treated as success. |
||
| 184 | * @return array Optimizely response in form of array. |
||
| 185 | * @throws \Exception |
||
| 186 | */ |
||
| 187 | 1 | private function sendHttpRequest($url, $queryParams = array(), $method='GET', |
|
| 188 | $postData = array(), $expectedResponseCodes = array(200)) |
||
| 189 | { |
||
| 190 | // Check if CURL is initialized (it should have been initialized in |
||
| 191 | // constructor). |
||
| 192 | 1 | if ($this->curlHandle==false) { |
|
| 193 | throw new \Exception('CURL is not initialized', -1); |
||
| 194 | } |
||
| 195 | |||
| 196 | 1 | if ($method!='GET' && $method!='POST' && $method!='PUT' && |
|
| 197 | 1 | $method!='PATCH' && $method!='DELETE') { |
|
| 198 | throw new \Exception('Invalid HTTP method passed: ' . $method, -1); |
||
| 199 | } |
||
| 200 | |||
| 201 | 1 | if (!isset($this->authCredentials['access_token'])) { |
|
| 202 | throw new \Exception('OAuth access token is not set. You should pass ' . |
||
| 203 | 'it to the class constructor when initializing the Optimizely client.', -1); |
||
| 204 | } |
||
| 205 | |||
| 206 | // Append query parameters to URL. |
||
| 207 | 1 | if (count($queryParams)!=0) { |
|
| 208 | $query = http_build_query($queryParams); |
||
| 209 | $url .= '?' . $query; |
||
| 210 | } |
||
| 211 | |||
| 212 | $headers = array( |
||
| 213 | 1 | "Authorization: Bearer " . $this->authCredentials['access_token'], |
|
| 214 | 1 | "Content-Type: application/json" |
|
| 215 | ); |
||
| 216 | 1 | $content = ''; |
|
| 217 | 1 | if (count($postData)!=0) { |
|
| 218 | $content = json_encode($postData); |
||
| 219 | } |
||
| 220 | 1 | $headers[] = "Content-length:" . strlen($content); |
|
| 221 | |||
| 222 | // Set HTTP options. |
||
| 223 | 1 | curl_setopt($this->curlHandle, CURLOPT_URL, $url); |
|
| 224 | 1 | curl_setopt($this->curlHandle, CURLOPT_CUSTOMREQUEST, $method); |
|
| 225 | 1 | if (count($postData)!=0) { |
|
| 226 | curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, $content); |
||
| 227 | } |
||
| 228 | 1 | curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, true); |
|
| 229 | 1 | curl_setopt($this->curlHandle, CURLOPT_HEADER, false); |
|
| 230 | 1 | curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headers); |
|
| 231 | |||
| 232 | // Execute HTTP request and get response. |
||
| 233 | 1 | $result = curl_exec($this->curlHandle); |
|
| 234 | 1 | if ($result === false) { |
|
| 235 | $code = curl_errno($this->curlHandle); |
||
| 236 | $error = curl_error($this->curlHandle); |
||
| 237 | throw new \Exception("Failed to send HTTP request $method '$url', the error code was $code, error message was: '$error'", -1); |
||
| 238 | } |
||
| 239 | |||
| 240 | // Check HTTP response code. |
||
| 241 | 1 | $info = curl_getinfo($this->curlHandle); |
|
| 242 | 1 | if (!in_array($info['http_code'], $expectedResponseCodes)) { |
|
| 243 | 1 | throw new \Exception('Unexpected HTTP response code: ' . $info['http_code'] . |
|
| 244 | 1 | '. Request was ' . $method . ' "' . $url . '". Response was "' . $result . '"', |
|
| 245 | 1 | $info['http_code']); |
|
| 246 | } |
||
| 247 | |||
| 248 | // JSON-decode response. |
||
| 249 | $decodedResult = json_decode($result, true); |
||
| 250 | if (!is_array($decodedResult)) { |
||
| 251 | throw new \Exception('Could not JSON-decode the Optimizely response. Request was ' . |
||
| 252 | $method . ' "' . $url . '". The response was: "' . $result . '"', |
||
| 253 | $info['http_code']); |
||
| 254 | } |
||
| 255 | |||
| 256 | // Return the response in form of array. |
||
| 257 | return $decodedResult; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Determines whether the access token has expired or not. Returns true if |
||
| 262 | * token has expired; false if token is valid. |
||
| 263 | * @return boolean |
||
| 264 | */ |
||
| 265 | 1 | private function isAccessTokenExpired() |
|
| 287 | |||
| 288 | /** |
||
| 289 | * This method retrieves the access token by refresh token. |
||
| 290 | * @return array |
||
| 291 | */ |
||
| 292 | private function getAccessTokenByRefreshToken() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Provides access to API services (experiments, campaigns, etc.) |
||
| 323 | * @method Audiences audiences() |
||
| 324 | * @method Campaigns campaigns() |
||
| 325 | * @method Events events() |
||
| 326 | * @method Experiment experiments() |
||
| 327 | * @method Pages pages() |
||
| 328 | * @method Projects projects() |
||
| 329 | */ |
||
| 330 | 1 | public function __call($name, $arguments) |
|
| 360 | } |