src/Listener/KernelEvent.php line 67

Open in your IDE?
  1. <?php
  2.     /******************************************************************************
  3.      * Copyright (c) Echo-numeric 2020-2023.                                      *
  4.      ******************************************************************************/
  5.     namespace App\Listener;
  6.     use App\Entity\User;
  7.     use App\Exception\PurchaseDeclarationException;
  8.     use App\Factory\PointFactory;
  9.     use App\Services\Common\PlatformService;
  10.     use App\Services\Common\Point\UserPointServiceInterface;
  11.     use App\Services\PurchaseDeclarationService;
  12.     use Exception;
  13.     use Spipu\Html2Pdf\Exception\Html2PdfException;
  14.     use Symfony\Component\DependencyInjection\ContainerInterface;
  15.     use Symfony\Component\HttpKernel\Event\RequestEvent;
  16.     use Symfony\Component\HttpKernel\Event\TerminateEvent;
  17.     use Twig\Error\LoaderError;
  18.     use Twig\Error\RuntimeError;
  19.     use Twig\Error\SyntaxError;
  20.     class KernelEvent
  21.     {
  22.         private PurchaseDeclarationService $purchaseDeclarationService;
  23.         private PlatformService            $platformService;
  24.         private UserPointServiceInterface $userPointService;
  25.         public function __construct(
  26.             PurchaseDeclarationService $purchaseDeclarationService,
  27.             PlatformService            $platformService,
  28.             PointFactory $pointFactory
  29.         )
  30.         {
  31.             $this->purchaseDeclarationService $purchaseDeclarationService;
  32.             $this->platformService            $platformService;
  33.             $this->userPointService $pointFactory->getUserPointService();
  34.         }
  35.         /**
  36.          * Fonction qui ajoute le sous domaine de la plateforme aux paramètres de la requête
  37.          *
  38.          * @throws Exception
  39.          */
  40.         public function onKernelRequestRequestEvent $event ): void
  41.         {
  42.             try {
  43.                 $event->getRequest()->attributes->set'subdomain'$this->platformService->getDomain() );
  44.             }
  45.             catch ( Exception $e ) {
  46.                 $event->getRequest()->attributes->set'subdomain''' );
  47.             }
  48.         }
  49.         /**
  50.          * @param TerminateEvent $event
  51.          *
  52.          * @return void
  53.          * @throws PurchaseDeclarationException
  54.          */
  55.         public function onKernelTerminateTerminateEvent $event ): void
  56.         {
  57.             $request $event->getRequest();
  58.             $method  $request->getMethod();
  59.             switch ( $request->get'_route' ) ) {
  60.                 case 'back_purchase_product_switch_enabled':
  61.                 case 'back_purchase_product_delete':
  62.                     try {
  63.                         $this->refreshReferencePdf();
  64.                     }
  65.                     catch ( Html2PdfException|LoaderError|RuntimeError|SyntaxError $e ) {
  66.                     }
  67.                     break;
  68.                 case 'back_purchase_product_edit':
  69.                     if ( $method === 'POST' ) {
  70.                         try {
  71.                             $this->refreshReferencePdf();
  72.                         }
  73.                         catch ( Html2PdfException|LoaderError|RuntimeError|SyntaxError $e ) {
  74.                         }
  75.                     }
  76.                     break;
  77.             }
  78.             $user $event->getRequest()->attributes->get('user');
  79.             if ($user instanceof User ){
  80.                 $this->userPointService->getPointsOfUser($userNULLTRUETRUE);
  81.             }
  82.         }
  83.         /**
  84.          * @return void
  85.          *
  86.          * @throws Html2PdfException
  87.          * @throws LoaderError
  88.          * @throws RuntimeError
  89.          * @throws SyntaxError
  90.          */
  91.         private function refreshReferencePdf(): void
  92.         {
  93.             $this->purchaseDeclarationService->refreshReferencePdf();
  94.         }
  95.     }