Created
March 10, 2013 20:09
-
-
Save elnur/5130199 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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