src/Aqarmap/Bundle/MortgageBundle/EventListener/MortgageSubscribeListener.php line 67

  1. <?php
  2. namespace Aqarmap\Bundle\MortgageBundle\EventListener;
  3. use Aqarmap\Bundle\MainBundle\Adapter\MailerServiceInterface;
  4. use Aqarmap\Bundle\MainBundle\Constant\Locales;
  5. use Aqarmap\Bundle\MainBundle\Helpers\MailerHelper;
  6. use Aqarmap\Bundle\MainBundle\Service\Setting;
  7. use Aqarmap\Bundle\MortgageBundle\Event\MortgageRequestEvent;
  8. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Contracts\Translation\TranslatorInterface;
  11. class MortgageSubscribeListener implements EventSubscriberInterface
  12. {
  13. /**
  14. * @var ParameterBagInterface
  15. */
  16. protected $parameterBag;
  17. public function __construct(
  18. private readonly TranslatorInterface $translator,
  19. private readonly Setting $settings,
  20. private readonly MailerServiceInterface $mailer,
  21. private readonly MailerHelper $mailerHelper,
  22. ParameterBagInterface $parameterBag,
  23. ) {
  24. $this->parameterBag = $parameterBag;
  25. }
  26. public function onCreateMortgageRequest(MortgageRequestEvent $event)
  27. {
  28. try {
  29. $this->translator->setLocale(Locales::EN);
  30. $compose = $this->getComposeMessage($event);
  31. return $this->mailer->sendMessage($compose);
  32. } catch (Exception) {
  33. }
  34. }
  35. public static function getSubscribedEvents(): array
  36. {
  37. return [
  38. 'aqarmap.mortgage.request' => 'onCreateMortgageRequest',
  39. ];
  40. }
  41. private function getComposeMessage($event)
  42. {
  43. $this->translator->setLocale(Locales::EN);
  44. $composeMessage = $this->mailerHelper->createMessageWithGlobalAttributes();
  45. $composeMessage->setSubject($this->translator->trans('email.subject.mortgage_email'));
  46. $composeMessage->setTo($this->settings->getSetting('features', 'mortgage_email'));
  47. $composeMessage->setFrom($this->parameterBag->get('website_email'));
  48. $composeMessage->setTemplate('@AqarmapMortgageBundle/Default/Email/request.html.twig');
  49. $composeMessage->setTemplateContext(['mortgage' => $event->getMortgage()]);
  50. $compose = $this->mailer->composeMessage($composeMessage);
  51. $compose->getHeaders()->addTextHeader('X-Mail-Category', 'default-mortgage-lead');
  52. $compose->getHeaders()->addTextHeader('X-Site-Country', $this->parameterBag->get('country'));
  53. return $compose;
  54. }
  55. }