<?php
namespace Aqarmap\Bundle\ListingBundle\EventListener;
use Aqarmap\Bundle\ListingBundle\Constant\LeadTypes;
use Aqarmap\Bundle\ListingBundle\Event\LeadEvent;
use Aqarmap\Bundle\ListingBundle\Model\LeadModel;
use Aqarmap\Bundle\ListingBundle\Service\LeadService;
use Aqarmap\Bundle\MainBundle\Constant\ProcessEmailBodyTypes;
use Aqarmap\Bundle\MainBundle\Constant\ProducerQueues;
use Aqarmap\Bundle\MainBundle\Contract\ProducerFactoryInterface;
use Aqarmap\Bundle\MainBundle\Model\MailUtilitiesModel;
use Aqarmap\Bundle\UserBundle\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class LeadSubscribeListener implements EventSubscriberInterface
{
public const USERNAME_FIRST_LETTER_INDEX = 0;
public const USERNAME_LAST_LETTER_INDEX = 5;
/**
* @var EntityManagerInterface
*/
protected $em;
/**
* @var LeadService
*/
protected $leadService;
/**
* @var ParameterBagInterface
*/
protected $parameterBagInterface;
/**
* @var TokenStorageInterface
*/
private $tokenStorage;
/**
* @var TranslatorInterface
*/
private $translator;
/**
* @var ProducerFactoryInterface
*/
private $producerFactory;
public function __construct(
EntityManagerInterface $em,
LeadService $leadService,
ParameterBagInterface $parameterBag,
TokenStorageInterface $tokenStorage,
TranslatorInterface $translator,
ProducerFactoryInterface $producerFactory
) {
$this->em = $em;
$this->leadService = $leadService;
$this->parameterBag = $parameterBag;
$this->tokenStorage = $tokenStorage;
$this->translator = $translator;
$this->producerFactory = $producerFactory;
}
public function onContactSeller(LeadEvent $event): void
{
if (null != $event->getSource()
&& \in_array($event->getSource(), [LeadTypes::TARGET_USER_MESSAGE, LeadTypes::USER_DOMAIN_MESSAGE])
) {
return;
}
$source = 'message-subscriber';
$leadModel = new LeadModel();
$leadModel->setLeadType(LeadTypes::SEND_MESSAGE);
$leadModel->setListing($event->getListing());
$leadModel->setUser($event->getUser());
$leadModel->setSource($source);
$leadModel->setCampaign($event->getCampaign());
$this->leadService->addLead($leadModel, true);
}
public function onShowPhoneNumber(LeadEvent $event): void
{
$listing = $event->getListing();
$lead = $event->getLead();
// Check user listing role
if ($listing->getParent() && $listing->getParent()->getSection()->getSearchable()) {
return;
}
$token = $this->tokenStorage->getToken();
$user = $token ?: null;
if (!$user instanceof User) {
$user = $lead->getUser();
}
$leadsInsightsEnabled = true;
/** @var User $participant */
foreach ($listing->getAllParticipants() as $participant) {
if ($lead
&& (!$listing->getSection()->getSearchable() || $listing->getParent())) {
$leadsInsightsEnabled = true;
$user = $lead->getUser();
}
$subjectUsername = $lead->getFullName() ?: $user->getUsername();
$mailUtility = new MailUtilitiesModel();
$mailUtility->setSubject($subjectUsername);
$mailUtility->setReplyTo([$listing->getUser()->getEmailCanonical()]);
$mailUtility->setTo([$participant->getEmailCanonical()]);
$this->translator->setLocale($participant->getLanguage() ?? $this->parameterBag->get('locale'));
if ($user->getMainNumber() && 'undefined' != $user->getMainNumber()->getPhone()->getNumber()
|| 'undefined' != $lead->getPhone()->getNumber() || 'undefined' != $lead->getEmail()) {
$mailUtilities = $this->buildMailUtilities($mailUtility);
$producerFactory = $this->producerFactory;
$producer = $producerFactory->create(ProducerQueues::PROCESS_EMAIL_BODY);
$producer->publish(serialize([ProducerQueues::PROCESS_EMAIL_BODY_KEY => [
'type' => ProcessEmailBodyTypes::SHOW_PHONE_NUMBER_MAILS,
'template' => '@AqarmapListingBundle/Lead/Email/show_phone.html.twig',
'subject' => $mailUtilities->getSubject(),
'from' => $mailUtilities->getFrom(),
'reply_to' => $mailUtilities->getReplyTo(),
'to' => $mailUtilities->getTo(),
'text_headers' => $mailUtilities->getTextHeaders(),
'data' => [
'listing' => $listing->getId(),
'user' => $user instanceof User ? $user->getId() : null,
'lead' => $lead->getId(),
'leads_insights_enabled' => $leadsInsightsEnabled,
'participant' => $participant->getId(),
],
],
]));
}
}
}
/**
* @return MailUtilitiesModel
*/
private function buildMailUtilities(MailUtilitiesModel $mailUtility)
{
$subject = ucfirst($this->translator->trans('phone_call_email.subject', [
'%user_name%' => $mailUtility->getSubject(),
], 'emails'));
// check if name start with user+Number change subject in email
if ('user+' == substr($mailUtility->getSubject(), self::USERNAME_FIRST_LETTER_INDEX, self::USERNAME_LAST_LETTER_INDEX)) {
$subject = ucfirst($this->translator->trans('phone_call_email.subject_without_name', [], 'emails'));
}
$from = [$this->parameterBag->get('website_email') => $this->translator->trans('layout.app_name')];
$textHeaders = ['X-Mail-Category' => 'listings-lead-show-phone', 'X-Site-Country' => $this->parameterBag->get('country')];
$mailUtility->setSubject($subject);
$mailUtility->setFrom($from);
$mailUtility->setTextHeaders($textHeaders);
return $mailUtility;
}
public static function getSubscribedEvents()
{
return [
'aqarmap.listing.message_submitted' => 'onContactSeller',
'aqarmap.listing.show_seller_number' => 'onShowPhoneNumber',
];
}
}