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 |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * The base URL for API requests. |
||
| 18 | */ |
||
| 19 | const BASE_URL = 'https://slack.com/api/'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var string The Slack API token string. |
||
| 23 | */ |
||
| 24 | protected $token; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var GuzzleHttp\ClientInterface A Guzzle HTTP client. |
||
| 28 | */ |
||
| 29 | protected $httpClient; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var LoopInterface An event loop instance. |
||
| 33 | */ |
||
| 34 | protected $loop; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Creates a new API client instance. |
||
| 38 | * |
||
| 39 | * @param GuzzleHttp\ClientInterface $httpClient A Guzzle client instance to |
||
| 40 | * send requests with. |
||
| 41 | */ |
||
| 42 | 58 | public function __construct(LoopInterface $loop, GuzzleHttp\ClientInterface $httpClient = null) |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Sets the Slack API token to be used during method calls. |
||
| 50 | * |
||
| 51 | * @param string $token The API token string. |
||
| 52 | */ |
||
| 53 | public function setToken($token) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Gets a message builder for creating a new message object. |
||
| 60 | * |
||
| 61 | * @return \Slack\Message\MessageBuilder |
||
| 62 | */ |
||
| 63 | public function getMessageBuilder() |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Gets the currently authenticated user. |
||
| 70 | * |
||
| 71 | * @return \React\Promise\PromiseInterface A promise for the currently authenticated user. |
||
| 72 | */ |
||
| 73 | public function getAuthedUser() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Gets information about the current Slack team logged in to. |
||
| 82 | * |
||
| 83 | * @return \React\Promise\PromiseInterface A promise for the current Slack team. |
||
| 84 | */ |
||
| 85 | 1 | public function getTeam() |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Gets a channel, group, or DM channel by ID. |
||
| 94 | * |
||
| 95 | * @param string $id The channel ID. |
||
| 96 | * |
||
| 97 | * @return \React\Promise\PromiseInterface A promise for a channel interface. |
||
| 98 | */ |
||
| 99 | public function getChannelGroupOrDMByID($id) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Gets all channels in the team. |
||
| 114 | * |
||
| 115 | * @return \React\Promise\PromiseInterface |
||
| 116 | */ |
||
| 117 | public function getChannels() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Gets a channel by its ID. |
||
| 130 | * |
||
| 131 | * @param string $id A channel ID. |
||
| 132 | * |
||
| 133 | * @return \React\Promise\PromiseInterface A promise for a channel object. |
||
| 134 | */ |
||
| 135 | 2 | public function getChannelById($id) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Gets a channel by its name. |
||
| 146 | * |
||
| 147 | * @param string $name The name of the channel. |
||
| 148 | * |
||
| 149 | * @return \React\Promise\PromiseInterface |
||
| 150 | */ |
||
| 151 | public function getChannelByName($name) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Gets all groups the authenticated user is a member of. |
||
| 166 | * |
||
| 167 | * @return \React\Promise\PromiseInterface |
||
| 168 | */ |
||
| 169 | public function getGroups() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Gets a group by its ID. |
||
| 182 | * |
||
| 183 | * @param string $id A group ID. |
||
| 184 | * |
||
| 185 | * @return \React\Promise\PromiseInterface A promise for a group object. |
||
| 186 | */ |
||
| 187 | public function getGroupById($id) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Gets a group by its name. |
||
| 198 | * |
||
| 199 | * @param string $name The name of the group. |
||
| 200 | * |
||
| 201 | * @return \React\Promise\PromiseInterface |
||
| 202 | */ |
||
| 203 | public function getGroupByName($name) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Gets all DMs the authenticated user has. |
||
| 218 | * |
||
| 219 | * @return \React\Promise\PromiseInterface |
||
| 220 | */ |
||
| 221 | public function getDMs() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Gets a direct message channel by its ID. |
||
| 234 | * |
||
| 235 | * @param string $id A DM channel ID. |
||
| 236 | * |
||
| 237 | * @return \React\Promise\PromiseInterface A promise for a DM object. |
||
| 238 | */ |
||
| 239 | public function getDMById($id) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Gets a direct message channel for a given user. |
||
| 254 | * |
||
| 255 | * @param User $user The user to get a DM for. |
||
| 256 | * |
||
| 257 | * @return \React\Promise\PromiseInterface A promise for a DM object. |
||
| 258 | */ |
||
| 259 | public function getDMByUser(User $user) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Gets a direct message channel by user's ID. |
||
| 266 | * |
||
| 267 | * @param string $id A user ID. |
||
| 268 | * |
||
| 269 | * @return \React\Promise\PromiseInterface A promise for a DM object. |
||
| 270 | */ |
||
| 271 | public function getDMByUserId($id) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Gets all users in the Slack team. |
||
| 282 | * |
||
| 283 | * @return \React\Promise\PromiseInterface A promise for an array of users. |
||
| 284 | */ |
||
| 285 | 2 | public function getUsers() |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Gets a user by its ID. |
||
| 299 | * |
||
| 300 | * @param string $id A user ID. |
||
| 301 | * |
||
| 302 | * @return \React\Promise\PromiseInterface A promise for a user object. |
||
| 303 | */ |
||
| 304 | 4 | public function getUserById($id) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Gets a user by username. |
||
| 315 | * |
||
| 316 | * If the user could not be found, the returned promise is rejected with a |
||
| 317 | * `UserNotFoundException` exception. |
||
| 318 | * |
||
| 319 | * @return \React\Promise\PromiseInterface A promise for a user object. |
||
| 320 | */ |
||
| 321 | 1 | public function getUserByName($username) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Sends a regular text message to a given channel. |
||
| 336 | * |
||
| 337 | * @param string $text The message text. |
||
| 338 | * @param ChannelInterface $channel The channel to send the message to. |
||
| 339 | * @return \React\Promise\PromiseInterface |
||
| 340 | */ |
||
| 341 | public function send($text, ChannelInterface $channel) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Posts a message. |
||
| 353 | * |
||
| 354 | * @param \Slack\Message\Message $message The message to post. |
||
| 355 | * |
||
| 356 | * @return \React\Promise\PromiseInterface |
||
| 357 | */ |
||
| 358 | public function postMessage(Message $message) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Sends an API request. |
||
| 375 | * |
||
| 376 | * @param string $method The API method to call. |
||
| 377 | * @param array $args An associative array of arguments to pass to the |
||
| 378 | * method call. |
||
| 379 | * |
||
| 380 | * @return \React\Promise\PromiseInterface A promise for an API response. |
||
| 381 | */ |
||
| 382 | 11 | public function apiCall($method, array $args = []) |
|
| 421 | } |
||
| 422 |