src/Aqarmap/Bundle/MainBundle/EventListener/FeedbackListener.php line 57

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\MainBundle\EventListener;
  3. use Aqarmap\Bundle\MainBundle\Adapter\MailerServiceInterface;
  4. use Aqarmap\Bundle\MainBundle\Event\FeedbackEvent;
  5. use Aqarmap\Bundle\MainBundle\Helpers\MailerHelper;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Contracts\Translation\TranslatorInterface;
  10. use Twig\Environment;
  11. class FeedbackListener implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var ContainerInterface
  15.      */
  16.     protected $container;
  17.     /**
  18.      * @var MailerServiceInterface
  19.      */
  20.     protected $mailer;
  21.     /**
  22.      * @var EntityManagerInterface
  23.      */
  24.     private $entityManager;
  25.     /**
  26.      * @var TranslatorInterface
  27.      */
  28.     private $translator;
  29.     private Environment $twigEngine;
  30.     /** @var MailerHelper */
  31.     private $mailerHelper;
  32.     public function __construct(
  33.         ContainerInterface $container,
  34.         EntityManagerInterface $entityManager,
  35.         TranslatorInterface $translator,
  36.         Environment $twigEngine,
  37.         MailerServiceInterface $mailer,
  38.         MailerHelper $mailerHelper
  39.     ) {
  40.         $this->container $container;
  41.         $this->entityManager $entityManager;
  42.         $this->translator $translator;
  43.         $this->twigEngine $twigEngine;
  44.         $this->mailer $mailer;
  45.         $this->mailerHelper $mailerHelper;
  46.     }
  47.     public function onSubmitFeedback(FeedbackEvent $event): void
  48.     {
  49.         $feedback $event->getFeedback();
  50.         $message $this->getComposeMessage($feedback);
  51.         $this->getMailer()->sendMessage($message);
  52.     }
  53.     /**
  54.      * @return object
  55.      */
  56.     public function getEntityManager()
  57.     {
  58.         return $this->entityManager;
  59.     }
  60.     /**
  61.      * @return MailerServiceInterface
  62.      */
  63.     public function getMailer()
  64.     {
  65.         return $this->mailer;
  66.     }
  67.     /**
  68.      * @return TranslatorInterface
  69.      */
  70.     public function getTranslator()
  71.     {
  72.         return $this->translator;
  73.     }
  74.     public function getTemplating(): Environment
  75.     {
  76.         return $this->twigEngine;
  77.     }
  78.     public static function getSubscribedEvents()
  79.     {
  80.         return [
  81.             'aqarmap.feedback.send_message' => ['onSubmitFeedback'],
  82.         ];
  83.     }
  84.     /**
  85.      * @throws \Exception
  86.      */
  87.     private function getComposeMessage($feedback)
  88.     {
  89.         $composeMessage $this->mailerHelper->createMessageWithGlobalAttributes();
  90.         $composeMessage->setSubject(preg_replace('/\s+?(\S+)?$/'''mb_substr($feedback->getMessage(), 0100)));
  91.         $composeMessage->setTo($this->container->getParameter('feedback_email'));
  92.         $composeMessage->setReplyTo($feedback->getEmail());
  93.         $composeMessage->setTemplate('@AqarmapMain/Feedback/Email/feedbackContent.html.twig');
  94.         $composeMessage->setTemplateContext(['feedback' => $feedback]);
  95.         $compose $this->getMailer()->composeMessage($composeMessage);
  96.         $compose->getHeaders()->addTextHeader('X-Mail-Category''default-feedback');
  97.         $compose->getHeaders()->addTextHeader('X-Site-Country'$this->container->getParameter('country'));
  98.         return $compose;
  99.     }
  100. }