Skip to content

Commit 9a2d9f7

Browse files
authored
Merge pull request #58 from LuongTienThinh/feat/webhook-command
feat: set webhook using cli
2 parents 4f1818c + 984c930 commit 9a2d9f7

File tree

5 files changed

+117
-11
lines changed

5 files changed

+117
-11
lines changed

phpstan-baseline.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ parameters:
44
path: src/Providers/TelegramGitNotifierServiceProvider.php
55

66
- message: '#Parameter \#1 \$token of method CSlant\\TelegramGitNotifier\\Webhook\:\:setToken\(\) expects string, mixed given\.#'
7-
path: src/Http/Actions/WebhookAction.php
7+
path: src/Services/WebhookService.php
88

99
- message: '#Parameter \#1 \$url of method CSlant\\TelegramGitNotifier\\Webhook\:\:setUrl\(\) expects string, mixed given\.#'
10-
path: src/Http/Actions/WebhookAction.php
10+
path: src/Services/WebhookService.php
1111

1212
- message: '#Parameter \#1 \$prefix of static method Illuminate\\Support\\Facades\\Route::prefix\(\) expects string, mixed given\.#'
1313
path: routes/bot.php

src/Commands/SetWebhook.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace CSlant\LaravelTelegramGitNotifier\Commands;
4+
5+
use CSlant\LaravelTelegramGitNotifier\Services\WebhookService;
6+
use CSlant\TelegramGitNotifier\Exceptions\WebhookException;
7+
use Illuminate\Console\Command;
8+
9+
class SetWebhook extends Command
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'tg-notifier:webhook:set';
17+
18+
/**
19+
* The console command description.
20+
*
21+
* @var string
22+
*/
23+
protected $description = 'Set webhook';
24+
25+
/**
26+
* Execute the console command.
27+
*
28+
* @return void
29+
*/
30+
public function handle(): void
31+
{
32+
try {
33+
$webhookService = new WebhookService();
34+
35+
$this->info($webhookService->setWebhook());
36+
} catch (WebhookException $e) {
37+
$this->error($e->getMessage());
38+
}
39+
}
40+
}

src/Http/Actions/WebhookAction.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22

33
namespace CSlant\LaravelTelegramGitNotifier\Http\Actions;
44

5+
use CSlant\LaravelTelegramGitNotifier\Services\WebhookService;
56
use CSlant\TelegramGitNotifier\Exceptions\WebhookException;
6-
use CSlant\TelegramGitNotifier\Webhook;
77

88
class WebhookAction
99
{
10-
protected Webhook $webhook;
10+
protected WebhookService $webhookService;
1111

1212
public function __construct()
1313
{
14-
$this->webhook = new Webhook();
15-
$this->webhook->setToken(config('telegram-git-notifier.bot.token'));
16-
$this->webhook->setUrl(config('telegram-git-notifier.app.url'));
14+
$this->webhookService = new WebhookService();
1715
}
1816

1917
/**
@@ -25,7 +23,7 @@ public function __construct()
2523
*/
2624
public function set(): string
2725
{
28-
return $this->webhook->setWebhook();
26+
return $this->webhookService->setWebhook();
2927
}
3028

3129
/**
@@ -37,7 +35,7 @@ public function set(): string
3735
*/
3836
public function delete(): string
3937
{
40-
return $this->webhook->deleteWebHook();
38+
return $this->webhookService->deleteWebHook();
4139
}
4240

4341
/**
@@ -49,7 +47,7 @@ public function delete(): string
4947
*/
5048
public function getUpdates(): string
5149
{
52-
return $this->webhook->getUpdates();
50+
return $this->webhookService->getUpdates();
5351
}
5452

5553
/**
@@ -61,6 +59,6 @@ public function getUpdates(): string
6159
*/
6260
public function getWebHookInfo(): string
6361
{
64-
return $this->webhook->getWebHookInfo();
62+
return $this->webhookService->getWebHookInfo();
6563
}
6664
}

src/Providers/TelegramGitNotifierServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace CSlant\LaravelTelegramGitNotifier\Providers;
44

55
use CSlant\LaravelTelegramGitNotifier\Commands\ChangeOwnerConfigJson;
6+
use CSlant\LaravelTelegramGitNotifier\Commands\SetWebhook;
67
use Illuminate\Support\ServiceProvider;
78

89
class TelegramGitNotifierServiceProvider extends ServiceProvider
@@ -59,6 +60,7 @@ protected function registerCommands(): void
5960
{
6061
$this->commands([
6162
ChangeOwnerConfigJson::class,
63+
SetWebhook::class,
6264
]);
6365
}
6466

src/Services/WebhookService.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace CSlant\LaravelTelegramGitNotifier\Services;
4+
5+
use CSlant\TelegramGitNotifier\Exceptions\WebhookException;
6+
use CSlant\TelegramGitNotifier\Webhook;
7+
8+
class WebhookService
9+
{
10+
protected Webhook $webhook;
11+
12+
public function __construct(?Webhook $webhook = null)
13+
{
14+
$this->webhook = $webhook ?? new Webhook();
15+
$this->webhook->setToken(config('telegram-git-notifier.bot.token'));
16+
$this->webhook->setUrl(config('telegram-git-notifier.app.url'));
17+
}
18+
19+
/**
20+
* Set webhook for telegram bot.
21+
*
22+
* @return string
23+
*
24+
* @throws WebhookException
25+
*/
26+
public function setWebhook(): string
27+
{
28+
return $this->webhook->setWebhook();
29+
}
30+
31+
/**
32+
* Delete webhook for telegram bot.
33+
*
34+
* @return string
35+
*
36+
* @throws WebhookException
37+
*/
38+
public function deleteWebHook(): string
39+
{
40+
return $this->webhook->deleteWebHook();
41+
}
42+
43+
/**
44+
* Get webhook update.
45+
*
46+
* @return string
47+
*
48+
* @throws WebhookException
49+
*/
50+
public function getUpdates(): string
51+
{
52+
return $this->webhook->getUpdates();
53+
}
54+
55+
/**
56+
* Get webhook info.
57+
*
58+
* @return string
59+
*
60+
* @throws WebhookException
61+
*/
62+
public function getWebHookInfo(): string
63+
{
64+
return $this->webhook->getWebHookInfo();
65+
}
66+
}

0 commit comments

Comments
 (0)