<?php
namespace Aqarmap\Bundle\ListingBundle\EventListener;
use Aqarmap\Bundle\ListingBundle\Constant\ActiveUserLeadSearch;
use Aqarmap\Bundle\ListingBundle\Constant\LeadQualityLabels;
use Aqarmap\Bundle\ListingBundle\Event\LeadEvent;
use Aqarmap\Bundle\UserBundle\Constant\UserTypes;
use Aqarmap\Bundle\UserBundle\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class LeadSystemLabelSubscriber implements EventSubscriberInterface
{
protected $userRepository;
protected $entityManager;
public function __construct(UserRepository $userRepository, EntityManagerInterface $entityManager)
{
$this->userRepository = $userRepository;
$this->entityManager = $entityManager;
}
public function setBrokerFlag(LeadEvent $event): void
{
$lead = $event->getLead();
if ($this->userRepository->hasActivePackages($event->getUser(), UserTypes::getBrokerChoices())) {
$lead->setSystemLabel(LeadQualityLabels::SYSTEM_BROKER);
$this->entityManager->persist($lead);
$this->entityManager->flush();
}
}
/**
* acceptance criteria
* 1- creator of a lead has 5 leads or more in the last 30 days from the lead creation date
* 2- must have no active packages
* 3- the user type must be private owner.
*/
public function setActiveUserFlag(LeadEvent $event): void
{
$lead = $event->getLead();
$user = $event->getUser();
$isNotBroker = $this->userRepository->hasActivePackages($user, UserTypes::getBrokerChoices());
$validUserType = UserTypes::INDIVIDUAL == $user->getUserType();
$exceedsActiveLeadsLimit = $this->userRepository
->countUserCreatedLeadsInDays($user, ActiveUserLeadSearch::NUMBER_OF_DAYS) >= ActiveUserLeadSearch::LEAD_COUNT;
if (
$isNotBroker
&& $validUserType
&& $exceedsActiveLeadsLimit
) {
$lead->setSystemLabel(LeadQualityLabels::SYSTEM_ACTIVE);
$this->entityManager->persist($lead);
$this->entityManager->flush();
}
}
public static function getSubscribedEvents()
{
return [
'aqarmap.listing.lead.add' => [
['setActiveUserFlag', 1],
['setBrokerFlag'],
],
];
}
}