Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasss93 committed Jan 6, 2024
1 parent 04428a1 commit 001ff93
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 199 deletions.
255 changes: 78 additions & 177 deletions src/Proxies/UpdateProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
use SergiX44\Nutgram\Telegram\Types\Chat\ChatJoinRequest;
use SergiX44\Nutgram\Telegram\Types\Chat\ChatMemberUpdated;
use SergiX44\Nutgram\Telegram\Types\Common\Update;
use SergiX44\Nutgram\Telegram\Types\Giveaway\Giveaway;
use SergiX44\Nutgram\Telegram\Types\Giveaway\GiveawayCompleted;
use SergiX44\Nutgram\Telegram\Types\Giveaway\GiveawayCreated;
use SergiX44\Nutgram\Telegram\Types\Giveaway\GiveawayWinners;
use SergiX44\Nutgram\Telegram\Types\Inline\CallbackQuery;
use SergiX44\Nutgram\Telegram\Types\Inline\ChosenInlineResult;
use SergiX44\Nutgram\Telegram\Types\Inline\InlineQuery;
Expand All @@ -25,8 +21,6 @@
use SergiX44\Nutgram\Telegram\Types\Poll\PollAnswer;
use SergiX44\Nutgram\Telegram\Types\Reaction\MessageReactionCountUpdated;
use SergiX44\Nutgram\Telegram\Types\Reaction\MessageReactionUpdated;
use SergiX44\Nutgram\Telegram\Types\Shared\ChatShared;
use SergiX44\Nutgram\Telegram\Types\Shared\UsersShared;
use SergiX44\Nutgram\Telegram\Types\User\User;

