src/EventSubscriber/RequestSubscriber.php line 19

  1. <?php
  2. namespace App\EventSubscriber;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Event\RequestEvent;
  6. use Twig\Environment;
  7. class RequestSubscriber implements EventSubscriberInterface
  8. {
  9.     public function __construct(
  10.         private EntityManagerInterface $em,
  11.         private \App\Repository\WhitelabelRepository $whitelabelRepository,
  12.         private Environment $twig,
  13.     ) {
  14.     }
  15.     public function onKernelRequest(RequestEvent $event)
  16.     {
  17.         // The isMainRequest() method was introduced in Symfony 5.3.
  18.         // In previous versions it was called isMasterRequest()
  19.         if (!$event->isMainRequest()) {
  20.             // don't do anything if it's not the main request
  21.             return;
  22.         }
  23.         if ('json' == $event->getRequest()->getContentTypeFormat()) {
  24.             $data json_decode($event->getRequest()->getContent(), true);
  25.             if (!empty($data) && is_array($data)) {
  26.                 $event->getRequest()->request->replace($data);
  27.             }
  28.         }
  29.         $domain $event->getRequest()->getHost();
  30.         if (!empty($_ENV['WHITELABEL_DOMAIN'])) {
  31.             $domain $_ENV['WHITELABEL_DOMAIN'];
  32.         }
  33.         $currentWhitelabel $this->whitelabelRepository->findCurrent();
  34.         if (empty($currentWhitelabel)) {
  35.             throw new \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException('Невалиден домейн: '.$domain);
  36.         }
  37.         $this->twig->addGlobal('currentWhitelabel', [
  38.             'id' => $currentWhitelabel->getId(),
  39.             'oneSignalApiId' => $currentWhitelabel->getOneSignalApiId(),
  40.         ]);
  41.         $this->em->getFilters()->enable('whitelabel')->setParameter('filterWhitelabelId'$currentWhitelabel->getId());
  42.     }
  43.     public static function getSubscribedEvents(): array
  44.     {
  45.         return [
  46.             RequestEvent::class => ['onKernelRequest'512],
  47.         ];
  48.     }
  49. }