Complex classes like ApiClient 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 ApiClient, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class ApiClient extends AbstractClient |
||
| 15 | { |
||
| 16 | const BASE_URL = 'https://slack.com/api/'; |
||
| 17 | const CONTENT_TYPE = 'application/x-www-form-urlencoded'; |
||
| 18 | |||
| 19 | private $arguments = [ |
||
| 20 | 'rtm.start' => [ |
||
| 21 | 'required' => [ |
||
| 22 | 'token', |
||
| 23 | ], |
||
| 24 | 'optional' => [ |
||
| 25 | 'simple_latest', |
||
| 26 | 'no_unreads', |
||
| 27 | 'mpim_aware', |
||
| 28 | ], |
||
| 29 | ], |
||
| 30 | 'chat.postMessage' => [ |
||
| 31 | 'required' => [ |
||
| 32 | 'token', |
||
| 33 | 'channel', |
||
| 34 | 'text', |
||
| 35 | ], |
||
| 36 | 'optional' => [ |
||
| 37 | 'parse', |
||
| 38 | 'link_names', |
||
| 39 | 'attachments', |
||
| 40 | 'unfurl_links', |
||
| 41 | 'unfurl_media', |
||
| 42 | 'username', |
||
| 43 | 'as_user', |
||
| 44 | 'icon_url', |
||
| 45 | 'icon_emoji', |
||
| 46 | ], |
||
| 47 | ], |
||
| 48 | 'oauth.access' => [ |
||
| 49 | 'required' => [ |
||
| 50 | 'client_id', |
||
| 51 | 'client_secret', |
||
| 52 | 'code', |
||
| 53 | ], |
||
| 54 | 'optional' => [ |
||
| 55 | 'redirect_uri', |
||
| 56 | ], |
||
| 57 | ], |
||
| 58 | 'team.info' => [ |
||
| 59 | 'required' => [ |
||
| 60 | 'token', |
||
| 61 | ], |
||
| 62 | ], |
||
| 63 | 'im.list' => [ |
||
| 64 | 'required' => [ |
||
| 65 | 'token', |
||
| 66 | ], |
||
| 67 | ], |
||
| 68 | 'users.list' => [ |
||
| 69 | 'required' => [ |
||
| 70 | 'token', |
||
| 71 | ], |
||
| 72 | 'optional' => [ |
||
| 73 | 'presence', |
||
| 74 | ], |
||
| 75 | ], |
||
| 76 | 'users.info' => [ |
||
| 77 | 'required' => [ |
||
| 78 | 'token', |
||
| 79 | 'user', |
||
| 80 | ], |
||
| 81 | ], |
||
| 82 | ]; |
||
| 83 | |||
| 84 | private $token; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * ApiClient constructor. |
||
| 88 | * |
||
| 89 | * @param null $token |
||
| 90 | */ |
||
| 91 | 43 | public function __construct($token = null) |
|
| 92 | { |
||
| 93 | 43 | $this->setToken($token); |
|
| 94 | 43 | } |
|
| 95 | |||
| 96 | /** |
||
| 97 | * API CURL Call with post method. |
||
| 98 | * |
||
| 99 | * @param string $method |
||
| 100 | * @param array $arguments |
||
| 101 | * |
||
| 102 | * @throws \Exception |
||
| 103 | * |
||
| 104 | * @return mixed |
||
| 105 | */ |
||
| 106 | 33 | public function apiCall($method, array $arguments = []) |
|
| 107 | { |
||
| 108 | try { |
||
| 109 | 33 | $requestBody = $this->prepareRequestBody($method, $arguments); |
|
| 110 | 30 | $response = $this->sendRequest($method, $requestBody); |
|
| 111 | |||
| 112 | 28 | return $this->processResponse($response); |
|
| 113 | 6 | } catch (\Exception $e) { |
|
| 114 | 6 | throw $e; |
|
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param $method |
||
| 120 | * @param $requestBody |
||
| 121 | * |
||
| 122 | * @throws \Exception |
||
| 123 | * |
||
| 124 | * @return mixed|\Psr\Http\Message\ResponseInterface |
||
| 125 | */ |
||
| 126 | 30 | private function sendRequest($method, $requestBody) |
|
| 127 | { |
||
| 128 | try { |
||
| 129 | /** @noinspection PhpUndefinedClassInspection */ |
||
| 130 | 30 | $request = new Request( |
|
| 131 | 30 | 'POST', |
|
| 132 | 30 | self::BASE_URL.$method, |
|
| 133 | 30 | ['Content-Type' => self::CONTENT_TYPE], |
|
| 134 | $requestBody |
||
| 135 | ); |
||
| 136 | |||
| 137 | 30 | return $this->getClient()->send($request); |
|
| 138 | 2 | } catch (\Exception $e) { |
|
| 139 | 2 | throw new \Exception('Failed to send data to the Slack API: '.$e->getMessage()); |
|
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param $method |
||
| 145 | * @param array $arguments |
||
| 146 | * |
||
| 147 | * @throws \Exception |
||
| 148 | * |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | 33 | private function prepareRequestBody($method, array $arguments = []) |
|
| 152 | { |
||
| 153 | 33 | $arguments = array_merge($arguments, $this->getArgs()); |
|
| 154 | |||
| 155 | // check the required arguments are provided |
||
| 156 | try { |
||
| 157 | 33 | $this->validateRequiredArguments($method, $arguments); |
|
| 158 | 3 | } catch (\Exception $e) { |
|
| 159 | 3 | throw new \Exception('Missing required argument(s): '.$e->getMessage()); |
|
| 160 | } |
||
| 161 | |||
| 162 | // filter unwanted arguments |
||
| 163 | 30 | return http_build_query($this->filterArguments($method, $arguments)); |
|
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param $response |
||
| 168 | * |
||
| 169 | * @throws \Exception |
||
| 170 | * |
||
| 171 | * @return mixed |
||
| 172 | */ |
||
| 173 | 28 | private function processResponse(ResponseInterface $response) |
|
| 174 | { |
||
| 175 | 28 | $response = json_decode($response->getBody()->getContents(), true); |
|
| 176 | |||
| 177 | 28 | if (!is_array($response)) { |
|
| 178 | 1 | throw new \Exception('Failed to process response from the Slack API'); |
|
| 179 | } |
||
| 180 | |||
| 181 | 27 | return $response; |
|
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @throws \Exception |
||
| 186 | * |
||
| 187 | * @return array |
||
| 188 | */ |
||
| 189 | 34 | public function getArgs() |
|
| 190 | { |
||
| 191 | return [ |
||
| 192 | 34 | 'token' => $this->getToken(), |
|
| 193 | 34 | 'username' => $this->getConfig()->get('botUsername'), |
|
| 194 | 34 | 'as_user' => $this->getConfig()->get('asUser'), |
|
| 195 | 34 | 'icon_url' => $this->getConfig()->get('iconURL'), |
|
| 196 | ]; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param $arguments |
||
| 201 | * |
||
| 202 | * @return mixed |
||
| 203 | */ |
||
| 204 | 2 | public function chatPostMessage($arguments) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * @param $arguments |
||
| 211 | * |
||
| 212 | * @throws \Exception |
||
| 213 | * |
||
| 214 | * @return mixed |
||
| 215 | */ |
||
| 216 | 1 | public function rtmStart($arguments) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * @throws \Exception |
||
| 223 | * |
||
| 224 | * @return array |
||
| 225 | * @return Team |
||
| 226 | */ |
||
| 227 | 4 | public function teamInfo() |
|
| 237 | |||
| 238 | /** |
||
| 239 | * @return \Botonomous\AbstractBaseSlack|null|void |
||
| 240 | */ |
||
| 241 | 2 | public function teamInfoAsObject() |
|
| 253 | |||
| 254 | /** |
||
| 255 | * List all the Slack users in the team. |
||
| 256 | * |
||
| 257 | * @return array |
||
| 258 | */ |
||
| 259 | 2 | public function usersList() |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Return a user by Slack user id. |
||
| 272 | * |
||
| 273 | * @param $arguments |
||
| 274 | * |
||
| 275 | * @throws \Exception |
||
| 276 | * |
||
| 277 | * @return mixed |
||
| 278 | */ |
||
| 279 | 10 | public function userInfo($arguments) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * @throws \Exception |
||
| 293 | * |
||
| 294 | * @return mixed |
||
| 295 | */ |
||
| 296 | 1 | public function test() |
|
| 300 | |||
| 301 | /** |
||
| 302 | * @throws \Exception |
||
| 303 | * |
||
| 304 | * @return array |
||
| 305 | */ |
||
| 306 | 5 | public function imList() |
|
| 316 | |||
| 317 | /** |
||
| 318 | * @return array |
||
| 319 | */ |
||
| 320 | 4 | public function imListAsObject() |
|
| 335 | |||
| 336 | /** |
||
| 337 | * @param $arguments |
||
| 338 | * |
||
| 339 | * @throws \Exception |
||
| 340 | * |
||
| 341 | * @return mixed |
||
| 342 | */ |
||
| 343 | 5 | public function oauthAccess($arguments) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * @param $method |
||
| 350 | * @param $arguments |
||
| 351 | * |
||
| 352 | * @throws \Exception |
||
| 353 | * |
||
| 354 | * @return bool |
||
| 355 | */ |
||
| 356 | 33 | private function validateRequiredArguments($method, $arguments) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * @param null $method |
||
| 375 | * |
||
| 376 | * @throws \Exception |
||
| 377 | * |
||
| 378 | * @return mixed |
||
| 379 | */ |
||
| 380 | 35 | public function getArguments($method = null) |
|
| 393 | |||
| 394 | /** |
||
| 395 | * @param array $arguments |
||
| 396 | */ |
||
| 397 | 1 | public function setArguments(array $arguments) |
|
| 401 | |||
| 402 | /** |
||
| 403 | * @param $method |
||
| 404 | * @param array $arguments |
||
| 405 | * |
||
| 406 | * @return array |
||
| 407 | */ |
||
| 408 | 31 | public function filterArguments($method, array $arguments) |
|
| 424 | |||
| 425 | /** |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | 34 | public function getToken() |
|
| 437 | |||
| 438 | /** |
||
| 439 | * @param string $token |
||
| 440 | */ |
||
| 441 | 43 | public function setToken($token) |
|
| 445 | } |
||
| 446 |