src/Subscriber/FailedImportLogMessageSubscriber.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber;
  3. use Symfony\Component\DependencyInjection\ContainerInterface;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  6. use Symfony\Component\Mailer\MailerInterface;
  7. use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
  8. use Symfony\Component\Mime\Email;
  9. class FailedImportLogMessageSubscriber implements EventSubscriberInterface
  10. {
  11.   private ?object $loggerImport;
  12.   private MailerInterface $mailer;
  13.   /**
  14.    * @param ContainerInterface $container
  15.    * @param MailerInterface $mailer
  16.    */
  17.   public function __construct(ContainerInterface $containerMailerInterface $mailer){
  18.     $this->loggerImport $container->get('monolog.logger.import');
  19.     $this->mailer $mailer;
  20.   }
  21.   /**
  22.    * @return string[]
  23.    */
  24.   public static function getSubscribedEvents(): array
  25.   {
  26.     return [
  27.       WorkerMessageFailedEvent::class => 'onFailedMessage'
  28.     ];
  29.   }
  30.   /**
  31.    * @param WorkerMessageFailedEvent $event
  32.    * @throws TransportExceptionInterface
  33.    */
  34.   public function onFailedMessage(WorkerMessageFailedEvent $event){
  35.     $message $event->getEnvelope()->getMessage();
  36.     $trace $event->getThrowable()->getTraceAsString();
  37.     $this->loggerImport->error(get_class($message).'\n'.$trace);
  38.     $email = (new Email());
  39.     $email->from('noreply@kromi.fr');
  40.     $email->to('developers@kromi.fr');
  41.     $email->subject('Erreur : Kromi - Occupation : Import des logs');
  42.     $email->text(<<<TEXT
  43. Une erreur est survenue lors de l'import des logs
  44. TEXT);
  45.     $this->mailer->send($email);
  46.   }
  47. }