From 56c7fb04d2da2054844ff5ab1e89db51031b6729 Mon Sep 17 00:00:00 2001 From: netkas Date: Sun, 3 Nov 2024 17:22:45 -0500 Subject: [PATCH] Refactor event type determination logic --- src/TgBotLib/Classes/Utilities.php | 121 ----------------------- src/TgBotLib/Enums/UpdateEventType.php | 129 +++++++++++++++++++++++++ src/TgBotLib/Events/MessageEvent.php | 28 ++++++ src/TgBotLib/PollingBot.php | 4 +- 4 files changed, 159 insertions(+), 123 deletions(-) create mode 100644 src/TgBotLib/Events/MessageEvent.php diff --git a/src/TgBotLib/Classes/Utilities.php b/src/TgBotLib/Classes/Utilities.php index b16cc27..fb89b00 100644 --- a/src/TgBotLib/Classes/Utilities.php +++ b/src/TgBotLib/Classes/Utilities.php @@ -52,125 +52,4 @@ return self::$currencies_cache[strtolower($code)]; } - - /** - * Determines the type of event based on the provided update object. - * - * @param Update $update The update object containing event information. - * @return UpdateEventType The determined type of the event. - */ - public static function determineEventType(Update $update): UpdateEventType - { - if($update->getRemovedChatBoost() !== null) - { - return UpdateEventType::REMOVED_CHAT_BOOST_EVENT; - } - - if($update->getChatBoost() !== null) - { - return UpdateEventType::CHAT_BOOST_EVENT; - } - - if($update->getChatJoinRequest() !== null) - { - return UpdateEventType::CHAT_JOIN_REQUEST_EVENT; - } - - if($update->getChatMember() !== null) - { - return UpdateEventType::CHAT_MEMBER_UPDATED; - } - - if($update->getMyChatMember() !== null) - { - return UpdateEventType::MY_CHAT_MEMBER_UPDATED; - } - - if($update->getPollAnswer() !== null) - { - return UpdateEventType::POLL_ANSWER; - } - - if($update->getPoll() !== null) - { - return UpdateEventType::POLL; - } - - if($update->getPurchasedPaidMedia() !== null) - { - return UpdateEventType::PAID_MEDIA_PURCHASED; - } - - if($update->getPreCheckoutQuery() !== null) - { - return UpdateEventType::PRE_CHECKOUT_QUERY; - } - - if($update->getPreCheckoutQuery() !== null) - { - return UpdateEventType::PRE_CHECKOUT_QUERY; - } - - if($update->getCallbackQuery() !== null) - { - return UpdateEventType::CALLBACK_QUERY; - } - - if($update->getChosenInlineResult() !== null) - { - return UpdateEventType::CHOSEN_INLINE_RESULT; - } - - if($update->getInlineQuery() !== null) - { - return UpdateEventType::INLINE_QUERY; - } - - if($update->getMessageReactionCount() !== null) - { - return UpdateEventType::MESSAGE_REACTION_COUNT; - } - - if($update->getMessageReaction() !== null) - { - return UpdateEventType::MESSAGE_REACTION; - } - - if($update->getDeletedBusinessMessages() !== null) - { - return UpdateEventType::DELETED_BUSINESS_MESSAGES; - } - - if($update->getEditedBusinessMessage() !== null) - { - return UpdateEventType::EDITED_BUSINESS_MESSAGE; - } - - if($update->getBusinessMessage() !== null) - { - return UpdateEventType::BUSINESS_MESSAGE; - } - - if($update->getBusinessConnection() !== null) - { - return UpdateEventType::BUSINESS_CONNECTION; - } - - if($update->getEditedChannelPost() !== null) - { - return UpdateEventType::EDITED_CHANNEL_POST; - } - - if($update->getChannelPost() !== null) - { - return UpdateEventType::CHANNEL_POST; - } - - if($update->getEditedMessage() !== null) - { - return UpdateEventType::EDITED_MESSAGE; - } - - return UpdateEventType::UPDATE_EVENT; - } } \ No newline at end of file diff --git a/src/TgBotLib/Enums/UpdateEventType.php b/src/TgBotLib/Enums/UpdateEventType.php index 1e93dea..f9aa8a5 100644 --- a/src/TgBotLib/Enums/UpdateEventType.php +++ b/src/TgBotLib/Enums/UpdateEventType.php @@ -16,6 +16,7 @@ use TgBotLib\Events\EditedChannelPostEvent; use TgBotLib\Events\EditedMessageEvent; use TgBotLib\Events\InlineQueryEvent; + use TgBotLib\Events\MessageEvent; use TgBotLib\Events\MessageReactionCountEvent; use TgBotLib\Events\MessageReactionEvent; use TgBotLib\Events\MyChatMemberUpdatedEvent; @@ -25,6 +26,7 @@ use TgBotLib\Events\PreCheckoutQueryEvent; use TgBotLib\Events\RemovedChatBoostEvent; use TgBotLib\Events\ShippingQueryEvent; + use TgBotLib\Objects\Update; enum UpdateEventType : string { @@ -51,4 +53,131 @@ case EDITED_CHANNEL_POST = EditedChannelPostEvent::class; case CHANNEL_POST = ChannelPostEvent::class; case EDITED_MESSAGE = EditedMessageEvent::class; + case MESSAGE = MessageEvent::class; + + /** + * Determines the type of event based on the provided Update object. + * + * @param Update $update The update object containing event details. + * @return UpdateEventType The type of event detected from the update. + */ + public static function determineEventType(Update $update): UpdateEventType + { + if($update->getRemovedChatBoost() !== null) + { + return UpdateEventType::REMOVED_CHAT_BOOST_EVENT; + } + + if($update->getChatBoost() !== null) + { + return UpdateEventType::CHAT_BOOST_EVENT; + } + + if($update->getChatJoinRequest() !== null) + { + return UpdateEventType::CHAT_JOIN_REQUEST_EVENT; + } + + if($update->getChatMember() !== null) + { + return UpdateEventType::CHAT_MEMBER_UPDATED; + } + + if($update->getMyChatMember() !== null) + { + return UpdateEventType::MY_CHAT_MEMBER_UPDATED; + } + + if($update->getPollAnswer() !== null) + { + return UpdateEventType::POLL_ANSWER; + } + + if($update->getPoll() !== null) + { + return UpdateEventType::POLL; + } + + if($update->getPurchasedPaidMedia() !== null) + { + return UpdateEventType::PAID_MEDIA_PURCHASED; + } + + if($update->getPreCheckoutQuery() !== null) + { + return UpdateEventType::PRE_CHECKOUT_QUERY; + } + + if($update->getPreCheckoutQuery() !== null) + { + return UpdateEventType::PRE_CHECKOUT_QUERY; + } + + if($update->getCallbackQuery() !== null) + { + return UpdateEventType::CALLBACK_QUERY; + } + + if($update->getChosenInlineResult() !== null) + { + return UpdateEventType::CHOSEN_INLINE_RESULT; + } + + if($update->getInlineQuery() !== null) + { + return UpdateEventType::INLINE_QUERY; + } + + if($update->getMessageReactionCount() !== null) + { + return UpdateEventType::MESSAGE_REACTION_COUNT; + } + + if($update->getMessageReaction() !== null) + { + return UpdateEventType::MESSAGE_REACTION; + } + + if($update->getDeletedBusinessMessages() !== null) + { + return UpdateEventType::DELETED_BUSINESS_MESSAGES; + } + + if($update->getEditedBusinessMessage() !== null) + { + return UpdateEventType::EDITED_BUSINESS_MESSAGE; + } + + if($update->getBusinessMessage() !== null) + { + return UpdateEventType::BUSINESS_MESSAGE; + } + + if($update->getBusinessConnection() !== null) + { + return UpdateEventType::BUSINESS_CONNECTION; + } + + if($update->getEditedChannelPost() !== null) + { + return UpdateEventType::EDITED_CHANNEL_POST; + } + + if($update->getChannelPost() !== null) + { + return UpdateEventType::CHANNEL_POST; + } + + if($update->getEditedMessage() !== null) + { + return UpdateEventType::EDITED_MESSAGE; + } + + if($update->getMessage() !== null) + { + return UpdateEventType::MESSAGE; + } + + return UpdateEventType::UPDATE_EVENT; + } } diff --git a/src/TgBotLib/Events/MessageEvent.php b/src/TgBotLib/Events/MessageEvent.php new file mode 100644 index 0000000..e2be5d1 --- /dev/null +++ b/src/TgBotLib/Events/MessageEvent.php @@ -0,0 +1,28 @@ +update->getMessage(); + } + } \ No newline at end of file diff --git a/src/TgBotLib/PollingBot.php b/src/TgBotLib/PollingBot.php index cadfa9d..3d2f2ff 100644 --- a/src/TgBotLib/PollingBot.php +++ b/src/TgBotLib/PollingBot.php @@ -136,7 +136,7 @@ $this->offset = $update->getUpdateId() + 1; } - $updateByType = $this->getEventHandlersByType(Utilities::determineEventType($update)); + $updateByType = $this->getEventHandlersByType(UpdateEventType::determineEventType($update)); if(count($updateByType) === 0) { @@ -152,7 +152,7 @@ { // Otherwise, use the appropriate event handler for the update type /** @var UpdateEvent $eventHandler */ - foreach($this->getEventHandlersByType(Utilities::determineEventType($update)) as $eventHandler) + foreach($this->getEventHandlersByType(UpdateEventType::determineEventType($update)) as $eventHandler) { (new $eventHandler($update))->handle($this); }