src/Aqarmap/Bundle/MainBundle/EventListener/FeedbackListener.php line 57
<?php
namespace Aqarmap\Bundle\MainBundle\EventListener;
use Aqarmap\Bundle\MainBundle\Adapter\MailerServiceInterface;
use Aqarmap\Bundle\MainBundle\Event\FeedbackEvent;
use Aqarmap\Bundle\MainBundle\Helpers\MailerHelper;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
class FeedbackListener implements EventSubscriberInterface
{
/**
* @var MailerServiceInterface
*/
protected $mailer;
public function __construct(
private readonly ParameterBagInterface $parameterBag,
private readonly EntityManagerInterface $entityManager,
private readonly TranslatorInterface $translator,
private readonly Environment $twigEngine,
MailerServiceInterface $mailer,
private readonly MailerHelper $mailerHelper,
) {
$this->mailer = $mailer;
}
public function onSubmitFeedback(FeedbackEvent $event): void
{
$feedback = $event->getFeedback();
$message = $this->getComposeMessage($feedback);
$this->getMailer()->sendMessage($message);
}
/**
* @return object
*/
public function getEntityManager()
{
return $this->entityManager;
}
/**
* @return MailerServiceInterface
*/
public function getMailer()
{
return $this->mailer;
}
/**
* @return TranslatorInterface
*/
public function getTranslator()
{
return $this->translator;
}
public function getTemplating(): Environment
{
return $this->twigEngine;
}
public static function getSubscribedEvents(): array
{
return [
'aqarmap.feedback.send_message' => ['onSubmitFeedback'],
];
}
/**
* @throws \Exception
*/
private function getComposeMessage($feedback)
{
$composeMessage = $this->mailerHelper->createMessageWithGlobalAttributes();
$composeMessage->setSubject(preg_replace('/\s+?(\S+)?$/', '', mb_substr((string) $feedback->getMessage(), 0, 100)));
$composeMessage->setTo($this->parameterBag->get('feedback_email'));
$composeMessage->setReplyTo($feedback->getEmail());
$composeMessage->setTemplate('@AqarmapMain/Feedback/Email/feedbackContent.html.twig');
$composeMessage->setTemplateContext(['feedback' => $feedback]);
$compose = $this->getMailer()->composeMessage($composeMessage);
$compose->getHeaders()->addTextHeader('X-Mail-Category', 'default-feedback');
$compose->getHeaders()->addTextHeader('X-Site-Country', $this->parameterBag->get('country'));
return $compose;
}
}