src/Listener/KernelEvent.php line 68

Open in your IDE?
  1. <?php
  2. namespace App\Listener;
  3. use App\Entity\User;
  4. use App\Exception\PurchaseDeclarationException;
  5. use App\Services\Common\Point\UserPointService;
  6. use App\Services\Common\PlatformService;
  7. use App\Services\Common\Point\UserPointServiceInterface;
  8. use App\Services\PurchaseDeclarationService;
  9. use Exception;
  10. use Spipu\Html2Pdf\Exception\Html2PdfException;
  11. use Symfony\Component\HttpKernel\Event\RequestEvent;
  12. use Symfony\Component\HttpKernel\Event\TerminateEvent;
  13. use Twig\Error\LoaderError;
  14. use Twig\Error\RuntimeError;
  15. use Twig\Error\SyntaxError;
  16. class KernelEvent
  17. {
  18. private PurchaseDeclarationService $purchaseDeclarationService;
  19. private PlatformService $platformService;
  20. private UserPointServiceInterface $userPointService;
  21. /**
  22. * @throws Exception
  23. */
  24. public function __construct(
  25. PurchaseDeclarationService $purchaseDeclarationService,
  26. PlatformService $platformService,
  27. UserPointService $userPointService
  28. )
  29. {
  30. $this->purchaseDeclarationService = $purchaseDeclarationService;
  31. $this->platformService = $platformService;
  32. $this->userPointService = $userPointService;
  33. }
  34. /**
  35. * Fonction qui ajoute le sous domaine de la plateforme aux paramètres de la requête
  36. *
  37. * @throws Exception
  38. */
  39. public function onKernelRequest( RequestEvent $event ): void
  40. {
  41. try {
  42. $event->getRequest()->attributes->set( 'subdomain', $this->platformService->getDomain() );
  43. }
  44. catch ( Exception $e ) {
  45. $event->getRequest()->attributes->set( 'subdomain', '' );
  46. }
  47. }
  48. /**
  49. * @param TerminateEvent $event
  50. *
  51. * @return void
  52. *
  53. * @throws PurchaseDeclarationException
  54. */
  55. public function onKernelTerminate( TerminateEvent $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( $user, NULL, TRUE, TRUE );
  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. }