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) |
|
109 | { |
||
110 | 10 | if (!is_array($authCredentials) || count($authCredentials)==0) { |
|
111 | 1 | throw new Exception('Auth credentials must be an non-empty array'); |
|
112 | } |
||
113 | |||
114 | 9 | $this->authCredentials = $authCredentials; |
|
115 | 9 | } |
|
116 | |||
117 | /** |
||
118 | * Returns access token information as array. |
||
119 | * @return array |
||
120 | */ |
||
121 | 1 | public function getAccessToken() |
|
122 | { |
||
123 | 1 | $accessToken = array(); |
|
124 | 1 | if (is_array($this->authCredentials)) { |
|
125 | 1 | if (isset($this->authCredentials['access_token'])) { |
|
126 | 1 | $accessToken['access_token'] = $this->authCredentials['access_token']; |
|
127 | 1 | } |
|
128 | |||
129 | 1 | if (isset($this->authCredentials['access_token_timestamp'])) { |
|
130 | 1 | $accessToken['access_token_timestamp'] = $this->authCredentials['access_token_timestamp']; |
|
131 | 1 | } |
|
132 | |||
133 | 1 | if (isset($this->authCredentials['token_type'])) { |
|
134 | 1 | $accessToken['token_type'] = $this->authCredentials['token_type']; |
|
135 | 1 | } |
|
136 | |||
137 | 1 | if (isset($this->authCredentials['expires_in'])) { |
|
138 | 1 | $accessToken['expires_in'] = $this->authCredentials['expires_in']; |
|
139 | 1 | } |
|
140 | 1 | } |
|
141 | |||
142 | 1 | return $accessToken; |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * Returns refresh token. |
||
147 | * @return string |
||
148 | */ |
||
149 | 2 | public function getRefreshToken() |
|
150 | { |
||
151 | 2 | if (is_array($this->authCredentials)) { |
|
152 | 2 | if (isset($this->authCredentials['refresh_token'])) { |
|
153 | 1 | return $this->authCredentials['refresh_token']; |
|
154 | } |
||
155 | 1 | } |
|
156 | |||
157 | 1 | return null; |
|
158 | } |
||
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', |
|
170 | $postData = array()) |
||
171 | { |
||
172 | // If access token has expired, try to get another one with refresh token. |
||
173 | 2 | if ($this->isAccessTokenExpired() && $this->getRefreshToken()!=null) { |
|
174 | 1 | $this->getAccessTokenByRefreshToken(); |
|
175 | } |
||
176 | |||
177 | // Produce absolute URL |
||
178 | 1 | $url = 'https://api.optimizely.com/' . $this->apiVersion . $url; |
|
179 | |||
180 | 1 | $result = $this->sendHttpRequest($url, $queryParams, $method, $postData); |
|
181 | |||
182 | return $result; |
||
183 | } |
||
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', |
|
387 | |||
388 | /** |
||
389 | * Determines whether the access token has expired or not. Returns true if |
||
390 | * token has expired; false if token is valid. |
||
391 | * @return boolean |
||
392 | */ |
||
393 | 2 | private function isAccessTokenExpired() |
|
394 | { |
||
395 | 2 | if(!isset($this->authCredentials['access_token'])) { |
|
396 | return true; // We do not have access token. |
||
397 | } |
||
398 | |||
399 | 2 | if (!isset($this->authCredentials['expires_in']) || |
|
400 | 2 | !isset($this->authCredentials['access_token_timestamp'])) { |
|
401 | 1 | return true; // Assume it has expired, since we can't tell for sure. |
|
402 | } |
||
403 | |||
404 | 1 | $expiresIn = $this->authCredentials['expires_in']; |
|
405 | 1 | $timestamp = $this->authCredentials['access_token_timestamp']; |
|
406 | |||
407 | 1 | if ($timestamp + $expiresIn < time()) { |
|
408 | // Access token has expired. |
||
409 | 1 | return true; |
|
410 | } |
||
411 | |||
412 | // Access token is valid. |
||
413 | return false; |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * This method retrieves the access token by refresh token. |
||
418 | * @return array |
||
419 | * @throw Exception |
||
420 | */ |
||
421 | 1 | private function getAccessTokenByRefreshToken() |
|
422 | { |
||
423 | 1 | if (!isset($this->authCredentials['client_id'])) |
|
424 | 1 | throw new Exception('OAuth 2.0 client ID is not set'); |
|
425 | |||
426 | 1 | if (!isset($this->authCredentials['client_secret'])) |
|
427 | 1 | throw new Exception('OAuth 2.0 client secret is not set'); |
|
428 | |||
429 | 1 | if (!isset($this->authCredentials['refresh_token'])) |
|
430 | 1 | throw new Exception('Refresh token is not set'); |
|
431 | |||
432 | 1 | $clientId = $this->authCredentials['client_id']; |
|
433 | 1 | $clientSecret = $this->authCredentials['client_secret']; |
|
434 | 1 | $refreshToken = $this->authCredentials['refresh_token']; |
|
435 | |||
436 | 1 | $url = "https://app.optimizely.com/oauth2/token?refresh_token=$refreshToken" . |
|
437 | 1 | "&client_id=$clientId&client_secret=$clientSecret&grant_type=refresh_token"; |
|
438 | |||
439 | 1 | $response = $this->sendHttpRequest($url, array(), 'POST'); |
|
440 | $decodedJsonData = $response->getDecodedJsonData(); |
||
441 | |||
442 | if (!isset($decodedJsonData['access_token'])) { |
||
443 | throw new Exception('Not found access token in response. Request URL was "' . |
||
444 | $url. '". Response was "' . print_r(json_encode($decodedJsonData), true). '"', |
||
445 | Exception::CODE_API_ERROR, $response->getHttpCode()); |
||
446 | } |
||
447 | |||
448 | $this->authCredentials['access_token'] = $decodedJsonData['access_token']; |
||
449 | $this->authCredentials['token_type'] = $decodedJsonData['token_type']; |
||
450 | $this->authCredentials['expires_in'] = $decodedJsonData['expires_in']; |
||
451 | $this->authCredentials['access_token_timestamp'] = time(); |
||
452 | } |
||
453 | |||
454 | /** |
||
455 | * Provides access to API services (experiments, campaigns, etc.) |
||
456 | * @method Audiences audiences() |
||
457 | * @method Campaigns campaigns() |
||
458 | * @method Events events() |
||
459 | * @method Experiment experiments() |
||
460 | * @method Pages pages() |
||
461 | * @method Projects projects() |
||
462 | */ |
||
463 | 1 | public function __call($name, $arguments) |
|
493 | |||
494 | /** |
||
495 | * Returns last HTTP request/response information (for diagnostics/debugging |
||
496 | * purposes): |
||
497 | * - request |
||
498 | * - method |
||
499 | * - headers |
||
500 | * - content |
||
501 | * - response |
||
502 | * - http_code |
||
503 | * - headers |
||
504 | * - content |
||
505 | * @return array |
||
506 | */ |
||
507 | 1 | public function getDiagnosticsInfo() |
|
511 | } |