From 19c4216362c04895b3f31276cf84d2c81ef71429 Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Fri, 3 Nov 2023 21:35:59 +0700 Subject: [PATCH 1/2] refactor: move methods to service and remove redundant actions --- composer.json | 2 +- src/Http/Actions/CallbackAction.php | 55 ---------------- src/Http/Actions/CommandAction.php | 63 ------------------- src/Http/Actions/IndexAction.php | 15 +++-- src/Services/CallbackService.php | 33 ++++++++++ src/Services/CommandService.php | 49 +++++++++++++++ .../NotificationService.php} | 11 ++-- 7 files changed, 99 insertions(+), 129 deletions(-) delete mode 100644 src/Http/Actions/CallbackAction.php delete mode 100644 src/Http/Actions/CommandAction.php rename src/{Http/Actions/SendNotificationAction.php => Services/NotificationService.php} (88%) diff --git a/composer.json b/composer.json index bbdd433..4d9b4b6 100755 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "homepage": "https://github.com/cslant/telegram-git-notifier-app", "license": "MIT", "require": { - "php": "^8.0", + "php": "^8.1", "ext-json": "*", "cslant/telegram-git-notifier": "^1.3.1" }, diff --git a/src/Http/Actions/CallbackAction.php b/src/Http/Actions/CallbackAction.php deleted file mode 100644 index f803531..0000000 --- a/src/Http/Actions/CallbackAction.php +++ /dev/null @@ -1,55 +0,0 @@ -bot = $bot; - $this->callbackService = new CallbackService($bot); - } - - /** - * @return void - * @throws MessageIsEmptyException - * @throws InvalidViewTemplateException - */ - public function __invoke(): void - { - $callback = $this->bot->telegram->Callback_Data(); - - if (str_contains($callback, SettingConstant::SETTING_CUSTOM_EVENTS)) { - $this->bot->eventHandle($callback); - - return; - } - - if (str_contains($callback, SettingConstant::SETTING_BACK)) { - $this->callbackService->answerBackButton($callback); - - return; - } - - $callback = str_replace(SettingConstant::SETTING_PREFIX, '', $callback); - - if ($this->bot->setting->updateSetting($callback, !$this->bot->setting->getSettings()[$callback])) { - $this->bot->editMessageReplyMarkup([ - 'reply_markup' => $this->bot->settingMarkup(), - ]); - } else { - $this->bot->answerCallbackQuery('Something went wrong!'); - } - } -} diff --git a/src/Http/Actions/CommandAction.php b/src/Http/Actions/CommandAction.php deleted file mode 100644 index a6381fa..0000000 --- a/src/Http/Actions/CommandAction.php +++ /dev/null @@ -1,63 +0,0 @@ -bot = $bot; - $this->commandService = new CommandService(); - } - - /** - * @return void - * @throws EntryNotFoundException - * @throws MessageIsEmptyException - */ - public function __invoke(): void - { - $text = $this->bot->telegram->Text(); - - switch ($text) { - case '/start': - $this->commandService->sendStartMessage($this->bot); - - break; - case '/menu': - $this->bot->sendMessage( - view('tools.menu'), - ['reply_markup' => $this->commandService->menuMarkup($this->bot->telegram)] - ); - - break; - case '/token': - case '/id': - case '/usage': - case '/server': - $this->bot->sendMessage(view('tools.' . trim($text, '/'))); - - break; - case '/settings': - $this->bot->settingHandle(); - - break; - case '/set_menu': - $this->bot->setMyCommands(CommandService::MENU_COMMANDS); - - break; - default: - $this->bot->sendMessage('🤨 Invalid Request!'); - } - } -} diff --git a/src/Http/Actions/IndexAction.php b/src/Http/Actions/IndexAction.php index 19768bd..1c51bb8 100644 --- a/src/Http/Actions/IndexAction.php +++ b/src/Http/Actions/IndexAction.php @@ -8,6 +8,9 @@ use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException; use CSlant\TelegramGitNotifier\Exceptions\SendNotificationException; use CSlant\TelegramGitNotifier\Notifier; +use CSlant\TelegramGitNotifierApp\Services\CallbackService; +use CSlant\TelegramGitNotifierApp\Services\CommandService; +use CSlant\TelegramGitNotifierApp\Services\NotificationService; use GuzzleHttp\Client; use Symfony\Component\HttpFoundation\Request; use Telegram; @@ -43,20 +46,20 @@ public function __construct() public function __invoke(): void { if ($this->bot->isCallback()) { - $callbackAction = new CallbackAction($this->bot); - $callbackAction(); + $callbackAction = new CallbackService($this->bot); + $callbackAction->handle(); return; } if ($this->bot->isMessage() && $this->bot->isOwner()) { - $commandAction = new CommandAction($this->bot); - $commandAction(); + $commandAction = new CommandService($this->bot); + $commandAction->handle(); return; } - $sendNotificationAction = new SendNotificationAction($this->notifier, $this->bot->setting); - $sendNotificationAction(); + $sendNotification = new NotificationService($this->notifier, $this->bot->setting); + $sendNotification->handle(); } } diff --git a/src/Services/CallbackService.php b/src/Services/CallbackService.php index 21369be..5182734 100644 --- a/src/Services/CallbackService.php +++ b/src/Services/CallbackService.php @@ -4,6 +4,7 @@ use CSlant\TelegramGitNotifier\Bot; use CSlant\TelegramGitNotifier\Constants\SettingConstant; +use CSlant\TelegramGitNotifier\Exceptions\InvalidViewTemplateException; use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException; use CSlant\TelegramGitNotifierApp\Traits\Markup; @@ -61,4 +62,36 @@ public function answerBackButton(string $callback): void 'reply_markup' => $markup, ]); } + + /** + * @return void + * @throws MessageIsEmptyException + * @throws InvalidViewTemplateException + */ + public function handle(): void + { + $callback = $this->bot->telegram->Callback_Data(); + + if (str_contains($callback, SettingConstant::SETTING_CUSTOM_EVENTS)) { + $this->bot->eventHandle($callback); + + return; + } + + if (str_contains($callback, SettingConstant::SETTING_BACK)) { + $this->answerBackButton($callback); + + return; + } + + $callback = str_replace(SettingConstant::SETTING_PREFIX, '', $callback); + + if ($this->bot->setting->updateSetting($callback, !$this->bot->setting->getSettings()[$callback])) { + $this->bot->editMessageReplyMarkup([ + 'reply_markup' => $this->bot->settingMarkup(), + ]); + } else { + $this->bot->answerCallbackQuery('Something went wrong!'); + } + } } diff --git a/src/Services/CommandService.php b/src/Services/CommandService.php index 0c97e6f..6972ddb 100644 --- a/src/Services/CommandService.php +++ b/src/Services/CommandService.php @@ -4,6 +4,7 @@ use CSlant\TelegramGitNotifier\Bot; use CSlant\TelegramGitNotifier\Exceptions\EntryNotFoundException; +use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException; use CSlant\TelegramGitNotifierApp\Traits\Markup; class CommandService @@ -36,6 +37,13 @@ class CommandService ], ]; + private Bot $bot; + + public function __construct(Bot $bot) + { + $this->bot = $bot; + } + /** * @param Bot $bot * @@ -53,4 +61,45 @@ public function sendStartMessage(Bot $bot): void ['caption' => $reply] ); } + + /** + * @return void + * @throws EntryNotFoundException + * @throws MessageIsEmptyException + */ + public function handle(): void + { + $text = $this->bot->telegram->Text(); + + switch ($text) { + case '/start': + $this->sendStartMessage($this->bot); + + break; + case '/menu': + $this->bot->sendMessage( + view('tools.menu'), + ['reply_markup' => $this->menuMarkup($this->bot->telegram)] + ); + + break; + case '/token': + case '/id': + case '/usage': + case '/server': + $this->bot->sendMessage(view('tools.' . trim($text, '/'))); + + break; + case '/settings': + $this->bot->settingHandle(); + + break; + case '/set_menu': + $this->bot->setMyCommands(CommandService::MENU_COMMANDS); + + break; + default: + $this->bot->sendMessage('🤨 Invalid Request!'); + } + } } diff --git a/src/Http/Actions/SendNotificationAction.php b/src/Services/NotificationService.php similarity index 88% rename from src/Http/Actions/SendNotificationAction.php rename to src/Services/NotificationService.php index edf0705..35fbe5a 100644 --- a/src/Http/Actions/SendNotificationAction.php +++ b/src/Services/NotificationService.php @@ -1,15 +1,16 @@ notifier->handleEventFromRequest($this->request); if (!empty($eventName)) { @@ -51,6 +53,7 @@ public function __invoke(): void * @return void * @throws InvalidViewTemplateException * @throws SendNotificationException + * @throws MessageIsEmptyException */ private function sendNotification(string $event): void { @@ -83,7 +86,7 @@ private function sendNotification(string $event): void * @param string $event * * @return bool - * @throws InvalidViewTemplateException + * @throws InvalidViewTemplateException|MessageIsEmptyException */ private function validateAccessEvent(string $event): bool { From 0a2784bec5091a2fd8ec34a183fa302bec02640a Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Fri, 3 Nov 2023 21:43:48 +0700 Subject: [PATCH 2/2] feat: add phpstan analyse webhook --- .github/workflows/phpstan.yml | 26 ++++++++++++++++++++++++++ composer.json | 4 +++- phpstan.neon.dist | 6 ++++++ src/Services/CallbackService.php | 8 +++++++- 4 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/phpstan.yml create mode 100644 phpstan.neon.dist diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml new file mode 100644 index 0000000..de07f19 --- /dev/null +++ b/.github/workflows/phpstan.yml @@ -0,0 +1,26 @@ +name: PHPStan + +on: [push, pull_request] + +jobs: + phpstan: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@2.26.0 + with: + php-version: '8.1' + + - name: Checkout code + uses: actions/checkout@v4 + + - name: Install dependencies + run: | + composer install --no-interaction --no-progress --no-suggest + + - name: Run PHPStan + run: | + composer analyse --error-format=github diff --git a/composer.json b/composer.json index 4d9b4b6..218bd24 100755 --- a/composer.json +++ b/composer.json @@ -25,7 +25,8 @@ "cslant/telegram-git-notifier": "^1.3.1" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^v3.37.1" + "friendsofphp/php-cs-fixer": "^v3.37.1", + "phpstan/phpstan": "^1.10.39" }, "autoload": { "psr-4": { @@ -48,6 +49,7 @@ "optimize-autoloader": true }, "scripts": { + "analyse": "vendor/bin/phpstan", "format": "vendor/bin/php-cs-fixer fix --allow-risky=yes", "post-install-cmd": [ "bash vendor/cslant/telegram-git-notifier/install.sh" diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 0000000..2e837cd --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,6 @@ +parameters: + level: 8 + paths: + - src + tmpDir: build/phpstan + checkMissingIterableValueType: false diff --git a/src/Services/CallbackService.php b/src/Services/CallbackService.php index 5182734..ae649fb 100644 --- a/src/Services/CallbackService.php +++ b/src/Services/CallbackService.php @@ -86,7 +86,13 @@ public function handle(): void $callback = str_replace(SettingConstant::SETTING_PREFIX, '', $callback); - if ($this->bot->setting->updateSetting($callback, !$this->bot->setting->getSettings()[$callback])) { + $settings = $this->bot->setting->getSettings(); + if (array_key_exists($callback, $settings) + && $this->bot->setting->updateSetting( + $callback, + !$settings[$callback] + ) + ) { $this->bot->editMessageReplyMarkup([ 'reply_markup' => $this->bot->settingMarkup(), ]);