Complex classes like Slackbot 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 Slackbot, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Slackbot extends AbstractBot |
||
| 12 | { |
||
| 13 | private $commands; |
||
| 14 | private $lastError; |
||
| 15 | private $currentCommand; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Botonomous constructor. |
||
| 19 | * |
||
| 20 | * @param Config|null $config |
||
| 21 | * |
||
| 22 | * @throws \Exception |
||
| 23 | */ |
||
| 24 | 35 | public function __construct(Config $config = null) |
|
| 33 | |||
| 34 | /** |
||
| 35 | * @param null|string $key |
||
| 36 | * |
||
| 37 | * @return mixed |
||
| 38 | */ |
||
| 39 | 8 | public function getRequest($key = null) |
|
| 43 | |||
| 44 | /** |
||
| 45 | * @return string|null |
||
| 46 | */ |
||
| 47 | 7 | private function determineAction() |
|
| 62 | |||
| 63 | /** |
||
| 64 | * @return null|AbstractBaseSlack |
||
| 65 | */ |
||
| 66 | 1 | private function handleMessageActions() |
|
| 81 | |||
| 82 | /** |
||
| 83 | * @throws \Exception |
||
| 84 | */ |
||
| 85 | 4 | private function handleSendResponse() |
|
| 86 | { |
||
| 87 | // 1. Start listening |
||
| 88 | 4 | $this->getListener()->listen(); |
|
| 89 | |||
| 90 | // 2. verify the request |
||
| 91 | try { |
||
| 92 | 4 | $verificationResult = $this->verifyRequest(); |
|
| 93 | |||
| 94 | 4 | if ($verificationResult['success'] !== true) { |
|
| 95 | 4 | throw new \Exception($verificationResult['message']); |
|
| 96 | } |
||
| 97 | 1 | } catch (\Exception $e) { |
|
| 98 | 1 | throw $e; |
|
| 99 | } |
||
| 100 | |||
| 101 | // 3. pre process the request |
||
| 102 | 3 | $this->preProcessRequest(); |
|
| 103 | |||
| 104 | // 4. check access control |
||
| 105 | 3 | if ($this->checkAccessControl() !== true) { |
|
| 106 | 2 | return; |
|
| 107 | } |
||
| 108 | |||
| 109 | // 5. set the current command |
||
| 110 | 1 | $message = $this->getMessage(); |
|
| 111 | 1 | $this->setCurrentCommand($this->getMessageUtility()->extractCommandName($message)); |
|
| 112 | |||
| 113 | // 6. log the message |
||
| 114 | 1 | if (empty($this->getRequest('debug'))) { |
|
| 115 | $this->getLoggerUtility()->logRaw($this->getFormattingUtility()->newLine()); |
||
| 116 | $this->getLoggerUtility()->logChat(__METHOD__, $message); |
||
| 117 | } |
||
| 118 | |||
| 119 | 1 | $this->getLoggerUtility()->logRaw($this->getRequestUtility()->getContent()); |
|
| 120 | |||
| 121 | // 7. send confirmation message if is enabled |
||
| 122 | 1 | $this->getSender()->sendConfirmation(); |
|
| 123 | |||
| 124 | // 8. And send the response to the channel, only if the response is not empty |
||
| 125 | 1 | $response = $this->respond($message); |
|
| 126 | |||
| 127 | 1 | if (!empty($response)) { |
|
| 128 | 1 | $this->getSender()->send($response); |
|
| 129 | } |
||
| 130 | 1 | } |
|
| 131 | |||
| 132 | /** |
||
| 133 | * @return bool |
||
| 134 | */ |
||
| 135 | 3 | private function checkAccessControl() |
|
| 158 | |||
| 159 | /** |
||
| 160 | * @throws \Exception |
||
| 161 | */ |
||
| 162 | 7 | public function run() |
|
| 175 | |||
| 176 | /** |
||
| 177 | * handle OAuth. |
||
| 178 | */ |
||
| 179 | 1 | private function handleOAuth() |
|
| 183 | |||
| 184 | /** |
||
| 185 | * @throws \Exception |
||
| 186 | * |
||
| 187 | * @return mixed |
||
| 188 | */ |
||
| 189 | 1 | private function handleUrlVerification() |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Pre-process the request. |
||
| 202 | */ |
||
| 203 | 3 | private function preProcessRequest() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * @param null $message |
||
| 220 | * |
||
| 221 | * @throws \Exception |
||
| 222 | * |
||
| 223 | * @return mixed |
||
| 224 | */ |
||
| 225 | 4 | public function respond($message = null) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @param null $message |
||
| 258 | * |
||
| 259 | * @throws \Exception |
||
| 260 | * |
||
| 261 | * @return bool|Command |
||
| 262 | */ |
||
| 263 | 7 | public function getCommandByMessage($message = null) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * @throws \Exception |
||
| 318 | * |
||
| 319 | * @return array<string,boolean|string> |
||
| 320 | */ |
||
| 321 | 4 | private function verifyRequest() |
|
| 348 | |||
| 349 | /** |
||
| 350 | * @return array |
||
| 351 | */ |
||
| 352 | 2 | public function getCommands() |
|
| 360 | |||
| 361 | /** |
||
| 362 | * @param array $commands |
||
| 363 | */ |
||
| 364 | 2 | public function setCommands(array $commands) |
|
| 368 | |||
| 369 | /** |
||
| 370 | * @return string |
||
| 371 | */ |
||
| 372 | 4 | public function getLastError() |
|
| 376 | |||
| 377 | /** |
||
| 378 | * @param string $lastError |
||
| 379 | */ |
||
| 380 | 4 | public function setLastError($lastError) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * @return string |
||
| 387 | */ |
||
| 388 | 1 | public function getCurrentCommand() |
|
| 392 | |||
| 393 | /** |
||
| 394 | * @param string $currentCommand |
||
| 395 | */ |
||
| 396 | 2 | public function setCurrentCommand($currentCommand) |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Return message based on the listener |
||
| 403 | * If listener is event and event text is empty, fall back to request text. |
||
| 404 | * |
||
| 405 | * @return mixed|string |
||
| 406 | */ |
||
| 407 | 11 | public function getMessage() |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Determine if bot user id is mentioned in the message. |
||
| 423 | * |
||
| 424 | * @return bool |
||
| 425 | */ |
||
| 426 | 1 | public function youTalkingToMe() |
|
| 442 | } |
||
| 443 |