<?php
declare(strict_types=1);
/**
* @author Maciej Kaczmarek <maciej.kaczmarek@autentika.pl>
*/
namespace Hitso\Bundle\CommonBundle\EventListener;
use Doctrine\ORM\EntityManagerInterface;
use Hitso\Bundle\CommonBundle\Entity\VisitLog;
use Hitso\Bundle\CommonBundle\Repository\VisitLogRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class GoogleListener implements EventSubscriberInterface
{
/**
* @var EntityManagerInterface
*/
private $em;
/**
* GoogleListener constructor.
*
* @param EntityManagerInterface $em
*/
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public function onRequest(GetResponseEvent $event)
{
if ($event->isMasterRequest()) {
$request = $event->getRequest();
$agent = $request->headers->get('User-Agent');
if ($agent && preg_match('/googlebot/i', $agent)) {
/** @var VisitLogRepository $visits */
$visits = $this->em->getRepository(VisitLog::class);
$visits->logVisit(VisitLog::TYPE_GOOGLE);
}
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => 'onRequest',
];
}
}