| Conditions | 40 |
| Paths | 14616 |
| Total Lines | 202 |
| Code Lines | 128 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | } |
||
| 380 |