Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
16 | class OptimizelyApiClient |
||
17 | { |
||
|
|||
18 | /** |
||
19 | * Auth credentials. |
||
20 | * @var array |
||
21 | */ |
||
22 | private $authCredentials = array(); |
||
23 | |||
24 | /** |
||
25 | * API version. |
||
26 | * @var string |
||
27 | */ |
||
28 | private $apiVersion; |
||
29 | |||
30 | /** |
||
31 | * CURL handle. |
||
32 | * @var resource |
||
33 | */ |
||
34 | private $curlHandle; |
||
35 | |||
36 | /** |
||
37 | * Debugging information. Typically contains the last HTTP request/response. |
||
38 | * @var type |
||
39 | */ |
||
40 | private $diagnosticsInfo = array(); |
||
41 | |||
42 | /** |
||
43 | * Instantiated services (used internally). |
||
44 | * @var array |
||
45 | */ |
||
46 | private $services = array(); |
||
47 | |||
48 | /** |
||
49 | * Constructor. |
||
50 | * @param array $authCredentials Auth credentials. |
||
51 | * @param string $apiVersion Optional. Currently supported 'v2' only. |
||
52 | */ |
||
53 | 13 | public function __construct($authCredentials, $apiVersion='v2') |
|
72 | |||
73 | /** |
||
74 | * Returns API version (currently it is always 'v2'). |
||
75 | * @return string |
||
76 | */ |
||
77 | 1 | public function getApiVersion() |
|
81 | |||
82 | /** |
||
83 | * Sets API version. |
||
84 | * @param string $apiVersion Currently, 'v2' only. |
||
85 | */ |
||
86 | 9 | public function setApiVersion($apiVersion) |
|
87 | { |
||
88 | 9 | if ($apiVersion!='v2') { |
|
89 | 1 | throw new Exception('Invalid API version passed'); |
|
90 | } |
||
91 | |||
92 | 9 | $this->apiVersion = $apiVersion; |
|
93 | 9 | } |
|
94 | |||
95 | /** |
||
96 | * Returns auth credentials |
||
97 | * @return array |
||
98 | */ |
||
99 | 1 | public function getAuthCredentials() |
|
103 | |||
104 | /** |
||
105 | * Sets Auth credentials. |
||
106 | * @param array $authCredentials |
||
107 | */ |
||
108 | 10 | public function setAuthCredentials($authCredentials) |
|
116 | |||
117 | /** |
||
118 | * Returns access token information as array. |
||
119 | * @return array |
||
120 | */ |
||
121 | 1 | public function getAccessToken() |
|
144 | |||
145 | /** |
||
146 | * Returns refresh token. |
||
147 | * @return string |
||
148 | */ |
||
149 | 2 | public function getRefreshToken() |
|
159 | |||
160 | /** |
||
161 | * Sends an HTTP request to the given URL and returns response in form of array. |
||
162 | * @param string $url The URL of Optimizely endpoint (relative, without host and API version). |
||
163 | * @param array $queryParams The list of query parameters. |
||
164 | * @param string $method HTTP method (GET or POST). |
||
165 | * @param array $postData Data send in request body (only for POST method). |
||
166 | * @return array Optimizely response in form of array. |
||
167 | * @throws Exception |
||
168 | */ |
||
169 | 2 | public function sendApiRequest($url, $queryParams = array(), $method='GET', |
|
184 | |||
185 | /** |
||
186 | * Sends an HTTP request to the given URL and returns response in form of array. |
||
187 | * @param string $url The URL of Optimizely endpoint. |
||
188 | * @param array $queryParams The list of query parameters. |
||
189 | * @param string $method HTTP method (GET or POST). |
||
190 | * @param array $postData Data send in request body (only for POST method). |
||
191 | * @return array Optimizely response in form of array. |
||
192 | * @throws Exception |
||
193 | */ |
||
194 | 2 | private function sendHttpRequest($url, $queryParams = array(), $method='GET', |
|
195 | $postData = array()) |
||
196 | { |
||
197 | // Reset diagnostics info. |
||
198 | 2 | $this->diagnosticsInfo = array(); |
|
199 | |||
200 | // Check if CURL is initialized (it should have been initialized in |
||
201 | // constructor). |
||
202 | 2 | if ($this->curlHandle==false) { |
|
203 | throw new Exception('CURL is not initialized', |
||
204 | Exception::CODE_CURL_ERROR); |
||
205 | } |
||
206 | |||
207 | 2 | if ($method!='GET' && $method!='POST' && $method!='PUT' && |
|
208 | 2 | $method!='PATCH' && $method!='DELETE') { |
|
209 | throw new Exception('Invalid HTTP method passed: ' . $method); |
||
210 | } |
||
211 | |||
212 | 2 | if (!isset($this->authCredentials['access_token'])) { |
|
213 | throw new Exception('OAuth access token is not set. You should pass ' . |
||
214 | 'it to the class constructor when initializing the Optimizely client.'); |
||
215 | } |
||
216 | |||
217 | // Append query parameters to URL. |
||
218 | 2 | if (count($queryParams)!=0) { |
|
219 | $query = http_build_query($queryParams); |
||
220 | $url .= '?' . $query; |
||
221 | } |
||
222 | |||
223 | $headers = array( |
||
224 | 2 | "Authorization: Bearer " . $this->authCredentials['access_token'], |
|
225 | "Content-Type: application/json" |
||
226 | 2 | ); |
|
227 | 2 | $content = ''; |
|
228 | 2 | if (count($postData)!=0) { |
|
229 | $content = json_encode($postData); |
||
230 | } |
||
231 | 2 | $headers[] = "Content-length:" . strlen($content); |
|
232 | |||
233 | // Set HTTP options. |
||
234 | 2 | if (!function_exists('curl_reset')) |
|
235 | 2 | { |
|
236 | function curl_reset(&$ch) |
||
395 | |||
396 | /** |
||
397 | * Determines whether the access token has expired or not. Returns true if |
||
398 | * token has expired; false if token is valid. |
||
399 | * @return boolean |
||
400 | */ |
||
401 | 2 | private function isAccessTokenExpired() |
|
423 | |||
424 | /** |
||
425 | * This method retrieves the access token by refresh token. |
||
426 | * @return array |
||
427 | * @throw Exception |
||
428 | */ |
||
429 | 1 | private function getAccessTokenByRefreshToken() |
|
461 | |||
462 | /** |
||
463 | * Provides access to API services (experiments, campaigns, etc.) |
||
464 | * @method Audiences audiences() |
||
465 | * @method Campaigns campaigns() |
||
466 | * @method Events events() |
||
467 | * @method Experiment experiments() |
||
468 | * @method Pages pages() |
||
469 | * @method Projects projects() |
||
470 | */ |
||
471 | 1 | public function __call($name, $arguments) |
|
501 | |||
502 | /** |
||
503 | * Returns last HTTP request/response information (for diagnostics/debugging |
||
504 | * purposes): |
||
505 | * - request |
||
506 | * - method |
||
507 | * - headers |
||
508 | * - content |
||
509 | * - response |
||
510 | * - http_code |
||
511 | * - headers |
||
512 | * - content |
||
513 | * @return array |
||
514 | */ |
||
515 | 1 | public function getDiagnosticsInfo() |
|
519 | } |