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

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