| Conditions | 12 |
| Paths | 6 |
| Total Lines | 68 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 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 |
||
| 29 | public function resolveListeners(Update $update): PromiseInterface |
||
| 30 | { |
||
| 31 | $deferred = new Deferred(); |
||
| 32 | $listeners = []; |
||
| 33 | $updateType = $update->getUpdateType(); |
||
| 34 | $this->mergeListenersByType($listeners, $updateType); |
||
| 35 | $this->mergeListenersByType($listeners, Update::class); |
||
| 36 | |||
| 37 | switch ($updateType) { |
||
| 38 | |||
| 39 | case CallbackQuery::class: |
||
| 40 | $chatId = $update->getEffectiveChat() ? $update->getEffectiveChat()->getId() : null; |
||
| 41 | $callbackQuery = $update->getCallbackQuery(); |
||
| 42 | $text = $callbackQuery->getMessage() ? $callbackQuery->getMessage()->getText() : null; |
||
| 43 | $this->conversationManager->getConversationHandler($chatId) |
||
| 44 | ->then(function ($handlerInfo) use ($deferred, $callbackQuery, $text, &$listeners) { |
||
| 45 | if (!$handlerInfo) { // if we are not in a conversation, call the listeners as usual |
||
| 46 | if ($text) { |
||
| 47 | $this->findListenerAndPush($listeners, 'cb_query_texts', $text); |
||
| 48 | } |
||
| 49 | if ($callbackQuery->getData()) { |
||
| 50 | $this->findListenerAndPush($listeners, 'cb_query_data', $callbackQuery->getData()); |
||
| 51 | } |
||
| 52 | } else { // if we are in a conversation, redirect it only to the conversation step |
||
| 53 | $listeners[] = new Listener($handlerInfo[0], $this->container); |
||
| 54 | } |
||
| 55 | $deferred->resolve($listeners); |
||
| 56 | })->otherwise(function ($e) use ($deferred) { |
||
|
|
|||
| 57 | // if something goes wrong, reject the promise |
||
| 58 | $deferred->reject($e); |
||
| 59 | }); |
||
| 60 | break; |
||
| 61 | |||
| 62 | case Message::class: |
||
| 63 | $text = $update->getMessage()->getText(); |
||
| 64 | $chatId = $update->getEffectiveChat()->getId(); |
||
| 65 | $this->conversationManager->getConversationHandler($chatId) |
||
| 66 | ->then(function ($handlerInfo) use ($chatId, $text, $deferred, &$listeners) { |
||
| 67 | if (!$handlerInfo) { // if we are not in a conversation, call the listeners as usual |
||
| 68 | $this->findListenerAndPush($listeners, 'messages', $text); |
||
| 69 | } elseif (!$handlerInfo[1] && $text) { |
||
| 70 | // if we are in a conversation, and listeners are not skipped by the step call, |
||
| 71 | // try to call the matching listeners |
||
| 72 | $listener = $this->findListenerAndPush($listeners, 'messages', $text, true); |
||
| 73 | if (!$listener) { |
||
| 74 | // if no listeners are found, call the next conversation step |
||
| 75 | $listeners[] = new Listener($handlerInfo[0], $this->container); |
||
| 76 | } else { |
||
| 77 | // if listener is found, escape the conversation |
||
| 78 | $this->conversationManager->deleteConversationCache($chatId); |
||
| 79 | } |
||
| 80 | } else { |
||
| 81 | // if the coversation is forcing only the conversation handlers, |
||
| 82 | // skip the other listeners |
||
| 83 | $listeners[] = new Listener($handlerInfo[0], $this->container); |
||
| 84 | } |
||
| 85 | $deferred->resolve($listeners); |
||
| 86 | })->otherwise(function ($e) use ($deferred) { |
||
| 87 | // if something goes wrong, reject the promise |
||
| 88 | $deferred->reject($e); |
||
| 89 | }); |
||
| 90 | break; |
||
| 91 | |||
| 92 | default: |
||
| 93 | $deferred->resolve($listeners); |
||
| 94 | } |
||
| 95 | |||
| 96 | return $deferred->promise(); |
||
| 97 | } |
||
| 141 |