Refactor event type determination logic
This commit is contained in:
parent
4829853d0e
commit
56c7fb04d2
4 changed files with 159 additions and 123 deletions
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
28
src/TgBotLib/Events/MessageEvent.php
Normal file
28
src/TgBotLib/Events/MessageEvent.php
Normal file
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
namespace TgBotLib\Events;
|
||||
|
||||
use TgBotLib\Abstracts\UpdateEvent;
|
||||
use TgBotLib\Enums\UpdateEventType;
|
||||
use TgBotLib\Objects\Message;
|
||||
|
||||
abstract class MessageEvent extends UpdateEvent
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function getEventType(): UpdateEventType
|
||||
{
|
||||
return UpdateEventType::MESSAGE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the current message.
|
||||
*
|
||||
* @return Message The message.
|
||||
*/
|
||||
protected function getMessage(): Message
|
||||
{
|
||||
return $this->update->getMessage();
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue