<?php
namespace Aqarmap\Bundle\ListingBundle\EventListener;
use Aqarmap\Bundle\ListingBundle\Event\SearchTriggerEvent;
use Aqarmap\Bundle\ListingBundle\Event\SearchTriggerInterface;
use Aqarmap\Bundle\MainBundle\Contract\ProducerFactoryInterface;
use Aqarmap\Bundle\UserBundle\Entity\User;
use FOS\UserBundle\Event\UserEvent;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* Listing Search Trigger Listener.
*/
class ListingSearchTriggerListener implements EventSubscriberInterface
{
/** @var UserInterface */
protected $user;
/** @var TokenStorageInterface */
private $tokenStorage;
/** @var EventDispatcherInterface */
protected $dispatcher;
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
public function __construct(
LoggerInterface $logger,
EventDispatcherInterface $eventDispatcher,
TokenStorageInterface $tokenStorage,
ProducerFactoryInterface $producerFactory,
SessionInterface $session
) {
$this->dispatcher = $tokenStorage;
$this->tokenStorage = $tokenStorage;
$this->user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
$this->eventDispatcher = $eventDispatcher;
}
public function listingSearchEvent(SearchTriggerInterface $event): void
{
$this->updateUserImperssion();
}
/**
* Update User Impression.
*/
protected function updateUserImperssion(): void
{
if ($this->user instanceof User) {
try {
$this->eventDispatcher
->dispatch(new UserEvent($this->user), 'aqarmap.listing.user.search');
} catch (\Exception $e) {
/** @var UserInterface $this- >user */
if ($this->user && $this->user->hasRole('ROLE_ADMIN')) {
$this->logger->error('Search Listener: '.$e->getMessage());
}
}
}
}
public static function getSubscribedEvents()
{
return [
SearchTriggerEvent::EVENT_NAME => 'listingSearchEvent',
];
}
}