2024-08-27 00:09:25 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Service;
|
|
|
|
|
|
|
|
|
|
use App\Config\WebhookScheme;
|
2025-10-25 17:26:56 +02:00
|
|
|
use App\Entity\Watchlist;
|
2025-10-13 13:51:51 +02:00
|
|
|
use App\Exception\UnsupportedDsnSchemeException;
|
2024-08-28 17:35:32 +02:00
|
|
|
use App\Notifier\DomainWatchdogNotification;
|
2025-10-13 13:51:51 +02:00
|
|
|
use Symfony\Component\Notifier\Exception\TransportExceptionInterface;
|
2024-08-28 17:35:32 +02:00
|
|
|
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(
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-13 13:51:51 +02:00
|
|
|
/**
|
|
|
|
|
* @throws UnsupportedDsnSchemeException
|
|
|
|
|
* @throws TransportExceptionInterface
|
|
|
|
|
*/
|
2025-10-25 17:26:56 +02:00
|
|
|
public function sendChatNotification(Watchlist $watchlist, DomainWatchdogNotification $notification): void
|
2024-08-27 00:09:25 +02:00
|
|
|
{
|
2025-10-25 17:26:56 +02:00
|
|
|
$webhookDsn = $watchlist->getWebhookDsn();
|
2025-05-22 14:00:17 +02:00
|
|
|
|
|
|
|
|
if (empty($webhookDsn)) {
|
2024-12-03 21:10:38 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2024-08-27 00:09:25 +02:00
|
|
|
|
2024-12-03 21:10:38 +01:00
|
|
|
foreach ($webhookDsn as $dsnString) {
|
2025-10-13 13:51:51 +02:00
|
|
|
$dsn = new Dsn($dsnString);
|
2024-08-27 00:09:25 +02:00
|
|
|
|
2024-12-03 21:10:38 +01:00
|
|
|
$scheme = $dsn->getScheme();
|
|
|
|
|
$webhookScheme = WebhookScheme::tryFrom($scheme);
|
2024-08-27 21:44:42 +02:00
|
|
|
|
2024-12-03 21:10:38 +01:00
|
|
|
if (null === $webhookScheme) {
|
2025-10-13 13:51:51 +02:00
|
|
|
throw new UnsupportedDsnSchemeException($scheme);
|
2024-12-03 21:10:38 +01:00
|
|
|
}
|
2024-08-27 21:44:42 +02:00
|
|
|
|
2024-12-03 21:10:38 +01:00
|
|
|
$transportFactoryClass = $webhookScheme->getChatTransportFactory();
|
|
|
|
|
/** @var AbstractTransportFactory $transportFactory */
|
|
|
|
|
$transportFactory = new $transportFactoryClass();
|
2024-08-27 21:44:42 +02:00
|
|
|
|
2024-12-03 21:10:38 +01:00
|
|
|
$push = $notification->asPushMessage(new NoRecipient());
|
|
|
|
|
$chat = $notification->asChatMessage(new NoRecipient(), $webhookScheme->value);
|
2024-08-27 21:44:42 +02:00
|
|
|
|
2025-10-13 13:51:51 +02:00
|
|
|
$factory = $transportFactory->create($dsn);
|
2024-12-03 21:10:38 +01:00
|
|
|
|
2025-10-13 13:51:51 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|