Skip to content

Instantly share code, notes, and snippets.

@migueleliasweb
Created April 1, 2014 13:50
Show Gist options
  • Save migueleliasweb/9914362 to your computer and use it in GitHub Desktop.
Save migueleliasweb/9914362 to your computer and use it in GitHub Desktop.
Inner join ='(
<?php
namespace ChefsClub\ApiBundle\Entity;
use ChefsClub\ApiBundle\Security\Exception\ActiveTokenNotFoundException;
use Doctrine\ORM\EntityRepository;
use Exception;
/**
* TokenRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class TokenRepository extends EntityRepository
{
/**
* @return Token Returns the active token for the user, if any
* @throws ActiveTokenNotFound
*/
public function getActiveTokenFromUser(User $User)
{
$Token = $this->createQueryBuilder('T')
->innerJoin('T.User', 'U')
->where('U = :UserParam')->setParameter('UserParam', $User->getUserid())
->andWhere('T.expirationDate < :expirationDate')->setParameter('expirationDate', new \DateTime())
->getQuery()
->getSingleResult();
if ($Token instanceof Token) {
return $Token;
} else {
throw new ActiveTokenNotFoundException();
}
}
/**
* @param User $User A user instance
* @return boolean Checks whether the User has a active token availble
*/
public function hasActiveToken(User $User)
{
try {
return $this->getActiveTokenFromUser($User) instanceof Token;
} catch (Exception $E) {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment