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() |
|
134 | |||
135 | /** |
||
136 | * Returns refresh token. |
||
137 | * @return string |
||
138 | */ |
||
139 | 1 | public function getRefreshToken() |
|
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', |
|
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', |
|
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 | } |