src/EventListener/ExceptionListener.php line 47

Open in your IDE?
  1. <?php
  2. /*
  3. * Disclaimer: This source code is protected by copyright law and by
  4. * international conventions.
  5. *
  6. * Any reproduction or partial or total distribution of the source code, by any
  7. * means whatsoever, is strictly forbidden.
  8. *
  9. * Anyone not complies with these provisions will be guilty of the offense of
  10. * infringement and the penal sanctions provided for by law.
  11. *
  12. * © 2020 All rights reserved.
  13. */
  14. namespace App\EventListener;
  15. use App\Helper\LogHelper;
  16. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  17. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  18. use Symfony\Component\Mailer\MailerInterface;
  19. /**
  20.  * Class ExceptionListener
  21.  *
  22.  * @author  Rémy P. <r.peyron@ingeno.eu>
  23.  * @package App\EventListener
  24.  */
  25. class ExceptionListener
  26. {
  27.     /**
  28.      * @var LogHelper
  29.      */
  30.     private $logHelper;
  31.     /**
  32.      * @var MailerInterface
  33.      */
  34.     private $mailer;
  35.     public function __construct(MailerInterface $mailerLogHelper $logHelper)
  36.     {
  37.         $this->mailer $mailer;
  38.         $this->logHelper $logHelper;
  39.     }
  40.     public function onKernelException(ExceptionEvent $event)
  41.     {
  42.         $exception $event->getThrowable();
  43.         $this->logHelper->addLog("Exception: " $exception->getMessage(), LogHelper::TYPE_ERROR);
  44.         if ($_ENV['APP_ENV'] !== 'production') {
  45.             return;
  46.         }
  47.         if ($event->isMainRequest() || ($exception instanceof HttpExceptionInterface)) {
  48.             $ignoreMessageEvent = [
  49.                 "Access Denied",
  50.                 "No route found",
  51.                 "Integrity constraint violation",
  52.                 "object not found by the @ParamConverter",
  53.             ];
  54.             foreach ($ignoreMessageEvent as $message) {
  55.                 if (false !== strpos(strtolower($exception->getMessage()), strtolower($message))) {
  56.                     return;
  57.                 }
  58.             }
  59. //
  60. //            $email = (new TemplatedEmail())
  61. //                ->from(new Address("noreply@erp-france.eu", 'RP-France'))
  62. //                ->addTo(new Address("support@infineosas.eu", "Rémy Peyron"))
  63. //                ->subject('Erreur RP-FRANCE: ' . (new DateTime())->format("Y-m-d H:i:s"))
  64. //                ->htmlTemplate("email/exception.html.twig")
  65. //                ->context([
  66. //                    "message"    => "Une erreur critique est survenue",
  67. //                    "exception"  => $exception,
  68. //                    "postData"   => json_encode($_POST, JSON_PRETTY_PRINT),
  69. //                    "getData"    => json_encode($_GET, JSON_PRETTY_PRINT),
  70. //                    "jsonTrace"  => $exception->getTraceAsString(),
  71. //                    "ip"         => $_SERVER["SERVER_ADDR"] ?? null,
  72. //                    "requestUri" => $_SERVER["REQUEST_URI"]
  73. //                ]);
  74. //
  75. //            try {
  76. //                $this->mailer->send($email);
  77. //            } catch (TransportExceptionInterface $e) {
  78. //                return;
  79. ////                dd($e->getMessage());
  80. //            }
  81.         }
  82.     }
  83. }