src/Aqarmap/Bundle/ListingBundle/EventListener/NotifierSubscribeListener.php line 64
<?phpnamespace Aqarmap\Bundle\ListingBundle\EventListener;use Aqarmap\Bundle\BuyerAlertsBundle\Service\BuyerAlertsManager;use Aqarmap\Bundle\ListingBundle\Entity\Location;use Aqarmap\Bundle\ListingBundle\Entity\PropertyType;use Aqarmap\Bundle\ListingBundle\Entity\Section;use Aqarmap\Bundle\ListingBundle\Event\LeadEvent;use Aqarmap\Bundle\ListingBundle\Event\SearchTriggerEvent;use Aqarmap\Bundle\ListingBundle\Event\SearchTriggerInterface;use Aqarmap\Bundle\ListingBundle\Repository\PropertyTypeRepository;use Aqarmap\Bundle\NotifierBundle\Service\NotifierManager;use Aqarmap\Bundle\UserBundle\Entity\User;use Doctrine\ORM\EntityManagerInterface;use Psr\Log\LoggerInterface;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;class NotifierSubscribeListener implements EventSubscriberInterface{/*** @var EntityManagerInterface*/protected $em;protected $notifierManager;/** @var UserInterface */protected $user;public function __construct(EntityManagerInterface $em,NotifierManager $notifierManager,TokenStorageInterface $tokenStorage,private readonly LoggerInterface $logger,private readonly BuyerAlertsManager $buyerAlertsManager,private readonly PropertyTypeRepository $propertyTypeRepository,) {$this->em = $em;$this->notifierManager = $notifierManager;$this->user = $tokenStorage->getToken() ? $tokenStorage->getToken()->getUser() : null;}public function onAddLead(LeadEvent $event): void{$listing = $event->getListing();$this->buyerAlertsManager->addNewRecentSearch($event->getUser(),$listing->getLocation(),$listing->getSection(),$listing->getPropertyType(),$listing->getPrice() ?: null,null,$listing->getArea() ?: null,null);}/*** Update Notifier.*/public function updateNotifier(SearchTriggerInterface $event): void{$item = $this->prepareData($event->getRequest());if ($this->user instanceof User && $item) {try {$this->buyerAlertsManager->addNewRecentSearch($this->user,$item['location'],$item['section'],$item['propertyType'],$item['maxPrice'],$item['minPrice']);} catch (\Exception $e) {if ($this->user && $this->user->hasRole('ROLE_ADMIN')) {$this->logger->error('Search Listener: '.$e->getMessage());}}}}/*** Prepares notifier data.** @return array*/private function prepareData(Request $request){$location = $request->attributes->get('location') ?? (int) current(explode(',', (string) $request->query->get('location')));if (!$location) {return;}$locationEntity = ($location instanceof Location)? $location: $this->em->getReference(Location::class, $location);$sectionEntity = null;$propertyTypeEntity = null;if ($sectionId = $request->query->get('section')) {$sectionEntity = $this->em->getReference(Section::class, $sectionId);}if ($propertyTypeId = $request->query->get('propertyType')) {$propertyTypeEntity = $this->em->getReference(PropertyType::class, $propertyTypeId);}if (null === $request->query->get('propertyType')) {$propertyTypeEntity = $this->propertyTypeRepository->getRootNodesQuery()->getOneOrNullResult();}return ['location' => $locationEntity,'section' => $sectionEntity ?? $request->attributes->get('section'),'propertyType' => $propertyTypeEntity ?? $request->attributes->get('propertyType'),'maxPrice' => (int) $request->query->get('maxPrice'),'minPrice' => (int) $request->query->get('minPrice'),];}public static function getSubscribedEvents(): array{return ['aqarmap.listing.message_submitted' => 'onAddLead','aqarmap.listing.call_requested' => 'onAddLead','aqarmap.listing.show_seller_number' => 'onAddLead',SearchTriggerEvent::EVENT_NAME => 'updateNotifier',];}}