| Total Complexity | 60 |
| Total Lines | 354 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like SendtochannelCommand 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.
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 SendtochannelCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class SendtochannelCommand extends AdminCommand |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $name = 'sendtochannel'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $description = 'Send message to a channel'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $usage = '/sendtochannel <message to send>'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $version = '0.3.0'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var bool |
||
| 48 | */ |
||
| 49 | protected $need_mysql = true; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Conversation Object |
||
| 53 | * |
||
| 54 | * @var Conversation |
||
| 55 | */ |
||
| 56 | protected $conversation; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Command execute method |
||
| 60 | * |
||
| 61 | * @return ServerResponse|mixed |
||
| 62 | * @throws TelegramException |
||
| 63 | */ |
||
| 64 | public function execute(): ServerResponse |
||
| 65 | { |
||
| 66 | $message = $this->getMessage() ?: $this->getEditedMessage() ?: $this->getChannelPost() ?: $this->getEditedChannelPost(); |
||
| 67 | $chat_id = $message->getChat()->getId(); |
||
| 68 | $user_id = $message->getFrom()->getId(); |
||
| 69 | |||
| 70 | $type = $message->getType(); |
||
| 71 | // 'Cast' the command type to message to protect the machine state |
||
| 72 | // if the command is recalled when the conversation is already started |
||
| 73 | in_array($type, ['command', 'text'], true) && $type = 'message'; |
||
| 74 | |||
| 75 | $text = trim($message->getText(true)); |
||
|
|
|||
| 76 | $text_yes_or_no = ($text === 'Yes' || $text === 'No'); |
||
| 77 | |||
| 78 | $data = [ |
||
| 79 | 'chat_id' => $chat_id, |
||
| 80 | ]; |
||
| 81 | |||
| 82 | // Conversation |
||
| 83 | $this->conversation = new Conversation($user_id, $chat_id, $this->getName()); |
||
| 84 | |||
| 85 | $notes = &$this->conversation->notes; |
||
| 86 | !is_array($notes) && $notes = []; |
||
| 87 | |||
| 88 | $channels = (array) $this->getConfig('your_channel'); |
||
| 89 | if (isset($notes['state'])) { |
||
| 90 | $state = $notes['state']; |
||
| 91 | } else { |
||
| 92 | $state = (count($channels) === 0) ? -1 : 0; |
||
| 93 | $notes['last_message_id'] = $message->getMessageId(); |
||
| 94 | } |
||
| 95 | |||
| 96 | $yes_no_keyboard = new Keyboard( |
||
| 97 | [ |
||
| 98 | 'keyboard' => [['Yes', 'No']], |
||
| 99 | 'resize_keyboard' => true, |
||
| 100 | 'one_time_keyboard' => true, |
||
| 101 | 'selective' => true, |
||
| 102 | ] |
||
| 103 | ); |
||
| 104 | |||
| 105 | switch ($state) { |
||
| 106 | case -1: |
||
| 107 | // getConfig has not been configured asking for channel to administer |
||
| 108 | if ($type !== 'message' || $text === '') { |
||
| 109 | $notes['state'] = -1; |
||
| 110 | $this->conversation->update(); |
||
| 111 | |||
| 112 | $result = $this->replyToChat( |
||
| 113 | 'Insert the channel name or ID (_@yourchannel_ or _-12345_)', |
||
| 114 | [ |
||
| 115 | 'parse_mode' => 'markdown', |
||
| 116 | 'reply_markup' => Keyboard::remove(['selective' => true]), |
||
| 117 | ] |
||
| 118 | ); |
||
| 119 | |||
| 120 | break; |
||
| 121 | } |
||
| 122 | $notes['channel'] = $text; |
||
| 123 | $notes['last_message_id'] = $message->getMessageId(); |
||
| 124 | // Jump to state 1 |
||
| 125 | goto insert; |
||
| 126 | |||
| 127 | // no break |
||
| 128 | default: |
||
| 129 | case 0: |
||
| 130 | // getConfig has been configured choose channel |
||
| 131 | if ($type !== 'message' || $text === '') { |
||
| 132 | $notes['state'] = 0; |
||
| 133 | $this->conversation->update(); |
||
| 134 | |||
| 135 | $keyboard = array_map(function ($channel) { |
||
| 136 | return [$channel]; |
||
| 137 | }, $channels); |
||
| 138 | |||
| 139 | $result = $this->replyToChat( |
||
| 140 | 'Choose a channel from the keyboard' . PHP_EOL . |
||
| 141 | '_or_ insert the channel name or ID (_@yourchannel_ or _-12345_)', |
||
| 142 | [ |
||
| 143 | 'parse_mode' => 'markdown', |
||
| 144 | 'reply_markup' => new Keyboard( |
||
| 145 | [ |
||
| 146 | 'keyboard' => $keyboard, |
||
| 147 | 'resize_keyboard' => true, |
||
| 148 | 'one_time_keyboard' => true, |
||
| 149 | 'selective' => true, |
||
| 150 | ] |
||
| 151 | ), |
||
| 152 | ] |
||
| 153 | ); |
||
| 154 | break; |
||
| 155 | } |
||
| 156 | $notes['channel'] = $text; |
||
| 157 | $notes['last_message_id'] = $message->getMessageId(); |
||
| 158 | |||
| 159 | // no break |
||
| 160 | case 1: |
||
| 161 | insert: |
||
| 162 | if (($type === 'message' && $text === '') || $notes['last_message_id'] === $message->getMessageId()) { |
||
| 163 | $notes['state'] = 1; |
||
| 164 | $this->conversation->update(); |
||
| 165 | |||
| 166 | $result = $this->replyToChat( |
||
| 167 | 'Insert the content you want to share: text, photo, audio...', |
||
| 168 | ['reply_markup' => Keyboard::remove(['selective' => true])] |
||
| 169 | ); |
||
| 170 | break; |
||
| 171 | } |
||
| 172 | $notes['last_message_id'] = $message->getMessageId(); |
||
| 173 | $notes['message'] = $message->getRawData(); |
||
| 174 | $notes['message_type'] = $type; |
||
| 175 | // no break |
||
| 176 | case 2: |
||
| 177 | if (!$text_yes_or_no || $notes['last_message_id'] === $message->getMessageId()) { |
||
| 178 | $notes['state'] = 2; |
||
| 179 | $this->conversation->update(); |
||
| 180 | |||
| 181 | // Grab any existing caption. |
||
| 182 | if ($caption = $message->getCaption()) { |
||
| 183 | $notes['caption'] = $caption; |
||
| 184 | $text = 'No'; |
||
| 185 | } elseif (in_array($notes['message_type'], ['video', 'photo'], true)) { |
||
| 186 | $text = 'Would you like to insert a caption?'; |
||
| 187 | if (!$text_yes_or_no && $notes['last_message_id'] !== $message->getMessageId()) { |
||
| 188 | $text .= PHP_EOL . 'Type Yes or No'; |
||
| 189 | } |
||
| 190 | |||
| 191 | $result = $this->replyToChat( |
||
| 192 | $text, |
||
| 193 | ['reply_markup' => $yes_no_keyboard] |
||
| 194 | ); |
||
| 195 | break; |
||
| 196 | } |
||
| 197 | } |
||
| 198 | $notes['set_caption'] = ($text === 'Yes'); |
||
| 199 | $notes['last_message_id'] = $message->getMessageId(); |
||
| 200 | // no break |
||
| 201 | case 3: |
||
| 202 | if ($notes['set_caption'] && ($notes['last_message_id'] === $message->getMessageId() || $type !== 'message')) { |
||
| 203 | $notes['state'] = 3; |
||
| 204 | $this->conversation->update(); |
||
| 205 | |||
| 206 | $result = $this->replyToChat( |
||
| 207 | 'Insert caption:', |
||
| 208 | ['reply_markup' => Keyboard::remove(['selective' => true])] |
||
| 209 | ); |
||
| 210 | break; |
||
| 211 | } |
||
| 212 | $notes['last_message_id'] = $message->getMessageId(); |
||
| 213 | if (isset($notes['caption'])) { |
||
| 214 | // If caption has already been send with the file, no need to ask for it. |
||
| 215 | $notes['set_caption'] = true; |
||
| 216 | } else { |
||
| 217 | $notes['caption'] = $text; |
||
| 218 | } |
||
| 219 | // no break |
||
| 220 | case 4: |
||
| 221 | if (!$text_yes_or_no || $notes['last_message_id'] === $message->getMessageId()) { |
||
| 222 | $notes['state'] = 4; |
||
| 223 | $this->conversation->update(); |
||
| 224 | |||
| 225 | $result = $this->replyToChat('Message will look like this:'); |
||
| 226 | |||
| 227 | if ($notes['message_type'] !== 'command') { |
||
| 228 | if ($notes['set_caption']) { |
||
| 229 | $data['caption'] = $notes['caption']; |
||
| 230 | } |
||
| 231 | $this->sendBack(new Message($notes['message'], $this->telegram->getBotUsername()), $data); |
||
| 232 | |||
| 233 | $data['reply_markup'] = $yes_no_keyboard; |
||
| 234 | |||
| 235 | $data['text'] = 'Would you like to post it?'; |
||
| 236 | if (!$text_yes_or_no && $notes['last_message_id'] !== $message->getMessageId()) { |
||
| 237 | $data['text'] .= PHP_EOL . 'Type Yes or No'; |
||
| 238 | } |
||
| 239 | $result = Request::sendMessage($data); |
||
| 240 | } |
||
| 241 | break; |
||
| 242 | } |
||
| 243 | |||
| 244 | $notes['post_message'] = ($text === 'Yes'); |
||
| 245 | $notes['last_message_id'] = $message->getMessageId(); |
||
| 246 | // no break |
||
| 247 | case 5: |
||
| 248 | $data['reply_markup'] = Keyboard::remove(['selective' => true]); |
||
| 249 | |||
| 250 | if ($notes['post_message']) { |
||
| 251 | $data['parse_mode'] = 'markdown'; |
||
| 252 | $data['text'] = $this->publish( |
||
| 253 | new Message($notes['message'], $this->telegram->getBotUsername()), |
||
| 254 | $notes['channel'], |
||
| 255 | $notes['caption'] |
||
| 256 | ); |
||
| 257 | } else { |
||
| 258 | $data['text'] = 'Aborted by user, message not sent..'; |
||
| 259 | } |
||
| 260 | |||
| 261 | $this->conversation->stop(); |
||
| 262 | $result = Request::sendMessage($data); |
||
| 263 | } |
||
| 264 | |||
| 265 | return $result; |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * SendBack |
||
| 270 | * |
||
| 271 | * Received a message, the bot can send a copy of it to another chat/channel. |
||
| 272 | * You don't have to care about the type of the message, the function detect it and use the proper |
||
| 273 | * REQUEST:: function to send it. |
||
| 274 | * $data include all the var that you need to send the message to the proper chat |
||
| 275 | * |
||
| 276 | * @todo This method will be moved to a higher level maybe in AdminCommand or Command |
||
| 277 | * @todo Looking for a more significant name |
||
| 278 | * |
||
| 279 | * @param Message $message |
||
| 280 | * @param array $data |
||
| 281 | * |
||
| 282 | * @return ServerResponse |
||
| 283 | * @throws TelegramException |
||
| 284 | */ |
||
| 285 | protected function sendBack(Message $message, array $data): ServerResponse |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Publish a message to a channel and return success or failure message in markdown format |
||
| 317 | * |
||
| 318 | * @param Message $message |
||
| 319 | * @param string|int $channel_id |
||
| 320 | * @param string|null $caption |
||
| 321 | * |
||
| 322 | * @return string |
||
| 323 | * @throws TelegramException |
||
| 324 | */ |
||
| 325 | protected function publish(Message $message, $channel_id, $caption = null): string |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Execute without db |
||
| 354 | * |
||
| 355 | * @todo Why send just to the first found channel? |
||
| 356 | * |
||
| 357 | * @return mixed |
||
| 358 | * @throws TelegramException |
||
| 359 | */ |
||
| 360 | public function executeNoDb(): ServerResponse |
||
| 380 |