<?php /****************************************************************************** * Copyright (c) Echo-numeric 2020-2023. * ******************************************************************************/ namespace App\Listener; use App\Entity\User; use App\Exception\PurchaseDeclarationException; use App\Factory\PointFactory; use App\Services\Common\PlatformService; use App\Services\Common\Point\UserPointServiceInterface; use App\Services\PurchaseDeclarationService; use Exception; use Spipu\Html2Pdf\Exception\Html2PdfException; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpKernel\Event\RequestEvent; use Symfony\Component\HttpKernel\Event\TerminateEvent; use Twig\Error\LoaderError; use Twig\Error\RuntimeError; use Twig\Error\SyntaxError; class KernelEvent { private PurchaseDeclarationService $purchaseDeclarationService; private PlatformService $platformService; private UserPointServiceInterface $userPointService; public function __construct( PurchaseDeclarationService $purchaseDeclarationService, PlatformService $platformService, PointFactory $pointFactory ) { $this->purchaseDeclarationService = $purchaseDeclarationService; $this->platformService = $platformService; $this->userPointService = $pointFactory->getUserPointService(); } /** * Fonction qui ajoute le sous domaine de la plateforme aux paramètres de la requête * * @throws Exception */ public function onKernelRequest( RequestEvent $event ): void { try { $event->getRequest()->attributes->set( 'subdomain', $this->platformService->getDomain() ); } catch ( Exception $e ) { $event->getRequest()->attributes->set( 'subdomain', '' ); } } /** * @param TerminateEvent $event * * @return void * @throws PurchaseDeclarationException */ public function onKernelTerminate( TerminateEvent $event ): void { $request = $event->getRequest(); $method = $request->getMethod(); switch ( $request->get( '_route' ) ) { case 'back_purchase_product_switch_enabled': case 'back_purchase_product_delete': try { $this->refreshReferencePdf(); } catch ( Html2PdfException|LoaderError|RuntimeError|SyntaxError $e ) { } break; case 'back_purchase_product_edit': if ( $method === 'POST' ) { try { $this->refreshReferencePdf(); } catch ( Html2PdfException|LoaderError|RuntimeError|SyntaxError $e ) { } } break; } $user = $event->getRequest()->attributes->get('user'); if ($user instanceof User ){ $this->userPointService->getPointsOfUser($user, NULL, TRUE, TRUE); } } /** * @return void * * @throws Html2PdfException * @throws LoaderError * @throws RuntimeError * @throws SyntaxError */ private function refreshReferencePdf(): void { $this->purchaseDeclarationService->refreshReferencePdf(); } }