<?php
namespace Aqarmap\Bundle\MortgageBundle\EventListener;
use Aqarmap\Bundle\MainBundle\Adapter\MailerServiceInterface;
use Aqarmap\Bundle\MainBundle\Constant\Locales;
use Aqarmap\Bundle\MainBundle\Helpers\MailerHelper;
use Aqarmap\Bundle\MainBundle\Service\Setting;
use Aqarmap\Bundle\MortgageBundle\Event\MortgageRequestEvent;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class MortgageSubscribeListener implements EventSubscriberInterface
{
/**
* @var Container
*/
protected $container;
/**
* @var ParameterBagInterface
*/
protected $parameterBag;
/**
* @var MailerServiceInterface
*/
private $mailer;
/**
* @var TranslatorInterface
*/
private $translator;
/**
* @var Setting
*/
private $settings;
/**
* @var MailerHelper
*/
private $mailerHelper;
/**
* @param Container $container
*/
public function __construct(
ContainerInterface $container,
TranslatorInterface $translator,
Setting $settings,
MailerServiceInterface $mailer,
MailerHelper $mailerHelper,
ParameterBagInterface $parameterBag
) {
$this->container = $container;
$this->translator = $translator;
$this->settings = $settings;
$this->mailer = $mailer;
$this->mailerHelper = $mailerHelper;
$this->parameterBag = $parameterBag;
}
public function onCreateMortgageRequest(MortgageRequestEvent $event)
{
try {
$this->translator->setLocale(Locales::EN);
$compose = $this->getComposeMessage($event);
return $this->mailer->sendMessage($compose);
} catch (Exception $e) {
}
}
public static function getSubscribedEvents()
{
return [
'aqarmap.mortgage.request' => 'onCreateMortgageRequest',
];
}
private function getComposeMessage($event)
{
$this->translator->setLocale(Locales::EN);
$composeMessage = $this->mailerHelper->createMessageWithGlobalAttributes();
$composeMessage->setSubject($this->translator->trans('email.subject.mortgage_email'));
$composeMessage->setTo($this->settings->getSetting('features', 'mortgage_email'));
$composeMessage->setFrom($this->parameterBag->get('website_email'));
$composeMessage->setTemplate('@AqarmapMortgageBundle/Default/Email/request.html.twig');
$composeMessage->setTemplateContext(['mortgage' => $event->getMortgage()]);
$compose = $this->mailer->composeMessage($composeMessage);
$compose->getHeaders()->addTextHeader('X-Mail-Category', 'default-mortgage-lead');
$compose->getHeaders()->addTextHeader('X-Site-Country', $this->container->getParameter('country'));
return $compose;
}
}