Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #19

Merged
merged 2 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/Console/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use ReflectionProperty;
use SergiX44\Nutgram\Handlers\Handler;
use SergiX44\Nutgram\Handlers\Type\Command as NutgramCommand;
use SergiX44\Nutgram\Nutgram;
Expand Down Expand Up @@ -39,9 +38,8 @@ public function handle(Nutgram $bot): int

protected function getHandlers(Nutgram $bot): Collection
{
$refHandlers = new ReflectionProperty(Nutgram::class, "handlers");
$refHandlers->setAccessible(true);
return collect(Arr::dot($refHandlers->getValue($bot)));
$handlers = (fn () => $this->handlers)->call($bot);
return collect(Arr::dot($handlers));
}

protected function getHandlerName(string $signature, bool $isCommand = false): string
Expand All @@ -60,6 +58,8 @@ protected function resolveUpdateListener(string $signature): string
'edited_message' => 'onEditedMessage',
'channel_post' => 'onChannelPost',
'edited_channel_post' => 'onEditedChannelPost',
'message_reaction' => 'onMessageReaction',
'message_reaction_count' => 'onMessageReactionCount',
'inline_query' => 'onInlineQuery',
'chosen_inline_result' => 'onChosenInlineResult',
'callback_query' => 'onCallbackQuery',
Expand All @@ -70,8 +70,13 @@ protected function resolveUpdateListener(string $signature): string
'my_chat_member' => 'onMyChatMember',
'chat_member' => 'onChatMember',
'chat_join_request' => 'onChatJoinRequest',
'chat_boost' => 'onChatBoost',
'removed_chat_boost' => 'onRemovedChatBoost',
'api_error' => 'onApiError',
'exception' => 'onException',
'fallback' => 'fallback',
'before_api_request' => 'beforeApiRequest',
'after_api_request' => 'afterApiRequest',
default => 'unknown',
};
}
Expand Down Expand Up @@ -101,6 +106,7 @@ protected function resolveMessageListener(string $signature): string
'poll' => 'onMessagePoll',
'venue' => 'onVenue',
'location' => 'onLocation',
'story' => 'onStory',
'new_chat_members' => 'onNewChatMembers',
'left_chat_member' => 'onLeftChatMember',
'new_chat_title' => 'onNewChatTitle',
Expand All @@ -115,13 +121,19 @@ protected function resolveMessageListener(string $signature): string
'pinned_message' => 'onPinnedMessage',
'invoice' => 'onInvoice',
'successful_payment' => 'onSuccessfulPayment',
'users_shared' => 'onUsersShared',
'chat_shared' => 'onChatShared',
'connected_website' => 'onConnectedWebsite',
'passport_data' => 'onPassportData',
'proximity_alert_triggered' => 'onProximityAlertTriggered',
'forum_topic_created' => 'onForumTopicCreated',
'forum_topic_edited' => 'onForumTopicEdited',
'forum_topic_closed' => 'onForumTopicClosed',
'forum_topic_reopened' => 'onForumTopicReopened',
'giveaway_created' => 'onGiveawayCreated',
'giveaway' => 'onGiveaway',
'giveaway_winners' => 'onGiveawayWinners',
'giveaway_completed' => 'onGiveawayCompleted',
'video_chat_scheduled' => 'onVideoChatScheduled',
'video_chat_started' => 'onVideoChatStarted',
'video_chat_ended' => 'onVideoChatEnded',
Expand All @@ -134,9 +146,7 @@ protected function resolveMessageListener(string $signature): string
protected function getCallableName(Handler $handler): string
{
// get callable value
$refCallable = new ReflectionProperty(Handler::class, "callable");
$refCallable->setAccessible(true);
$callable = $refCallable->getValue($handler);
$callable = (fn () => $this->callable)->call($handler);

// parse callable
if (is_string($callable)) {
Expand Down
5 changes: 3 additions & 2 deletions tests/Commands/HookSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

use Mockery\MockInterface;
use Nutgram\Laravel\Console\HookSetCommand;
use SergiX44\Nutgram\Configuration;
use SergiX44\Nutgram\Nutgram;

test('nutgram:hook:set sets the bot webhook', function () {
$this->mock(Nutgram::class, function (MockInterface $mock) {
$mock->shouldReceive('setWebhook')
->with('https://foo.bar/hook', null, null, 50, null, null, null)
->with('https://foo.bar/hook', null, null, 50, Configuration::DEFAULT_ALLOWED_UPDATES, null, null)
->andReturn(0);
});

Expand All @@ -27,7 +28,7 @@

$this->mock(Nutgram::class, function (MockInterface $mock) use ($hashedAppKey) {
$mock->shouldReceive('setWebhook')
->with('https://foo.bar/hook', null, null, 50, null, null, $hashedAppKey)
->with('https://foo.bar/hook', null, null, 50, Configuration::DEFAULT_ALLOWED_UPDATES, null, $hashedAppKey)
->andReturn(0);
});

Expand Down
Loading