<?php
namespace Aqarmap\Bundle\CRMBundle\EventListener;
use App\Exception\LogicHttpException;
use Aqarmap\Bundle\FeatureToggleBundle\Service\FeatureToggleManager;
use Aqarmap\Bundle\ListingBundle\Event\LeadEvent;
use Aqarmap\Bundle\ListingBundle\Service\LeadsCRMService;
use Aqarmap\Bundle\NotificationBundle\Types\LeadGenerated;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CRMLeadListener implements EventSubscriberInterface
{
/**
* @var EntityManagerInterface
*/
protected $em;
/**
* @var EventDispatcherInterface
*/
protected $dispatcher;
/**
* @var FeatureToggleManager
*/
protected $featuresToggleManager;
/**
* @var LeadsCRMService
*/
private $leadsCRMService;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var LeadGenerated
*/
private $leadGenerated;
public function __construct(
EntityManagerInterface $em,
EventDispatcherInterface $dispatcher,
FeatureToggleManager $featuresToggleManager,
LeadsCRMService $leadsCRMService,
LoggerInterface $logger,
LeadGenerated $leadGenerated
) {
$this->em = $em;
$this->dispatcher = $dispatcher;
$this->featuresToggleManager = $featuresToggleManager;
$this->leadsCRMService = $leadsCRMService;
$this->logger = $logger;
$this->leadGenerated = $leadGenerated;
}
public function onAddLead(LeadEvent $event): void
{
$listing = $event->getListing();
if ($this->featuresToggleManager->isEnabled('web.leads.crm.sync')) {
try {
$this->leadsCRMService->sendLead($listing, $event->getLead());
} catch (LogicHttpException $e) {
$this->logger->info(sprintf('CRM Lead Listener: %s', $e->getMessage()));
} catch (\Exception $e) {
$this->logger->error(sprintf('CRM Lead Listener: %s', $e->getMessage()));
}
}
if ($this->featuresToggleManager->isEnabled('web.leads.generate.mail')) {
$this->dispatcher->dispatch($this->leadGenerated->setSubject($event->getLead()), 'lead.generated');
}
}
public static function getSubscribedEvents()
{
return [
'aqarmap.listing.lead.add' => ['onAddLead'],
];
}
}