src/Aqarmap/Bundle/ListingBundle/EventListener/LeadSystemLabelSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\ListingBundle\EventListener;
  3. use Aqarmap\Bundle\ListingBundle\Constant\ActiveUserLeadSearch;
  4. use Aqarmap\Bundle\ListingBundle\Constant\LeadQualityLabels;
  5. use Aqarmap\Bundle\ListingBundle\Event\LeadEvent;
  6. use Aqarmap\Bundle\UserBundle\Constant\UserTypes;
  7. use Aqarmap\Bundle\UserBundle\Repository\UserRepository;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class LeadSystemLabelSubscriber implements EventSubscriberInterface
  11. {
  12.     protected $userRepository;
  13.     protected $entityManager;
  14.     public function __construct(UserRepository $userRepositoryEntityManagerInterface $entityManager)
  15.     {
  16.         $this->userRepository $userRepository;
  17.         $this->entityManager $entityManager;
  18.     }
  19.     public function setBrokerFlag(LeadEvent $event): void
  20.     {
  21.         $lead $event->getLead();
  22.         if ($this->userRepository->hasActivePackages($event->getUser(), UserTypes::getBrokerChoices())) {
  23.             $lead->setSystemLabel(LeadQualityLabels::SYSTEM_BROKER);
  24.             $this->entityManager->persist($lead);
  25.             $this->entityManager->flush();
  26.         }
  27.     }
  28.     /**
  29.      * acceptance criteria
  30.      * 1- creator of a lead has 5 leads or more in the last 30 days from the lead creation date
  31.      * 2- must have no active packages
  32.      * 3- the user type must be private owner.
  33.      */
  34.     public function setActiveUserFlag(LeadEvent $event): void
  35.     {
  36.         $lead $event->getLead();
  37.         $user $event->getUser();
  38.         $isNotBroker $this->userRepository->hasActivePackages($userUserTypes::getBrokerChoices());
  39.         $validUserType UserTypes::INDIVIDUAL == $user->getUserType();
  40.         $exceedsActiveLeadsLimit $this->userRepository
  41.             ->countUserCreatedLeadsInDays($userActiveUserLeadSearch::NUMBER_OF_DAYS) >= ActiveUserLeadSearch::LEAD_COUNT;
  42.         if (
  43.             $isNotBroker
  44.             && $validUserType
  45.             && $exceedsActiveLeadsLimit
  46.         ) {
  47.             $lead->setSystemLabel(LeadQualityLabels::SYSTEM_ACTIVE);
  48.             $this->entityManager->persist($lead);
  49.             $this->entityManager->flush();
  50.         }
  51.     }
  52.     public static function getSubscribedEvents()
  53.     {
  54.         return [
  55.             'aqarmap.listing.lead.add' => [
  56.                 ['setActiveUserFlag'1],
  57.                 ['setBrokerFlag'],
  58.             ],
  59.         ];
  60.     }
  61. }