<?php
namespace App\EventSubscriber;
use App\Service\WebhookAttemptService;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\TerminateEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class WebhookSendSubscriber implements EventSubscriberInterface
{
private WebhookAttemptService $service;
private LoggerInterface $logger;
public function __construct(WebhookAttemptService $service, LoggerInterface $logger) {
$this->service = $service;
$this->logger = $logger;
}
public static function getSubscribedEvents(): array
{
return [KernelEvents::TERMINATE => 'sendWebookAsync'];
}
public function sendWebookAsync(TerminateEvent $event): void {
$request = $event->getRequest();
$asyncSendStorage = $request->attributes->get(WebhookAttemptService::ASYNC_SEND, null);
if(false === is_null($asyncSendStorage) && true === is_array($asyncSendStorage)) {
array_map(function($webhookAttemptId) {
$this->service->sendWebhookById($webhookAttemptId);
}, $asyncSendStorage);
}
}
}