/**
Expand All @@ -35,265 +29,172 @@
*/
trait UpdateProxy
{
/**
* @return Update|null
*/
public function update(): ?Update
/*
|--------------------------------------------------------------------------
| ID proxies
|--------------------------------------------------------------------------
*/

public function userId(): ?int
{
return $this->update;
return $this->update?->getUser()?->id;
}

/**
* @return int|null
*/
public function chatId(): ?int
{
return $this->update?->getChat()?->id;
}

/**
* @return Chat|null
*/
public function chat(): ?Chat
{
return $this->update?->getChat();
}

/**
* @return int|null
*/
public function userId(): ?int
{
return $this->update?->getUser()?->id;
}

/**
* @return User|null
*/
public function user(): ?User
public function updateId(): ?int
{
return $this->update?->getUser();
return $this->update?->update_id;
}

/**
* @return int|null
*/
public function messageId(): ?int
{
return $this->update?->getMessage()?->message_id;
return $this->message()?->message_id;
}

/**
* @return Message|null
*/
public function message(): ?Message
public function messageThreadId(): ?int
{
return $this->update?->getMessage();
return $this->message()?->message_thread_id;
}

/**
* @return bool
*/
public function isCallbackQuery(): bool
public function inlineMessageId(): ?string
{
return $this->update?->callback_query !== null;
return $this->chosenInlineResult()?->inline_message_id ?? $this->callbackQuery()?->inline_message_id;
}

/**
* @return CallbackQuery|null
*/
public function callbackQuery(): ?CallbackQuery
{
return $this->update?->callback_query;
}
/*
|--------------------------------------------------------------------------
| Special proxies
|--------------------------------------------------------------------------
*/

/**
* @return bool
*/
public function isInlineQuery(): bool
public function update(): ?Update
{
return $this->update?->inline_query !== null;
return $this->update;
}

/**
* @return InlineQuery|null
*/
public function inlineQuery(): ?InlineQuery
public function user(): ?User
{
return $this->update?->inline_query;
return $this->update?->getUser();
}

/**
* @return ChosenInlineResult|null
*/
public function chosenInlineResult(): ?ChosenInlineResult
public function chat(): ?Chat
{
return $this->update?->chosen_inline_result;
return $this->update?->getChat();
}

/**
* @return ShippingQuery|null
*/
public function shippingQuery(): ?ShippingQuery
{
return $this->update?->shipping_query;
}
/*
|--------------------------------------------------------------------------
| Check proxies
|--------------------------------------------------------------------------
*/

/**
* @return bool
*/
public function isPreCheckoutQuery(): bool
public function isCommand(): bool
{
return $this->update?->pre_checkout_query !== null;
/** @var MessageEntity $entity */
$entity = $this->update?->message?->entities[0] ?? null;

return $entity !== null &&
$entity->offset === 0 &&
$entity->type === MessageEntityType::BOT_COMMAND;
}

/**
* @return PreCheckoutQuery|null
*/
public function preCheckoutQuery(): ?PreCheckoutQuery
public function isInlineQuery(): bool
{
return $this->update?->pre_checkout_query;
return $this->update?->inline_query !== null;
}

/**
* @return Poll|null
*/
public function poll(): ?Poll
public function isCallbackQuery(): bool
{
return $this->update?->poll;
return $this->update?->callback_query !== null;
}

/**
* @return PollAnswer|null
*/
public function pollAnswer(): ?PollAnswer
public function isPreCheckoutQuery(): bool
{
return $this->update?->poll_answer;
return $this->update?->pre_checkout_query !== null;
}

/**
* @return bool
*/
public function isMyChatMember(): bool
{
return $this->update?->my_chat_member !== null;
}

/**
* @return ChatMemberUpdated|null
*/
public function chatMember(): ?ChatMemberUpdated
/*
|--------------------------------------------------------------------------
| Update proxies
|--------------------------------------------------------------------------
*/

public function message(): ?Message
{
return $this->update?->chat_member ?? $this->update?->my_chat_member;
return $this->update?->getMessage();
}

/**
* @return ChatJoinRequest|null
*/
public function chatJoinRequest(): ?ChatJoinRequest
public function messageReaction(): ?MessageReactionUpdated
{
return $this->update?->chat_join_request;
return $this->update?->message_reaction;
}

/**
* @return string|null
*/
public function inlineMessageId(): ?string
public function messageReactionCount(): ?MessageReactionCountUpdated
{
return $this->chosenInlineResult()?->inline_message_id ??
$this->callbackQuery()?->inline_message_id;
return $this->update?->message_reaction_count;
}

/**
* @return bool
*/
public function isCommand(): bool
public function inlineQuery(): ?InlineQuery
{
/** @var MessageEntity $entity */
$entity = $this->update?->message?->entities[0] ?? null;

return $entity !== null &&
$entity->offset === 0 &&
$entity->type === MessageEntityType::BOT_COMMAND;
return $this->update?->inline_query;
}

/**
* @return MessageReactionUpdated|null
*/
public function messageReaction(): ?MessageReactionUpdated
public function chosenInlineResult(): ?ChosenInlineResult
{
return $this->update?->message_reaction;
return $this->update?->chosen_inline_result;
}

/**
* @return MessageReactionCountUpdated|null
*/
public function messageReactionCount(): ?MessageReactionCountUpdated
public function callbackQuery(): ?CallbackQuery
{
return $this->update?->message_reaction_count;
return $this->update?->callback_query;
}

/**
* @return ChatBoostUpdated|null
*/
public function chatBoost(): ?ChatBoostUpdated
public function shippingQuery(): ?ShippingQuery
{
return $this->update?->chat_boost;
return $this->update?->shipping_query;
}

/**
* @return ChatBoostRemoved|null
*/
public function removedChatBoost(): ?ChatBoostRemoved
public function preCheckoutQuery(): ?PreCheckoutQuery
{
return $this->update?->removed_chat_boost;
return $this->update?->pre_checkout_query;
}

/**
* @return UsersShared|null
*/
public function usersShared(): ?UsersShared
public function poll(): ?Poll
{
return $this->update?->getMessage()?->users_shared;
return $this->update?->poll;
}

/**
* @return ChatShared|null
*/
public function chatShared(): ?ChatShared
public function pollAnswer(): ?PollAnswer
{
return $this->update?->getMessage()?->chat_shared;
return $this->update?->poll_answer;
}

/**
* @return GiveawayCreated|null
*/
public function giveawayCreated(): ?GiveawayCreated
public function chatMember(): ?ChatMemberUpdated
{
return $this->update?->getMessage()?->giveaway_created;
return $this->update?->chat_member ?? $this->update?->my_chat_member;
}

/**
* @return Giveaway|null
*/
public function giveaway(): ?Giveaway
public function chatJoinRequest(): ?ChatJoinRequest
{
return $this->update?->getMessage()?->giveaway;
return $this->update?->chat_join_request;
}

/**
* @return GiveawayWinners|null
*/
public function giveawayWinners(): ?GiveawayWinners
public function chatBoost(): ?ChatBoostUpdated
{
return $this->update?->getMessage()?->giveaway_winners;
return $this->update?->chat_boost;
}

/**
* @return GiveawayCompleted|null
*/
public function giveawayCompleted(): ?GiveawayCompleted
public function removedChatBoost(): ?ChatBoostRemoved
{
return $this->update?->getMessage()?->giveaway_completed;
return $this->update?->removed_chat_boost;
}
}
Loading

0 comments on commit 001ff93

Please sign in to comment.