<?php
namespace Aqarmap\Bundle\MainBundle\EventListener;
use Gedmo\IpTraceable\IpTraceableListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* IpTraceSubscriber.
*/
class IpTraceSubscriber implements EventSubscriberInterface
{
private IpTraceableListener $ipTraceableListener;
public function __construct(IpTraceableListener $ipTraceableListener)
{
$this->ipTraceableListener = $ipTraceableListener;
}
public function onKernelRequest(RequestEvent $event): void
{
if (null === $event->getRequest()) {
return;
}
// If you use a cache like Varnish, you may want to set a proxy to Request::getClientIp() method
// $this->request->setTrustedProxies(array('127.0.0.1'));
$ip = $event->getRequest()->getClientIp();
if (null !== $ip) {
$this->ipTraceableListener->setIpValue($ip);
}
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
];
}
}