src/EventListener/ServerError.php line 44

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Component\Communicator\Provider\Mailer;
  4. use App\Component\Authenticator\Authenticator;
  5. use Exception;
  6. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  9. use Throwable;
  10. class ServerError
  11. {
  12.     /**
  13.      * @var Authenticator
  14.      */
  15.     private $authenticator;
  16.     /**
  17.      * @var ParameterBagInterface
  18.      */
  19.     private $parameters;
  20.     /**
  21.      * @var Mailer
  22.      */
  23.     private $mailer;
  24.     function __construct(Authenticator $authenticatorParameterBagInterface $parametersMailer $mailer)
  25.     {
  26.         $this->authenticator $authenticator;
  27.         $this->parameters $parameters;
  28.         $this->mailer $mailer;
  29.     }
  30.     /**
  31.      * Determines action on kernel exception.
  32.      *
  33.      * @param ExceptionEvent $event
  34.      *
  35.      * @throws Exception
  36.      */
  37.     public function onKernelException(ExceptionEvent $event)
  38.     {
  39.         if(!$this->parameters->get('server_error_notifications')) {
  40.             return;
  41.         }
  42.         $request $event->getRequest();
  43.         $exception $event->getThrowable();
  44.         $this->sendNotifications($exception$request);
  45.     }
  46.     /**
  47.      * Sends e-mail to the webmasters with server error details.
  48.      *
  49.      * @param Exception $exception
  50.      *
  51.      * @throws Exception
  52.      */
  53.     public function sendNotifications(Throwable $exceptionRequest $request) {
  54.         $webmasters $this->parameters->get('webmasters');
  55.         if(!is_array($webmasters) || !$this->isWorthNotifying($exception)) {
  56.             return;
  57.         }
  58.         foreach($webmasters as $webmaster) {
  59.             $this->mailer->send($webmaster'Trocado: server error''error', [
  60.                 'endpoint' => $request->getMethod() . ' ' $request->getRequestUri(),
  61.                 'environment' => $this->parameters->get('kernel.environment'),
  62.                 'exception' => $exception,
  63.                 'trace' => $exception->getTrace(),
  64.                 'user' => $this->authenticator->getUser(),
  65.                 'read_unrestricted' => $this->authenticator->hasPermissionWithCodeName('read_unrestricted')
  66.             ], true);
  67.         }
  68.     }
  69.     /**
  70.      * Determine whether the exception is not worth notifying.
  71.      *
  72.      * @param Throwable $exception
  73.      *
  74.      * @return bool
  75.      */
  76.     private function isWorthNotifying(Throwable $exception): bool
  77.     {
  78.         if($this->isWindows() && (
  79.             strpos($exception->getMessage(), 'Warning: copy') !== false ||
  80.             strpos($exception->getMessage(), 'Cannot rename') !== false ||
  81.             strpos($exception->getMessage(), 'Compile Error: Doctrine\Common\Proxy\AbstractProxyFactory::getProxyDefinition()') !== false ||
  82.             strpos($exception->getMessage(), 'Attempted to load class') !== false
  83.         )) {
  84.             return false;
  85.         }
  86.         if(
  87.             strpos($exception->getMessage(), 'No route found for') !== false ||
  88.             strpos($exception->getMessage(), 'statelesscheck') !== false
  89.         ) {
  90.             return false;
  91.         }
  92.         return true;
  93.     }
  94.     /**
  95.      * Determine whether the OS is Windows.
  96.      */
  97.     private function isWindows(): bool
  98.     {
  99.         return PHP_OS_FAMILY === 'Windows';
  100.     }
  101. }