Skip to content

Instantly share code, notes, and snippets.

@elnur
Created March 10, 2013 20:09
Show Gist options
  • Save elnur/5130199 to your computer and use it in GitHub Desktop.
Save elnur/5130199 to your computer and use it in GitHub Desktop.
<?php
namespace Elnur\Listener;
use DateTime;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Core\SecurityContextInterface;
use JMS\DiExtraBundle\Annotation\Service;
use JMS\DiExtraBundle\Annotation\InjectParams;
use JMS\DiExtraBundle\Annotation\Observe;
use Elnur\Manager\UserManager;
/**
* @Service
*/
class VisitListener
{
/**
* @var SecurityContextInterface
*/
private $securityContext;
/**
* @var UserManager
*/
private $userManager;
/**
* @InjectParams
*
* @param SecurityContextInterface $securityContext
* @param UserManager $userManager
*/
public function __construct(SecurityContextInterface $securityContext, UserManager $userManager)
{
$this->securityContext = $securityContext;
$this->userManager = $userManager;
}
/**
* @Observe("kernel.request")
*/
public function onRequest(GetResponseEvent $event)
{
if ($event->getRequestType() !== HttpKernelInterface::MASTER_REQUEST) {
return;
}
$token = $this->securityContext->getToken();
if (null === $token || !$this->securityContext->isGranted('ROLE_USER')) {
return;
}
$user = $token->getUser();
$user->setLastVisitAt(new DateTime);
$this->userManager->save($user);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment