2025-10-17 11:13:25 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Services\Channels;
|
|
|
|
|
|
|
|
|
|
use GuzzleHttp\Client;
|
2025-10-17 11:30:59 +03:00
|
|
|
use App\Services\Logger;
|
2025-10-17 11:13:25 +03:00
|
|
|
|
|
|
|
|
class WebhookChannel implements NotificationChannelInterface
|
|
|
|
|
{
|
|
|
|
|
private Client $httpClient;
|
2025-10-17 11:30:59 +03:00
|
|
|
private Logger $logger;
|
2025-10-17 11:13:25 +03:00
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->httpClient = new Client(['timeout' => 10]);
|
2025-10-17 11:30:59 +03:00
|
|
|
$this->logger = new Logger('webhook_channel');
|
2025-10-17 11:13:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function send(array $config, string $message, array $data = []): bool
|
|
|
|
|
{
|
|
|
|
|
$url = trim($config['webhook_url'] ?? '');
|
|
|
|
|
if (empty($url)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build a sane, generic JSON payload for automation tools (n8n, Zapier, etc.)
|
|
|
|
|
$payload = [
|
|
|
|
|
'event' => 'domain_expiration_alert',
|
|
|
|
|
'message' => $message,
|
|
|
|
|
'data' => $data,
|
|
|
|
|
'sent_at' => date('c')
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$response = $this->httpClient->post($url, [
|
|
|
|
|
'headers' => [
|
|
|
|
|
'Content-Type' => 'application/json'
|
|
|
|
|
],
|
|
|
|
|
'json' => $payload
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$status = $response->getStatusCode();
|
2025-10-17 11:30:59 +03:00
|
|
|
$ok = $status >= 200 && $status < 300;
|
|
|
|
|
if ($ok) {
|
|
|
|
|
$this->logger->info('Webhook sent successfully', [
|
|
|
|
|
'url' => $url,
|
|
|
|
|
'status' => $status
|
|
|
|
|
]);
|
|
|
|
|
} else {
|
|
|
|
|
$this->logger->error('Webhook responded with non-2xx', [
|
|
|
|
|
'url' => $url,
|
|
|
|
|
'status' => $status
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
return $ok;
|
2025-10-17 11:13:25 +03:00
|
|
|
} catch (\Exception $e) {
|
2025-10-17 11:30:59 +03:00
|
|
|
$this->logger->error('Webhook send failed', [
|
|
|
|
|
'url' => $url,
|
|
|
|
|
'exception' => $e->getMessage()
|
|
|
|
|
]);
|
2025-10-17 11:13:25 +03:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|