Files
domain-watchdog/src/Service/ChatNotificationService.php

61 lines
1.9 KiB
PHP
Raw Normal View History

2024-08-27 00:09:25 +02:00
<?php
namespace App\Service;
use App\Config\WebhookScheme;
use App\Entity\WatchList;
use App\Exception\UnsupportedDsnSchemeException;
use App\Notifier\DomainWatchdogNotification;
use Symfony\Component\Notifier\Exception\TransportExceptionInterface;
use Symfony\Component\Notifier\Recipient\NoRecipient;
2024-08-27 00:09:25 +02:00
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
use Symfony\Component\Notifier\Transport\Dsn;
readonly class ChatNotificationService
{
public function __construct(
) {
}
/**
* @throws UnsupportedDsnSchemeException
* @throws TransportExceptionInterface
*/
public function sendChatNotification(WatchList $watchList, DomainWatchdogNotification $notification): void
2024-08-27 00:09:25 +02:00
{
$webhookDsn = $watchList->getWebhookDsn();
2025-05-22 14:00:17 +02:00
if (empty($webhookDsn)) {
return;
}
2024-08-27 00:09:25 +02:00
foreach ($webhookDsn as $dsnString) {
$dsn = new Dsn($dsnString);
2024-08-27 00:09:25 +02:00
$scheme = $dsn->getScheme();
$webhookScheme = WebhookScheme::tryFrom($scheme);
2024-08-27 21:44:42 +02:00
if (null === $webhookScheme) {
throw new UnsupportedDsnSchemeException($scheme);
}
2024-08-27 21:44:42 +02:00
$transportFactoryClass = $webhookScheme->getChatTransportFactory();
/** @var AbstractTransportFactory $transportFactory */
$transportFactory = new $transportFactoryClass();
2024-08-27 21:44:42 +02:00
$push = $notification->asPushMessage(new NoRecipient());
$chat = $notification->asChatMessage(new NoRecipient(), $webhookScheme->value);
2024-08-27 21:44:42 +02:00
$factory = $transportFactory->create($dsn);
if ($factory->supports($push)) {
$factory->send($push);
} elseif ($factory->supports($chat)) {
$factory->send($chat);
} else {
throw new \InvalidArgumentException('Unsupported message type');
2024-08-27 00:09:25 +02:00
}
}
}
}