Created
April 1, 2014 13:50
-
-
Save migueleliasweb/9914362 to your computer and use it in GitHub Desktop.
Inner join ='(
This file contains hidden or 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 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