<?php
declare(strict_types=1);
namespace App\Subscriber;
use App\Event\Webhook\WebhookTriggeredEvent;
use App\Service\Request\ContextInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
readonly class WebhookSubscriber implements EventSubscriberInterface
{
public function __construct(
private ContextInterface $context,
private LoggerInterface $logger
) {}
public static function getSubscribedEvents(): array
{
return [
WebhookTriggeredEvent::class => 'onTriggered'
];
}
public function onTriggered(WebhookTriggeredEvent $event) : void
{
$webhookName = $event->getWebhookName();
$actionName = $event->getAction();
$data = $event->getData();
if (isset($data['error']) && $data['error']) {
$this->logger->error(\sprintf('[webhook.triggerred] webhook \'%s\' triggered on action \'%s\' with data \'%s\'',
$webhookName,
$actionName,
\is_scalar($data) ? $data : \json_encode($data)
),
[
'request-id' => $this->context->getRequestId()?->toString(),
'correlation-id' => $this->context->getCorrelationId()?->toString()
]
);
} else {
$this->logger->info(\sprintf('[webhook.triggerred] webhook \'%s\' triggered on action \'%s\' with data \'%s\'',
$webhookName,
$actionName,
\is_scalar($data) ? $data : \json_encode($data)
),
[
'request-id' => $this->context->getRequestId()?->toString(),
'correlation-id' => $this->context->getCorrelationId()?->toString()
]
);
}
}
}