From 9400fca92a921e18d55258dc30cd92819c4a4af0 Mon Sep 17 00:00:00 2001 From: LuongTienThinh Date: Wed, 24 Jan 2024 17:22:21 +0700 Subject: [PATCH 1/7] feat: set webhook using cli --- src/Commands/SetWebhook.php | 49 +++++++++++++++++++ .../TelegramGitNotifierServiceProvider.php | 2 + src/Services/WebhookService.php | 24 +++++++++ 3 files changed, 75 insertions(+) create mode 100644 src/Commands/SetWebhook.php create mode 100644 src/Services/WebhookService.php diff --git a/src/Commands/SetWebhook.php b/src/Commands/SetWebhook.php new file mode 100644 index 0000000..a32d376 --- /dev/null +++ b/src/Commands/SetWebhook.php @@ -0,0 +1,49 @@ +webhookService = $webhookService; + } + + /** + * Execute the console command. + * + * @return void + */ + public function handle(): void + { + try { + $log = $this->webhookService->handle(); + + $this->info($log); + } catch (WebhookException $e) { + $this->error($e->getMessage()); + } + } +} diff --git a/src/Providers/TelegramGitNotifierServiceProvider.php b/src/Providers/TelegramGitNotifierServiceProvider.php index 7ed2954..34bd936 100644 --- a/src/Providers/TelegramGitNotifierServiceProvider.php +++ b/src/Providers/TelegramGitNotifierServiceProvider.php @@ -3,6 +3,7 @@ namespace CSlant\LaravelTelegramGitNotifier\Providers; use CSlant\LaravelTelegramGitNotifier\Commands\ChangeOwnerConfigJson; +use CSlant\LaravelTelegramGitNotifier\Commands\SetWebhook; use Illuminate\Support\ServiceProvider; class TelegramGitNotifierServiceProvider extends ServiceProvider @@ -59,6 +60,7 @@ protected function registerCommands(): void { $this->commands([ ChangeOwnerConfigJson::class, + SetWebhook::class, ]); } diff --git a/src/Services/WebhookService.php b/src/Services/WebhookService.php new file mode 100644 index 0000000..e37555e --- /dev/null +++ b/src/Services/WebhookService.php @@ -0,0 +1,24 @@ +webhookAction = $webhookAction; + } + + + /** + * @throws WebhookException + */ + public function handle(): string + { + return $this->webhookAction->set(); + } +} From 09342a7074da3444ab013cb08d6d59bd38893b0c Mon Sep 17 00:00:00 2001 From: LuongTienThinh Date: Thu, 25 Jan 2024 09:35:58 +0700 Subject: [PATCH 2/7] fix: change code activity stream cli set webhook --- composer.json | 8 +---- src/Commands/SetWebhook.php | 3 +- src/Http/Actions/WebhookAction.php | 18 +++++------ src/Services/WebhookService.php | 51 ++++++++++++++++++++++++++---- 4 files changed, 55 insertions(+), 25 deletions(-) diff --git a/composer.json b/composer.json index bc35369..f61fe6a 100644 --- a/composer.json +++ b/composer.json @@ -51,13 +51,7 @@ }, "scripts": { "analyse": "vendor/bin/phpstan analyse", - "format": "vendor/bin/php-cs-fixer fix --allow-risky=yes", - "post-install-cmd": [ - "bash vendor/cslant/telegram-git-notifier/install.sh" - ], - "post-update-cmd": [ - "bash vendor/cslant/telegram-git-notifier/install.sh" - ] + "format": "vendor/bin/php-cs-fixer fix --allow-risky=yes" }, "support": { "issues": "https://github.com/cslant/laravel-telegram-git-notifier/issues" diff --git a/src/Commands/SetWebhook.php b/src/Commands/SetWebhook.php index a32d376..dc77772 100644 --- a/src/Commands/SetWebhook.php +++ b/src/Commands/SetWebhook.php @@ -5,7 +5,6 @@ use CSlant\LaravelTelegramGitNotifier\Services\WebhookService; use CSlant\TelegramGitNotifier\Exceptions\WebhookException; use Illuminate\Console\Command; -use Illuminate\Support\Facades\Log; class SetWebhook extends Command { @@ -39,7 +38,7 @@ public function __construct(WebhookService $webhookService) public function handle(): void { try { - $log = $this->webhookService->handle(); + $log = $this->webhookService->setWebhook(); $this->info($log); } catch (WebhookException $e) { diff --git a/src/Http/Actions/WebhookAction.php b/src/Http/Actions/WebhookAction.php index 83ac78d..3811e7e 100644 --- a/src/Http/Actions/WebhookAction.php +++ b/src/Http/Actions/WebhookAction.php @@ -2,18 +2,16 @@ namespace CSlant\LaravelTelegramGitNotifier\Http\Actions; +use CSlant\LaravelTelegramGitNotifier\Services\WebhookService; use CSlant\TelegramGitNotifier\Exceptions\WebhookException; -use CSlant\TelegramGitNotifier\Webhook; class WebhookAction { - protected Webhook $webhook; + protected WebhookService $webhookService; - public function __construct() + public function __construct(WebhookService $webhookService) { - $this->webhook = new Webhook(); - $this->webhook->setToken(config('telegram-git-notifier.bot.token')); - $this->webhook->setUrl(config('telegram-git-notifier.app.url')); + $this->webhookService = $webhookService; } /** @@ -25,7 +23,7 @@ public function __construct() */ public function set(): string { - return $this->webhook->setWebhook(); + return $this->webhookService->setWebhook(); } /** @@ -37,7 +35,7 @@ public function set(): string */ public function delete(): string { - return $this->webhook->deleteWebHook(); + return $this->webhookService->deleteWebHook(); } /** @@ -49,7 +47,7 @@ public function delete(): string */ public function getUpdates(): string { - return $this->webhook->getUpdates(); + return $this->webhookService->getUpdates(); } /** @@ -61,6 +59,6 @@ public function getUpdates(): string */ public function getWebHookInfo(): string { - return $this->webhook->getWebHookInfo(); + return $this->webhookService->getWebHookInfo(); } } diff --git a/src/Services/WebhookService.php b/src/Services/WebhookService.php index e37555e..91bdc1e 100644 --- a/src/Services/WebhookService.php +++ b/src/Services/WebhookService.php @@ -2,23 +2,62 @@ namespace CSlant\LaravelTelegramGitNotifier\Services; -use CSlant\LaravelTelegramGitNotifier\Http\Actions\WebhookAction; use CSlant\TelegramGitNotifier\Exceptions\WebhookException; +use CSlant\TelegramGitNotifier\Webhook; class WebhookService { - protected WebhookAction $webhookAction; + protected Webhook $webhook; - public function __construct(WebhookAction $webhookAction) { - $this->webhookAction = $webhookAction; + public function __construct(Webhook $webhook) + { + $this->webhook = $webhook; + $this->webhook->setToken(config('telegram-git-notifier.bot.token')); + $this->webhook->setUrl(config('telegram-git-notifier.app.url')); + } + + /** + * Set webhook for telegram bot. + * + * @return string + * @throws WebhookException + */ + public function setWebhook(): string + { + return $this->webhook->setWebhook(); + } + + /** + * Delete webhook for telegram bot. + * + * @return string + * @throws WebhookException + */ + public function deleteWebHook(): string + { + return $this->webhook->deleteWebHook(); } + /** + * Get webhook update. + * + * @return string + * @throws WebhookException + */ + public function getUpdates(): string + { + return $this->webhook->getUpdates(); + } /** + * Get webhook info. + * + * @return string + * * @throws WebhookException */ - public function handle(): string + public function getWebHookInfo(): string { - return $this->webhookAction->set(); + return $this->webhook->getWebHookInfo(); } } From 2f2db72ee4f443ea41faf63c1458b2ff513c4522 Mon Sep 17 00:00:00 2001 From: LuongTienThinh <91936861+LuongTienThinh@users.noreply.github.com> Date: Thu, 25 Jan 2024 09:42:55 +0700 Subject: [PATCH 3/7] revert composer.json --- composer.json | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index f61fe6a..bc35369 100644 --- a/composer.json +++ b/composer.json @@ -51,7 +51,13 @@ }, "scripts": { "analyse": "vendor/bin/phpstan analyse", - "format": "vendor/bin/php-cs-fixer fix --allow-risky=yes" + "format": "vendor/bin/php-cs-fixer fix --allow-risky=yes", + "post-install-cmd": [ + "bash vendor/cslant/telegram-git-notifier/install.sh" + ], + "post-update-cmd": [ + "bash vendor/cslant/telegram-git-notifier/install.sh" + ] }, "support": { "issues": "https://github.com/cslant/laravel-telegram-git-notifier/issues" From 03e6827987295fbd6096e81decb8f29df6d04d7c Mon Sep 17 00:00:00 2001 From: LuongTienThinh Date: Thu, 25 Jan 2024 10:20:49 +0700 Subject: [PATCH 4/7] fix: error analyse testing --- phpstan-baseline.neon | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 2cfb5a2..eb32fda 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -4,10 +4,10 @@ parameters: path: src/Providers/TelegramGitNotifierServiceProvider.php - message: '#Parameter \#1 \$token of method CSlant\\TelegramGitNotifier\\Webhook\:\:setToken\(\) expects string, mixed given\.#' - path: src/Http/Actions/WebhookAction.php + path: src/Services/WebhookService.php - message: '#Parameter \#1 \$url of method CSlant\\TelegramGitNotifier\\Webhook\:\:setUrl\(\) expects string, mixed given\.#' - path: src/Http/Actions/WebhookAction.php + path: src/Services/WebhookService.php - message: '#Parameter \#1 \$prefix of static method Illuminate\\Support\\Facades\\Route::prefix\(\) expects string, mixed given\.#' path: routes/bot.php From 7d28bd88aef7f180922a77ca2b84e030f8c4787b Mon Sep 17 00:00:00 2001 From: LuongTienThinh Date: Thu, 25 Jan 2024 10:23:09 +0700 Subject: [PATCH 5/7] fix: error format testing --- src/Services/WebhookService.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Services/WebhookService.php b/src/Services/WebhookService.php index 91bdc1e..8b14e6f 100644 --- a/src/Services/WebhookService.php +++ b/src/Services/WebhookService.php @@ -20,6 +20,7 @@ public function __construct(Webhook $webhook) * Set webhook for telegram bot. * * @return string + * * @throws WebhookException */ public function setWebhook(): string @@ -31,6 +32,7 @@ public function setWebhook(): string * Delete webhook for telegram bot. * * @return string + * * @throws WebhookException */ public function deleteWebHook(): string @@ -42,6 +44,7 @@ public function deleteWebHook(): string * Get webhook update. * * @return string + * * @throws WebhookException */ public function getUpdates(): string From 476db7d032d5cb1643d4cb2c8f36a8ef8d144a98 Mon Sep 17 00:00:00 2001 From: LuongTienThinh Date: Thu, 25 Jan 2024 11:37:56 +0700 Subject: [PATCH 6/7] refactor: optimize set webhook command --- src/Commands/SetWebhook.php | 10 +--------- src/Http/Actions/WebhookAction.php | 4 ++-- src/Services/WebhookService.php | 4 ++-- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/src/Commands/SetWebhook.php b/src/Commands/SetWebhook.php index dc77772..171f8dd 100644 --- a/src/Commands/SetWebhook.php +++ b/src/Commands/SetWebhook.php @@ -22,14 +22,6 @@ class SetWebhook extends Command */ protected $description = 'Set webhook'; - protected WebhookService $webhookService; - - public function __construct(WebhookService $webhookService) - { - parent::__construct(); - $this->webhookService = $webhookService; - } - /** * Execute the console command. * @@ -38,7 +30,7 @@ public function __construct(WebhookService $webhookService) public function handle(): void { try { - $log = $this->webhookService->setWebhook(); + $log = (new WebhookService())->setWebhook(); $this->info($log); } catch (WebhookException $e) { diff --git a/src/Http/Actions/WebhookAction.php b/src/Http/Actions/WebhookAction.php index 3811e7e..d5c36c8 100644 --- a/src/Http/Actions/WebhookAction.php +++ b/src/Http/Actions/WebhookAction.php @@ -9,9 +9,9 @@ class WebhookAction { protected WebhookService $webhookService; - public function __construct(WebhookService $webhookService) + public function __construct() { - $this->webhookService = $webhookService; + $this->webhookService = new WebhookService(); } /** diff --git a/src/Services/WebhookService.php b/src/Services/WebhookService.php index 8b14e6f..ba05c62 100644 --- a/src/Services/WebhookService.php +++ b/src/Services/WebhookService.php @@ -9,9 +9,9 @@ class WebhookService { protected Webhook $webhook; - public function __construct(Webhook $webhook) + public function __construct() { - $this->webhook = $webhook; + $this->webhook = new Webhook(); $this->webhook->setToken(config('telegram-git-notifier.bot.token')); $this->webhook->setUrl(config('telegram-git-notifier.app.url')); } From 984c930e81e03bf212cee918938b23ce89bc48e2 Mon Sep 17 00:00:00 2001 From: LuongTienThinh Date: Thu, 25 Jan 2024 12:51:19 +0700 Subject: [PATCH 7/7] refactor: optimize set webhook command 1 --- src/Commands/SetWebhook.php | 4 ++-- src/Services/WebhookService.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Commands/SetWebhook.php b/src/Commands/SetWebhook.php index 171f8dd..b8ae818 100644 --- a/src/Commands/SetWebhook.php +++ b/src/Commands/SetWebhook.php @@ -30,9 +30,9 @@ class SetWebhook extends Command public function handle(): void { try { - $log = (new WebhookService())->setWebhook(); + $webhookService = new WebhookService(); - $this->info($log); + $this->info($webhookService->setWebhook()); } catch (WebhookException $e) { $this->error($e->getMessage()); } diff --git a/src/Services/WebhookService.php b/src/Services/WebhookService.php index ba05c62..3d6ea8a 100644 --- a/src/Services/WebhookService.php +++ b/src/Services/WebhookService.php @@ -9,9 +9,9 @@ class WebhookService { protected Webhook $webhook; - public function __construct() + public function __construct(?Webhook $webhook = null) { - $this->webhook = new Webhook(); + $this->webhook = $webhook ?? new Webhook(); $this->webhook->setToken(config('telegram-git-notifier.bot.token')); $this->webhook->setUrl(config('telegram-git-notifier.app.url')); }