Раздел «Вебхуки» в панели, команды webhook:list и webhook:test
Подписки на события видны и правятся из панели: события галочками, секрет с заменой, пробная посылка, журнал доставок со страницами. Состояние показывает число подписок и недоставленных посылок.
This commit is contained in:
1 parent
fce276bb13
commit
054c1f2897
11 files changed
+825
-2
No files matched your search
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rsgrinko\Proton\Console\Commands;
|
||||
|
||||
use Rsgrinko\Proton\Console\Command;
|
||||
use Rsgrinko\Proton\Models\Webhook;
|
||||
use Rsgrinko\Proton\Models\WebhookDelivery;
|
||||
use Rsgrinko\Proton\Webhooks\Webhooks;
|
||||
|
||||
/**
|
||||
* список подписок на события.
|
||||
*/
|
||||
final class WebhookListCommand extends Command
|
||||
{
|
||||
public function name(): string
|
||||
{
|
||||
return 'webhook:list';
|
||||
}
|
||||
|
||||
public function description(): string
|
||||
{
|
||||
return 'список подписок на события и состояние посылок';
|
||||
}
|
||||
|
||||
public function usage(): string
|
||||
{
|
||||
return 'webhook:list [--events]';
|
||||
}
|
||||
|
||||
public function run(): int
|
||||
{
|
||||
if ($this->hasOption('events')) {
|
||||
return $this->printEvents();
|
||||
}
|
||||
|
||||
/** @var array<int, Webhook> $webhooks */
|
||||
$webhooks = Webhook::query()->orderBy('id')->get();
|
||||
|
||||
if ($webhooks === []) {
|
||||
$this->line('Подписок нет. Завести: раздел «Вебхуки» в панели');
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
$rows = [];
|
||||
|
||||
foreach ($webhooks as $webhook) {
|
||||
$events = $webhook->events();
|
||||
|
||||
$rows[] = [
|
||||
(string) $webhook->id(),
|
||||
(string) $webhook->name,
|
||||
(string) $webhook->url,
|
||||
in_array(Webhook::ALL_EVENTS, $events, true) ? 'все' : (string) count($events),
|
||||
(bool) $webhook->active ? 'да' : 'нет',
|
||||
(int) $webhook->raw('last_status') > 0 ? (string) (int) $webhook->raw('last_status') : '—',
|
||||
(string) ($webhook->raw('last_sent_at') ?? '—'),
|
||||
];
|
||||
}
|
||||
|
||||
$this->table(['id', 'Название', 'Адрес', 'События', 'Включена', 'Ответ', 'Последняя посылка'], $rows);
|
||||
|
||||
$stats = WebhookDelivery::stats();
|
||||
|
||||
$this->line();
|
||||
$this->line(
|
||||
'Посылки: доставлено ' . (int) ($stats[WebhookDelivery::SENT] ?? 0)
|
||||
. ', в очереди ' . (int) ($stats[WebhookDelivery::PENDING] ?? 0)
|
||||
. ', не доставлено ' . (int) ($stats[WebhookDelivery::FAILED] ?? 0)
|
||||
);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Реестр событий: на что вообще можно подписаться.
|
||||
*/
|
||||
private function printEvents(): int
|
||||
{
|
||||
$rows = [];
|
||||
|
||||
foreach (Webhooks::groups() as $group => $events) {
|
||||
foreach ($events as $code => $label) {
|
||||
$rows[] = [$group, $code, $label];
|
||||
}
|
||||
}
|
||||
|
||||
$this->table(['Раздел', 'Событие', 'Что случилось'], $rows);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user