src/Listener/MailArchiveListener.php line 46

Open in your IDE?
  1. <?php
  2. namespace App\Listener;
  3. use App\Entity\MailArchive\Message;
  4. use App\Service\Mailer;
  5. use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Doctrine\ORM\Events;
  8. use Doctrine\Persistence\Event\LifecycleEventArgs;
  9. use Symfony\Component\Console\Event\ConsoleTerminateEvent;
  10. use Symfony\Component\HttpKernel\Event\TerminateEvent;
  11. class MailArchiveListener implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var EntityManagerInterface
  15.      */
  16.     private $em;
  17.     /**
  18.      * @var Mailer
  19.      */
  20.     private $mailer;
  21.     /**
  22.      * @var string
  23.      */
  24.     private $fileStorage;
  25.     public function getSubscribedEvents()
  26.     {
  27.         return [Events::postLoad];
  28.     }
  29.     public function __construct(EntityManagerInterface $emMailer $mailerstring $fileStorage)
  30.     {
  31.         $this->em $em;
  32.         $this->mailer $mailer;
  33.         $this->fileStorage $fileStorage;
  34.     }
  35.     private function flushArchive()
  36.     {
  37.         $this->mailer->flushArchive();
  38.     }
  39.     public function onKernelTerminate(TerminateEvent $event)
  40.     {
  41.         $this->flushArchive();
  42.     }
  43.     public function onConsoleTerminate(ConsoleTerminateEvent $event)
  44.     {
  45.         $this->flushArchive();
  46.     }
  47.     public function postLoad(LifecycleEventArgs $args)
  48.     {
  49.         $message $args->getObject();
  50.         if (!$message instanceof Message) {
  51.             return;
  52.         }
  53.         $body sprintf('%s/mail-archive/%d.html'$this->fileStorage$message->getId());
  54.         $export sprintf('%s/mail-archive/%d.eml'$this->fileStorage$message->getId());
  55.         if (is_file($body)) {
  56.             $message->setBody(file_get_contents($body));
  57.         }
  58.         if (is_file($export)) {
  59.             $message->setExport(file_get_contents($export));
  60.         }
  61.     }
  62. }