src/AppBundle/EventSubscriber/WebhookSubscriber.php line 54

Open in your IDE?
  1. <?php
  2. namespace AppBundle\EventSubscriber;
  3. use AppBundle\Annotation\Webhook;
  4. use AppBundle\Logger\ApiLoggerInterface;
  5. use AppBundle\Logger\ZohoSubcriptionWebhookLogger;
  6. use Doctrine\Common\Annotations\AnnotationReader;
  7. use Doctrine\ORM\Exception\ORMException;
  8. use Psr\Container\ContainerInterface;
  9. use Symfony\Contracts\Service\ServiceSubscriberInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. class WebhookSubscriber implements ServiceSubscriberInterfaceEventSubscriberInterface
  15. {
  16.     /**
  17.      * @var ContainerInterface
  18.      */
  19.     private $container;
  20.     public function __construct(ContainerInterface $container)
  21.     {
  22.         $this->container $container;
  23.     }
  24.     /**
  25.      * @return array<mixed>
  26.      */
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             KernelEvents::RESPONSE => ['onKernelResponse'1000],
  31.         ];
  32.     }
  33.     /**
  34.      * @return array<string>
  35.      */
  36.     public static function getSubscribedServices(): array
  37.     {
  38.         return [
  39.             ZohoSubcriptionWebhookLogger::class,
  40.         ];
  41.     }
  42.     /**
  43.      * @param ResponseEvent $responseEvent
  44.      * @return void
  45.      * @throws ORMException
  46.      */
  47.     public function onKernelResponse(ResponseEvent $responseEvent): void
  48.     {
  49.         $request $responseEvent->getRequest();
  50.         $response $responseEvent->getResponse();
  51.         $annotation $this->getWehookAnnotation($request);
  52.         if ($annotation && $annotation->getLogger()) {
  53.             /** @var ApiLoggerInterface $logger */
  54.             $logger $this->container->get((string) $annotation->getLogger());
  55.             $logger->log($request$response);
  56.         }
  57.     }
  58.     /**
  59.      * @param Request $request
  60.      * @return Webhook|null
  61.      */
  62.     private function getWehookAnnotation(Request $request): ?Webhook {
  63.         try {
  64.             $annotations = [];
  65.             if ($controller $request->attributes->get('_controller')) {
  66.                 /** @var string[] $splittedController */
  67.                 $splittedController explode(':'$controller);
  68.                 /** @var string $controllerName */
  69.                 $controllerName count($splittedController) > $splittedController[0] : $controller;
  70.                 $reflectedMethod = new \ReflectionClass($controllerName);
  71.                 $annotations array_filter(
  72.                     (new AnnotationReader())->getClassAnnotations($reflectedMethod),
  73.                     static function ($annotation) { return $annotation instanceof Webhook; }
  74.                 );
  75.             }
  76.             return count($annotations) > current($annotations) : null;
  77.         } catch (\Exception $e) {
  78.         }
  79.         return null;
  80.     }
  81. }