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

feat: set webhook using cli #58

Merged
merged 7 commits into from
Jan 25, 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
4 changes: 2 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions src/Commands/SetWebhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace CSlant\LaravelTelegramGitNotifier\Commands;

use CSlant\LaravelTelegramGitNotifier\Services\WebhookService;
use CSlant\TelegramGitNotifier\Exceptions\WebhookException;
use Illuminate\Console\Command;

class SetWebhook extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'tg-notifier:webhook:set';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Set webhook';

/**
* Execute the console command.
*
* @return void
*/
public function handle(): void
{
try {
$webhookService = new WebhookService();

$this->info($webhookService->setWebhook());
} catch (WebhookException $e) {
$this->error($e->getMessage());
}
}
}
16 changes: 7 additions & 9 deletions src/Http/Actions/WebhookAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
$this->webhook = new Webhook();
$this->webhook->setToken(config('telegram-git-notifier.bot.token'));
$this->webhook->setUrl(config('telegram-git-notifier.app.url'));
$this->webhookService = new WebhookService();
}

/**
Expand All @@ -25,7 +23,7 @@ public function __construct()
*/
public function set(): string
{
return $this->webhook->setWebhook();
return $this->webhookService->setWebhook();
}

/**
Expand All @@ -37,7 +35,7 @@ public function set(): string
*/
public function delete(): string
{
return $this->webhook->deleteWebHook();
return $this->webhookService->deleteWebHook();
}

/**
Expand All @@ -49,7 +47,7 @@ public function delete(): string
*/
public function getUpdates(): string
{
return $this->webhook->getUpdates();
return $this->webhookService->getUpdates();
}

/**
Expand All @@ -61,6 +59,6 @@ public function getUpdates(): string
*/
public function getWebHookInfo(): string
{
return $this->webhook->getWebHookInfo();
return $this->webhookService->getWebHookInfo();
}
}
2 changes: 2 additions & 0 deletions src/Providers/TelegramGitNotifierServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -59,6 +60,7 @@ protected function registerCommands(): void
{
$this->commands([
ChangeOwnerConfigJson::class,
SetWebhook::class,
]);
}

Expand Down
66 changes: 66 additions & 0 deletions src/Services/WebhookService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace CSlant\LaravelTelegramGitNotifier\Services;

use CSlant\TelegramGitNotifier\Exceptions\WebhookException;
use CSlant\TelegramGitNotifier\Webhook;

class WebhookService
{
protected Webhook $webhook;

public function __construct(?Webhook $webhook = null)
{
$this->webhook = $webhook ?? new 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 getWebHookInfo(): string
{
return $this->webhook->getWebHookInfo();
}
}