2018-06-25 21:56:32 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Repository;
|
|
|
|
|
|
|
|
use App\Entity\{ApiToken, User};
|
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
2018-06-26 22:28:42 +00:00
|
|
|
use Doctrine\ORM\Query\Expr\Join;
|
2018-06-25 21:56:32 +00:00
|
|
|
use Symfony\Bridge\Doctrine\RegistryInterface;
|
|
|
|
|
|
|
|
class ApiTokenRepository extends ServiceEntityRepository
|
|
|
|
{
|
|
|
|
public function __construct(RegistryInterface $registry)
|
|
|
|
{
|
|
|
|
parent::__construct($registry, ApiToken::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function add(ApiToken $token): void
|
|
|
|
{
|
|
|
|
$this->getEntityManager()->persist($token);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function findUserByTokenKey(string $tokenKey): ?User
|
|
|
|
{
|
2018-06-26 22:28:42 +00:00
|
|
|
$qb = $this->getEntityManager()->createQueryBuilder();
|
2018-06-25 21:56:32 +00:00
|
|
|
$qb
|
2018-06-26 22:28:42 +00:00
|
|
|
->select('u')
|
|
|
|
->from(User::class, 'u')
|
|
|
|
->innerJoin(ApiToken::class, 'at', Join::WITH, 'at.user = u')
|
2018-06-25 21:56:32 +00:00
|
|
|
->where('at.key = :tokenKey')
|
|
|
|
->setParameter('tokenKey', $tokenKey)
|
|
|
|
;
|
|
|
|
|
|
|
|
return $qb->getQuery()->getOneOrNullResult();
|
|
|
|
}
|
|
|
|
}
|