| Conditions | 4 |
| Paths | 4 |
| Total Lines | 74 |
| Code Lines | 42 |
| 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 |
||
| 52 | public function execute() |
||
| 53 | { |
||
| 54 | $message = $this->getMessage(); |
||
| 55 | |||
| 56 | $from = $message->getFrom(); |
||
| 57 | $user_id = $from->getId(); |
||
| 58 | $chat_id = $message->getChat()->getId(); |
||
| 59 | $message_id = $message->getMessageId(); |
||
| 60 | |||
| 61 | $data = [ |
||
| 62 | 'chat_id' => $chat_id, |
||
| 63 | 'reply_to_message_id' => $message_id, |
||
| 64 | ]; |
||
| 65 | |||
| 66 | //Send chat action |
||
| 67 | Request::sendChatAction([ |
||
| 68 | 'chat_id' => $chat_id, |
||
| 69 | 'action' => 'typing', |
||
| 70 | ]); |
||
| 71 | |||
| 72 | $caption = sprintf( |
||
| 73 | 'Your Id: %d' . PHP_EOL . |
||
| 74 | 'Name: %s %s' . PHP_EOL . |
||
| 75 | 'Username: %s', |
||
| 76 | $user_id, |
||
| 77 | $from->getFirstName(), |
||
| 78 | $from->getLastName(), |
||
| 79 | $from->getUsername() |
||
| 80 | ); |
||
| 81 | |||
| 82 | //Fetch user profile photo |
||
| 83 | $limit = 10; |
||
| 84 | $offset = null; |
||
| 85 | $response = Request::getUserProfilePhotos( |
||
| 86 | [ |
||
| 87 | 'user_id' => $user_id, |
||
| 88 | 'limit' => $limit, |
||
| 89 | 'offset' => $offset, |
||
| 90 | ] |
||
| 91 | ); |
||
| 92 | |||
| 93 | if ($response->isOk()) { |
||
| 94 | /** @var UserProfilePhotos $user_profile_photos */ |
||
| 95 | $user_profile_photos = $response->getResult(); |
||
| 96 | |||
| 97 | if ($user_profile_photos->getTotalCount() > 0) { |
||
| 98 | $photos = $user_profile_photos->getPhotos(); |
||
| 99 | |||
| 100 | /** @var PhotoSize $photo */ |
||
| 101 | $photo = $photos[0][2]; |
||
| 102 | $file_id = $photo->getFileId(); |
||
| 103 | |||
| 104 | $data['photo'] = $file_id; |
||
| 105 | $data['caption'] = $caption; |
||
| 106 | |||
| 107 | $result = Request::sendPhoto($data); |
||
| 108 | |||
| 109 | //Download the photo after send message response to speedup response |
||
| 110 | $response2 = Request::getFile(['file_id' => $file_id]); |
||
| 111 | if ($response2->isOk()) { |
||
| 112 | /** @var File $photo_file */ |
||
| 113 | $photo_file = $response2->getResult(); |
||
| 114 | Request::downloadFile($photo_file); |
||
| 115 | } |
||
| 116 | |||
| 117 | return $result; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | //No Photo just send text |
||
| 122 | $data['text'] = $caption; |
||
| 123 | |||
| 124 | return Request::sendMessage($data); |
||
| 125 | } |
||
| 126 | } |
||
| 127 |