src/Aqarmap/Bundle/ListingBundle/EventListener/NotifierSubscribeListener.php line 64

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\ListingBundle\EventListener;
  3. use Aqarmap\Bundle\BuyerAlertsBundle\Service\BuyerAlertsManager;
  4. use Aqarmap\Bundle\ListingBundle\Entity\Location;
  5. use Aqarmap\Bundle\ListingBundle\Entity\PropertyType;
  6. use Aqarmap\Bundle\ListingBundle\Entity\Section;
  7. use Aqarmap\Bundle\ListingBundle\Event\LeadEvent;
  8. use Aqarmap\Bundle\ListingBundle\Event\SearchTriggerEvent;
  9. use Aqarmap\Bundle\ListingBundle\Event\SearchTriggerInterface;
  10. use Aqarmap\Bundle\ListingBundle\Repository\PropertyTypeRepository;
  11. use Aqarmap\Bundle\MainBundle\Contract\ProducerFactoryInterface;
  12. use Aqarmap\Bundle\NotifierBundle\Service\NotifierManager;
  13. use Aqarmap\Bundle\UserBundle\Entity\User;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Psr\Log\LoggerInterface;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  19. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  20. class NotifierSubscribeListener implements EventSubscriberInterface
  21. {
  22.     /**
  23.      * @var EntityManagerInterface
  24.      */
  25.     protected $em;
  26.     protected $notifierManager;
  27.     /** @var UserInterface */
  28.     protected $user;
  29.     /** @var LoggerInterface */
  30.     private $logger;
  31.     /** @var BuyerAlertsManager */
  32.     private $buyerAlertsManager;
  33.     /**
  34.      * @var PropertyTypeRepository
  35.      */
  36.     private $propertyTypeRepository;
  37.     public function __construct(
  38.         EntityManagerInterface $em,
  39.         NotifierManager $notifierManager,
  40.         TokenStorageInterface $tokenStorage,
  41.         SessionInterface $session,
  42.         LoggerInterface $logger,
  43.         ProducerFactoryInterface $producerFactory,
  44.         BuyerAlertsManager $buyerAlertsManager,
  45.         PropertyTypeRepository $propertyTypeRepository
  46.     ) {
  47.         $this->em $em;
  48.         $this->notifierManager $notifierManager;
  49.         $this->user $tokenStorage->getToken() ? $tokenStorage->getToken()->getUser() : null;
  50.         $this->logger $logger;
  51.         $this->buyerAlertsManager $buyerAlertsManager;
  52.         $this->propertyTypeRepository $propertyTypeRepository;
  53.     }
  54.     public function onAddLead(LeadEvent $event): void
  55.     {
  56.         $listing $event->getListing();
  57.         $this->buyerAlertsManager->addNewRecentSearch(
  58.             $event->getUser(),
  59.             $listing->getLocation(),
  60.             $listing->getSection(),
  61.             $listing->getPropertyType(),
  62.             $listing->getPrice() ?: null,
  63.             null,
  64.             $listing->getArea() ?: null,
  65.             null
  66.         );
  67.     }
  68.     /**
  69.      * Update Notifier.
  70.      */
  71.     public function updateNotifier(SearchTriggerInterface $event): void
  72.     {
  73.         $item $this->prepareData($event->getRequest());
  74.         if ($this->user instanceof User && $item) {
  75.             try {
  76.                 $this->buyerAlertsManager->addNewRecentSearch(
  77.                     $this->user,
  78.                     $item['location'],
  79.                     $item['section'],
  80.                     $item['propertyType'],
  81.                     $item['maxPrice'],
  82.                     $item['minPrice']
  83.                 );
  84.             } catch (\Exception $e) {
  85.                 if ($this->user && $this->user->hasRole('ROLE_ADMIN')) {
  86.                     $this->logger->error('Search Listener: '.$e->getMessage());
  87.                 }
  88.             }
  89.         }
  90.     }
  91.     /**
  92.      * Prepares notifier data.
  93.      *
  94.      * @return array
  95.      */
  96.     private function prepareData(Request $request)
  97.     {
  98.         $location $request->attributes->get('location') ?? (int) current(
  99.             explode(',', (string) $request->query->get('location'))
  100.         );
  101.         if (!$location) {
  102.             return;
  103.         }
  104.         $locationEntity = (
  105.             $location instanceof Location
  106.         )
  107.             ? $location
  108.             $this->em->getReference('AqarmapListingBundle:Location'$location);
  109.         $sectionEntity null;
  110.         $propertyTypeEntity null;
  111.         if ($sectionId $request->query->get('section')) {
  112.             $sectionEntity $this->em->getReference(Section::class, $sectionId);
  113.         }
  114.         if ($propertyTypeId $request->query->get('propertyType')) {
  115.             $propertyTypeEntity $this->em->getReference(PropertyType::class, $propertyTypeId);
  116.         }
  117.         if (null === $request->query->get('propertyType')) {
  118.             $propertyTypeEntity $this->propertyTypeRepository
  119.                 ->getRootNodesQuery()
  120.                 ->getOneOrNullResult();
  121.         }
  122.         return [
  123.             'location' => $locationEntity,
  124.             'section' => $sectionEntity ?? $request->attributes->get('section'),
  125.             'propertyType' => $propertyTypeEntity ?? $request->attributes->get('propertyType'),
  126.             'maxPrice' => (int) $request->query->get('maxPrice'),
  127.             'minPrice' => (int) $request->query->get('minPrice'),
  128.         ];
  129.     }
  130.     public static function getSubscribedEvents()
  131.     {
  132.         return [
  133.             'aqarmap.listing.message_submitted' => 'onAddLead',
  134.             'aqarmap.listing.call_requested' => 'onAddLead',
  135.             'aqarmap.listing.show_seller_number' => 'onAddLead',
  136.             SearchTriggerEvent::EVENT_NAME => 'updateNotifier',
  137.         ];
  138.     }
  139. }