src/Subscriber/WebhookSubscriber.php line 34

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Subscriber;
  4. use App\Event\Webhook\WebhookTriggeredEvent;
  5. use App\Service\Request\ContextInterface;
  6. use Psr\Log\LoggerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. readonly class WebhookSubscriber implements EventSubscriberInterface
  9. {
  10.     public function __construct(
  11.         private ContextInterface $context,
  12.         private LoggerInterface $logger
  13.     ) {}
  14.     public static function getSubscribedEvents(): array
  15.     {
  16.         return [
  17.             WebhookTriggeredEvent::class => 'onTriggered'
  18.         ];
  19.     }
  20.     public function onTriggered(WebhookTriggeredEvent $event) : void
  21.     {
  22.         $webhookName $event->getWebhookName();
  23.         $actionName $event->getAction();
  24.         $data $event->getData();
  25.         if (isset($data['error']) && $data['error']) {
  26.             $this->logger->error(\sprintf('[webhook.triggerred] webhook \'%s\' triggered on action \'%s\' with data \'%s\'',
  27.                 $webhookName,
  28.                 $actionName,
  29.                 \is_scalar($data) ? $data \json_encode($data)
  30.             ),
  31.                 [
  32.                     'request-id' => $this->context->getRequestId()?->toString(),
  33.                     'correlation-id' => $this->context->getCorrelationId()?->toString()
  34.                 ]
  35.             );
  36.         } else {
  37.             $this->logger->info(\sprintf('[webhook.triggerred] webhook \'%s\' triggered on action \'%s\' with data \'%s\'',
  38.                 $webhookName,
  39.                 $actionName,
  40.                 \is_scalar($data) ? $data \json_encode($data)
  41.             ),
  42.                 [
  43.                     'request-id' => $this->context->getRequestId()?->toString(),
  44.                     'correlation-id' => $this->context->getCorrelationId()?->toString()
  45.                 ]
  46.             );
  47.         }
  48.     }
  49. }