<?php
namespace Aqarmap\Bundle\UserBundle\EventListener;
use Aqarmap\Bundle\BuyerAlertsBundle\Service\BuyerAlertsManager;
use Aqarmap\Bundle\FeatureToggleBundle\Service\FeatureToggleManager;
use Aqarmap\Bundle\ListingBundle\Event\LeadEvent;
use Aqarmap\Bundle\ListingBundle\Service\LeadService;
use Aqarmap\Bundle\UserBundle\Event\userInterestEvent;
use Aqarmap\Bundle\UserBundle\Services\Contracts\UserInterestBuilderInterface;
use Aqarmap\Bundle\UserBundle\Services\UserInterestManager;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class UserInterestSubscribeListener implements EventSubscriberInterface
{
/**
* @var EntityManagerInterface
*/
protected $em;
/**
* @var FeatureToggleManager
*/
protected $featureToggleManager;
/**
* @var LeadService
*/
protected $leadService;
/**
* @var UserInterestManager
*/
protected $userInterestManager;
/**
* @var UserInterestBuilderInterface
*/
protected $userInterestBuilder;
/**
* @var LoggerInterface
*/
protected $logger;
/** @var BuyerAlertsManager */
private $buyerAlertsManager;
public function __construct(
EntityManagerInterface $em,
UserInterestManager $userInterestManager,
FeatureToggleManager $featureToggleManager,
LeadService $leadService,
UserInterestBuilderInterface $userInterestBuilder,
LoggerInterface $logger,
BuyerAlertsManager $buyerAlertsManager
) {
$this->em = $em;
$this->userInterestManager = $userInterestManager;
$this->featureToggleManager = $featureToggleManager;
$this->leadService = $leadService;
$this->userInterestBuilder = $userInterestBuilder;
$this->logger = $logger;
$this->buyerAlertsManager = $buyerAlertsManager;
}
public function onAddInteraction(userInterestEvent $event): void
{
$userInterest = $event->getUserInterest();
$this->em->persist($userInterest);
$this->em->flush($userInterest);
}
public function onAddLead(LeadEvent $event): void
{
$listing = $event->getListing();
if ($this->featureToggleManager->isEnabled('web.client.served.count')) {
$this->leadService->publishUpdatingClientServedCount($listing->getUser()->getId());
}
try {
$this->buyerAlertsManager->addNewRecentSearch(
$event->getUser(),
$listing->getLocation(),
$listing->getSection(),
$listing->getPropertyType(),
$listing->getPrice(),
null,
$listing->getArea(),
null
);
} catch (\Exception $e) {
$this->logger->error('Search Listener: '.$e->getMessage());
}
}
public function onCreateInterest(userInterestEvent $event): void
{
$userInterestManager = $this->userInterestManager;
/** @var $locationRepo \Aqarmap\Bundle\ListingBundle\Entity\LocationRepository */
$locationRepo = $this->em->getRepository(\Aqarmap\Bundle\ListingBundle\Entity\Location::class);
if (!$event->getUserInterest()->getLocation()->getSearchable()
) {
$location = null;
$locations = $locationRepo->getPath($event->getUserInterest()->getLocation());
arsort($locations);
foreach ($locations as $treeLocation) {
if ($treeLocation->getSearchable()) {
$location = $treeLocation;
break;
}
}
if ($location) {
if ($location != $event->getUserInterest()->getLocation()) {
$userInterestManager->addNew(
$event->getUserInterest()->getUser(),
$location,
$event->getUserInterest()->getSection(),
$event->getUserInterest()->getPropertyType(),
$event->getUserInterest()->getMaxPrice(),
$event->getUserInterest()->getMinPrice(),
true,
false
)
->commit();
}
}
}
}
public static function getSubscribedEvents()
{
return [
'aqarmap.listing.message_submitted' => 'onAddLead',
'aqarmap.listing.call_requested' => 'onAddLead',
'aqarmap.listing.show_seller_number' => 'onAddLead',
'aqarmap.interest.create_non_searchable' => 'onCreateInterest',
];
}
}