src/Aqarmap/Bundle/UserBundle/EventListener/UserInterestSubscribeListener.php line 100

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\UserBundle\EventListener;
  3. use Aqarmap\Bundle\BuyerAlertsBundle\Service\BuyerAlertsManager;
  4. use Aqarmap\Bundle\FeatureToggleBundle\Service\FeatureToggleManager;
  5. use Aqarmap\Bundle\ListingBundle\Event\LeadEvent;
  6. use Aqarmap\Bundle\ListingBundle\Service\LeadService;
  7. use Aqarmap\Bundle\UserBundle\Event\userInterestEvent;
  8. use Aqarmap\Bundle\UserBundle\Services\Contracts\UserInterestBuilderInterface;
  9. use Aqarmap\Bundle\UserBundle\Services\UserInterestManager;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class UserInterestSubscribeListener implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var EntityManagerInterface
  17.      */
  18.     protected $em;
  19.     /**
  20.      * @var FeatureToggleManager
  21.      */
  22.     protected $featureToggleManager;
  23.     /**
  24.      * @var LeadService
  25.      */
  26.     protected $leadService;
  27.     /**
  28.      * @var UserInterestManager
  29.      */
  30.     protected $userInterestManager;
  31.     /**
  32.      * @var UserInterestBuilderInterface
  33.      */
  34.     protected $userInterestBuilder;
  35.     /**
  36.      * @var LoggerInterface
  37.      */
  38.     protected $logger;
  39.     /** @var BuyerAlertsManager */
  40.     private $buyerAlertsManager;
  41.     public function __construct(
  42.         EntityManagerInterface $em,
  43.         UserInterestManager $userInterestManager,
  44.         FeatureToggleManager $featureToggleManager,
  45.         LeadService $leadService,
  46.         UserInterestBuilderInterface $userInterestBuilder,
  47.         LoggerInterface $logger,
  48.         BuyerAlertsManager $buyerAlertsManager
  49.     ) {
  50.         $this->em $em;
  51.         $this->userInterestManager $userInterestManager;
  52.         $this->featureToggleManager $featureToggleManager;
  53.         $this->leadService $leadService;
  54.         $this->userInterestBuilder $userInterestBuilder;
  55.         $this->logger $logger;
  56.         $this->buyerAlertsManager $buyerAlertsManager;
  57.     }
  58.     public function onAddInteraction(userInterestEvent $event): void
  59.     {
  60.         $userInterest $event->getUserInterest();
  61.         $this->em->persist($userInterest);
  62.         $this->em->flush($userInterest);
  63.     }
  64.     public function onAddLead(LeadEvent $event): void
  65.     {
  66.         $listing $event->getListing();
  67.         if ($this->featureToggleManager->isEnabled('web.client.served.count')) {
  68.             $this->leadService->publishUpdatingClientServedCount($listing->getUser()->getId());
  69.         }
  70.         try {
  71.             $this->buyerAlertsManager->addNewRecentSearch(
  72.                 $event->getUser(),
  73.                 $listing->getLocation(),
  74.                 $listing->getSection(),
  75.                 $listing->getPropertyType(),
  76.                 $listing->getPrice(),
  77.                 null,
  78.                 $listing->getArea(),
  79.                 null
  80.             );
  81.         } catch (\Exception $e) {
  82.             $this->logger->error('Search Listener: '.$e->getMessage());
  83.         }
  84.     }
  85.     public function onCreateInterest(userInterestEvent $event): void
  86.     {
  87.         $userInterestManager $this->userInterestManager;
  88.         /** @var $locationRepo \Aqarmap\Bundle\ListingBundle\Entity\LocationRepository */
  89.         $locationRepo $this->em->getRepository(\Aqarmap\Bundle\ListingBundle\Entity\Location::class);
  90.         if (!$event->getUserInterest()->getLocation()->getSearchable()
  91.         ) {
  92.             $location null;
  93.             $locations $locationRepo->getPath($event->getUserInterest()->getLocation());
  94.             arsort($locations);
  95.             foreach ($locations as $treeLocation) {
  96.                 if ($treeLocation->getSearchable()) {
  97.                     $location $treeLocation;
  98.                     break;
  99.                 }
  100.             }
  101.             if ($location) {
  102.                 if ($location != $event->getUserInterest()->getLocation()) {
  103.                     $userInterestManager->addNew(
  104.                         $event->getUserInterest()->getUser(),
  105.                         $location,
  106.                         $event->getUserInterest()->getSection(),
  107.                         $event->getUserInterest()->getPropertyType(),
  108.                         $event->getUserInterest()->getMaxPrice(),
  109.                         $event->getUserInterest()->getMinPrice(),
  110.                         true,
  111.                         false
  112.                     )
  113.                         ->commit();
  114.                 }
  115.             }
  116.         }
  117.     }
  118.     public static function getSubscribedEvents()
  119.     {
  120.         return [
  121.             'aqarmap.listing.message_submitted' => 'onAddLead',
  122.             'aqarmap.listing.call_requested' => 'onAddLead',
  123.             'aqarmap.listing.show_seller_number' => 'onAddLead',
  124.             'aqarmap.interest.create_non_searchable' => 'onCreateInterest',
  125.         ];
  126.     }
  127. }