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

  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\ParameterBag\ParameterBagInterface;
  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 MailerServiceInterface
  15. */
  16. protected $mailer;
  17. public function __construct(
  18. private readonly ParameterBagInterface $parameterBag,
  19. private readonly EntityManagerInterface $entityManager,
  20. private readonly TranslatorInterface $translator,
  21. private readonly Environment $twigEngine,
  22. MailerServiceInterface $mailer,
  23. private readonly MailerHelper $mailerHelper,
  24. ) {
  25. $this->mailer = $mailer;
  26. }
  27. public function onSubmitFeedback(FeedbackEvent $event): void
  28. {
  29. $feedback = $event->getFeedback();
  30. $message = $this->getComposeMessage($feedback);
  31. $this->getMailer()->sendMessage($message);
  32. }
  33. /**
  34. * @return object
  35. */
  36. public function getEntityManager()
  37. {
  38. return $this->entityManager;
  39. }
  40. /**
  41. * @return MailerServiceInterface
  42. */
  43. public function getMailer()
  44. {
  45. return $this->mailer;
  46. }
  47. /**
  48. * @return TranslatorInterface
  49. */
  50. public function getTranslator()
  51. {
  52. return $this->translator;
  53. }
  54. public function getTemplating(): Environment
  55. {
  56. return $this->twigEngine;
  57. }
  58. public static function getSubscribedEvents(): array
  59. {
  60. return [
  61. 'aqarmap.feedback.send_message' => ['onSubmitFeedback'],
  62. ];
  63. }
  64. /**
  65. * @throws \Exception
  66. */
  67. private function getComposeMessage($feedback)
  68. {
  69. $composeMessage = $this->mailerHelper->createMessageWithGlobalAttributes();
  70. $composeMessage->setSubject(preg_replace('/\s+?(\S+)?$/', '', mb_substr((string) $feedback->getMessage(), 0, 100)));
  71. $composeMessage->setTo($this->parameterBag->get('feedback_email'));
  72. $composeMessage->setReplyTo($feedback->getEmail());
  73. $composeMessage->setTemplate('@AqarmapMain/Feedback/Email/feedbackContent.html.twig');
  74. $composeMessage->setTemplateContext(['feedback' => $feedback]);
  75. $compose = $this->getMailer()->composeMessage($composeMessage);
  76. $compose->getHeaders()->addTextHeader('X-Mail-Category', 'default-feedback');
  77. $compose->getHeaders()->addTextHeader('X-Site-Country', $this->parameterBag->get('country'));
  78. return $compose;
  79. }
  80. }