<?php
namespace Aqarmap\Bundle\ListingBundle\EventListener;
use Aqarmap\Bundle\ListingBundle\Event\LeadEvent;
use Aqarmap\Bundle\ListingBundle\Repository\ListingLeadRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class LeadDuplicateSubscriber implements EventSubscriberInterface
{
protected $listingLeadRepository;
protected $entityManager;
public function __construct(ListingLeadRepository $listingLeadRepository, EntityManagerInterface $entityManager)
{
$this->listingLeadRepository = $listingLeadRepository;
$this->entityManager = $entityManager;
}
public function handleDuplicateLead(LeadEvent $event): void
{
$lead = $event->getLead();
$criteria = [
'user' => $event->getUser(),
'listing' => $lead->getListing(),
];
$lead->setIsDuplicate($this->listingLeadRepository->isDuplicated($criteria));
$this->entityManager->persist($lead);
$this->entityManager->flush();
}
public static function getSubscribedEvents()
{
return [
'aqarmap.listing.lead.add' => [
['handleDuplicateLead'],
],
];
}
}