src/EventSubscriber/Core/MailerSubscriber.php line 108

Open in your IDE?
  1. <?php
  2. /*
  3.  * @since 1.0.0
  4.  * @copyright Copyright (C) 2020 ArtMedia. All rights reserved.
  5.  * @website http://artmedia.biz.pl
  6.  * @author Arkadiusz Tobiasz
  7.  * @email kontakt@artmedia.biz.pl
  8.  */
  9. namespace App\EventSubscriber\Core;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use App\Events\Core\CronJobAddEvent;
  12. use App\Events\Core\MailAddEvent;
  13. use App\Events\Core\MailQueueSendEvent;
  14. use App\Events\Core\MailDeleteEvent;
  15. use App\Model\Core\CronJob;
  16. use App\Entity\Core\Mail;
  17. use Doctrine\ORM\EntityManagerInterface;
  18. use App\Settings\SettingsManager;
  19. use App\Manager\Core\Mail\MailsManager;
  20. class MailerSubscriber implements EventSubscriberInterface
  21. {
  22.     private $em;
  23.     private $settingsManager;
  24.     private $mailsManager;
  25.     public function __construct(
  26.         MailsManager $mailsManager,
  27.         SettingsManager $settingsManager,
  28.         EntityManagerInterface $em
  29.     ) {
  30.         $this->mailsManager $mailsManager;
  31.         $this->settingsManager $settingsManager;
  32.         $this->em $em;
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             CronJobAddEvent::NAME => 'onCronJobAddEvent',
  38.             MailAddEvent::NAME => 'onMailAddEvent',
  39.             MailQueueSendEvent::NAME => 'onMailQueueSendEvent',
  40.             MailDeleteEvent::NAME => 'onMailDeleteEvent',
  41.         ];
  42.     }
  43.     public function onMailDeleteEvent(MailDeleteEvent $event): void
  44.     {
  45.         $period $this->settingsManager->getValue(SettingsManager::CORE_MAILER_DELETE_PERIOD);
  46.         $this->em->getRepository(Mail::class)->onMailDeleteEvent($period);
  47.     }
  48.     public function onMailQueueSendEvent(MailQueueSendEvent $event): void
  49.     {
  50.         $limit $this->settingsManager->getValue(SettingsManager::CORE_MAIL_BULK_LIMIT);
  51.         $mails $this->em->getRepository(Mail::class)->getListToSend($limit);
  52.         if ($mails) {
  53.             foreach ($mails as $mail) {
  54.                 $this->mailsManager->create(
  55.                     $mail->getSender(),
  56.                     $mail->getReceiver(),
  57.                     $mail->getSubject(),
  58.                     $mail->getHtml(),
  59.                     $mail->getTxt(),
  60.                     [],
  61.                     $mail->getPriority(),
  62.                     $mail->getAttachments(),
  63.                     false
  64.                 );
  65.                 $this->mailsManager->send();
  66.                 
  67.                 $mail->setSendDate(new \DateTime());
  68.             }
  69.             $this->em->flush();
  70.         }
  71.     }
  72.     public function onMailAddEvent(MailAddEvent $event): void
  73.     {
  74.         $this->mailsManager->create(
  75.             $event->getSender(),
  76.             $event->getReceiver(),
  77.             $event->getSubject(),
  78.             $event->getHtml(),
  79.             $event->getTxt(),
  80.             $event->getParams(),
  81.             $event->getPriority(),
  82.         );
  83.         if ($event->getAttachments()) {
  84.             foreach ($event->getAttachments() as $attachment) {
  85.                 $this->mailsManager->addAttachment($attachment);
  86.             }
  87.         }
  88.         
  89.         if ($event->isAddToQueue()) {
  90.             $this->mailsManager->addToQuene();
  91.         } else {
  92.             $this->mailsManager->addToQuene(true);
  93.             $this->mailsManager->send();
  94.         }
  95.     }
  96.     public function onCronJobAddEvent(CronJobAddEvent $event): void
  97.     {
  98.         $cronJob = new CronJob();
  99.         $cronJob->setName('Send mailer queue')
  100.             ->setEvent(MailQueueSendEvent::NAME)
  101.             ->setClass('App\Events\Core\MailQueueSendEvent')
  102.             ->setInterval(5)
  103.         ;
  104.         $event->addCronJob($cronJob);
  105.         $cronJob = new CronJob();
  106.         $cronJob->setName('Delete send emails')
  107.             ->setEvent(MailDeleteEvent::NAME)
  108.             ->setClass('App\Events\Core\MailDeleteEvent')
  109.             ->setInterval(1440)
  110.         ;
  111.         $event->addCronJob($cronJob);
  112.     }
  113. }