Complex classes like Sender 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 Sender, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Sender |
||
| 14 | { |
||
| 15 | private $slackbot; |
||
| 16 | private $loggerUtility; |
||
| 17 | private $messageUtility; |
||
| 18 | private $config; |
||
| 19 | private $apiClient; |
||
| 20 | private $client; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Sender constructor. |
||
| 24 | * |
||
| 25 | * @param AbstractBot $slackbot |
||
| 26 | */ |
||
| 27 | 9 | public function __construct(AbstractBot $slackbot) |
|
| 31 | |||
| 32 | /** |
||
| 33 | * @return Slackbot |
||
| 34 | */ |
||
| 35 | 7 | public function getSlackbot() |
|
| 39 | |||
| 40 | /** |
||
| 41 | * @param AbstractBot $slackbot |
||
| 42 | */ |
||
| 43 | 9 | public function setSlackbot(AbstractBot $slackbot) |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Final endpoint for the response. |
||
| 50 | * |
||
| 51 | * @param $channel |
||
| 52 | * @param $text |
||
| 53 | * @param $attachments |
||
| 54 | * |
||
| 55 | * @throws \Exception |
||
| 56 | * |
||
| 57 | * @return bool |
||
| 58 | */ |
||
| 59 | 7 | public function send($text, $channel = null, $attachments = null) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * @param $response |
||
| 101 | */ |
||
| 102 | private function respondToSlashCommand($response) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param $data |
||
| 121 | */ |
||
| 122 | 1 | private function respondToSlack($data) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @param $data |
||
| 129 | */ |
||
| 130 | 6 | private function respondAsJSON($data) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * @return LoggerUtility |
||
| 142 | */ |
||
| 143 | 7 | public function getLoggerUtility() |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @param LoggerUtility $loggerUtility |
||
| 154 | */ |
||
| 155 | 7 | public function setLoggerUtility(LoggerUtility $loggerUtility) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @return Config |
||
| 162 | */ |
||
| 163 | 6 | public function getConfig() |
|
| 171 | |||
| 172 | /** |
||
| 173 | * @param Config $config |
||
| 174 | */ |
||
| 175 | 1 | public function setConfig(Config $config) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * @return ApiClient |
||
| 182 | */ |
||
| 183 | 1 | public function getApiClient() |
|
| 191 | |||
| 192 | /** |
||
| 193 | * @param ApiClient $apiClient |
||
| 194 | */ |
||
| 195 | 1 | public function setApiClient(ApiClient $apiClient) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * @return Client |
||
| 202 | */ |
||
| 203 | 1 | public function getClient() |
|
| 211 | |||
| 212 | /** |
||
| 213 | * @param Client $client |
||
| 214 | */ |
||
| 215 | 1 | public function setClient(Client $client) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Specify the response type |
||
| 222 | * If response in config is set to empty, it will be considered based on listener. |
||
| 223 | * |
||
| 224 | * @return mixed|string |
||
| 225 | */ |
||
| 226 | 7 | private function getResponseType() |
|
| 242 | |||
| 243 | /** |
||
| 244 | * @return string|null |
||
| 245 | */ |
||
| 246 | private function getResponseByListenerType() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Send confirmation. |
||
| 259 | */ |
||
| 260 | 1 | public function sendConfirmation() |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @return MessageUtility |
||
| 278 | */ |
||
| 279 | 1 | public function getMessageUtility() |
|
| 287 | |||
| 288 | /** |
||
| 289 | * @param MessageUtility $messageUtility |
||
| 290 | */ |
||
| 291 | 1 | public function setMessageUtility(MessageUtility $messageUtility) |
|
| 295 | } |
||
| 296 |