src/App/Event/KernelExceptionEvent.php line 137

Open in your IDE?
  1. <?php
  2. namespace App\Event;
  3. use App\Entity\Parametre\Utilisateur;
  4. use App\Helper\MailerHelper;
  5. use Doctrine\ORM\EntityManager;
  6. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  7. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  8. use Symfony\Component\Security\Core\Authentication\Token\Storage\UsageTrackingTokenStorage;
  9. use Twig\Environment;
  10. use Webservice\Controller\AbstractWebserviceController;
  11. /**
  12.  * KernelExceptionEvent.
  13.  *
  14.  * @author John Doe
  15.  * @package App\Event
  16.  */
  17. class KernelExceptionEvent {
  18.     /** @var EntityManager */
  19.     private $em;
  20.     /** @var array */
  21.     private $smtpParameters;
  22.     /** @var UsageTrackingTokenStorage */
  23.     private $tokenStorage;
  24.     /** @var Environment */
  25.     private $twig;
  26.     public function __construct(EntityManager $emEnvironment $twigUsageTrackingTokenStorage $tokenStorage$smtpParameters) {
  27.         $this->em             $em;
  28.         $this->twig           $twig;
  29.         $this->tokenStorage   $tokenStorage;
  30.         $this->smtpParameters $smtpParameters;
  31.     }
  32.     /**
  33.      * @return EntityManager
  34.      */
  35.     private function em() {
  36.         return $this->em;
  37.     }
  38.     /**
  39.      * @return Utilisateur|null
  40.      */
  41.     private function getUser() {
  42.         if (null === $this->tokenStorage->getToken()) {
  43.             return null;
  44.         }
  45.         return $this->tokenStorage->getToken()->getUser();
  46.     }
  47.     /**
  48.      * @param ExceptionEvent $e
  49.      * @throws \Throwable
  50.      * @throws \Twig\Error\LoaderError
  51.      * @throws \Twig\Error\RuntimeError
  52.      * @throws \Twig\Error\SyntaxError
  53.      */
  54.     private function notifyTechnicalStaff(ExceptionEvent $e) {
  55.         $date            = new \DateTime();
  56.         $currentUser     $this->getUser();
  57.         $currentUserName "Non authentifié";
  58.         if (null !== $currentUser) {
  59.             $currentUserName "{$currentUser->getPrenomNom()} ({$currentUser->getEmail()}) - ID : {$currentUser->getId()}";
  60.         }
  61.         // Skip notify for specific host.
  62.         if (true === in_array($_SERVER["HTTP_HOST"], [
  63.                 "club-la-pelle.local:8080",
  64.                 "app.px.local:8080",
  65.                 "c9999999.px.local:8080",
  66.                 "c9999998.px.local:8080",
  67.             ])) {
  68. //            http_response_code(500);
  69. //            echo "<h4 class='text-danger'>Erreur 500:</h4>";
  70. //            dump($e);
  71. //            exit;
  72.             return;
  73.         }
  74.         // Ignore when text is into exception.
  75.         $ignoreMessageEvent = [
  76.             "No route found",
  77.             "Integrity constraint violation",
  78.             "object not found by the @ParamConverter",
  79.         ];
  80.         // Checking ignore message.
  81.         foreach ($ignoreMessageEvent as $message) {
  82.             if (false !== strpos(strtolower($e->getThrowable()->getMessage()), strtolower($message))) {
  83.                 return;
  84.             }
  85.         }
  86.         $notifyTo = ["sysadmin@adheria.eu"];
  87.         $postData $_POST;
  88.         foreach ($postData as $k => $v) {
  89.             if ($k === "_password" || $k === "password" || $k === "_csrf_token") {
  90.                 $postData[$k] = "<obfuscated content>";
  91.             }
  92.         }
  93.         // Submit mail to user.
  94.         $mailer = new MailerHelper($this->smtpParameters$this->twig"fr");
  95.         $mailer->sendMail(
  96.             "Layout/email/kernel-exception.html.twig",
  97.             [
  98.                 "userName"  => $currentUserName,
  99.                 "date"      => $date,
  100.                 "subject"   => "Erreur 500",
  101.                 "description" => "Une erreur interne s'est produite.",
  102.                 "request"   => $e->getRequest(),
  103.                 "postData"  => $postData,
  104.                 "getData"   => $_GET,
  105.                 "exception" => $e->getThrowable(),
  106.             ],
  107.             $notifyTo
  108.         );
  109.     }
  110.     /**
  111.      * @param ExceptionEvent $e
  112.      * @throws \Throwable
  113.      * @throws \Twig\Error\LoaderError
  114.      * @throws \Twig\Error\RuntimeError
  115.      * @throws \Twig\Error\SyntaxError
  116.      */
  117.     public function onKernelException(ExceptionEvent $e) {
  118.         if (=== preg_match("/^\/api/"$e->getRequest()->getRequestUri())) {
  119.             return $this->onKernelExceptionWebservice($e);
  120.         }
  121.         $this->notifyTechnicalStaff($e);
  122.     }
  123.     /**
  124.      * @param ExceptionEvent $e
  125.      */
  126.     public function onKernelExceptionWebservice(ExceptionEvent $e) {
  127.         header("Content-Type: application/json");
  128.         $throw  $e->getThrowable();
  129.         $output = [
  130.             "errorCode"    => $e->getThrowable()->getCode(),
  131.             "errorMessage" => $e->getThrowable()->getMessage(),
  132.         ];
  133.         switch (get_class($throw)) {
  134.             default:
  135.                 break;
  136.             case NotFoundHttpException::class:
  137.                 $output = [
  138.                     "errorCode"    => "ROUTE_NOT_FOUND",
  139.                     "errorMessage" => "This route was not found.",
  140.                 ];
  141.                 break;
  142.         }
  143.         AbstractWebserviceController::criticalJsonResponse($output);
  144.     }
  145. }