Skip to content

Commit

Permalink
Onthefly repo for ClientManager and TokenManager
Browse files Browse the repository at this point in the history
This avoids a database connection to be established for every request
Fixes issue #422
  • Loading branch information
abienvenu committed Jan 31, 2018
1 parent dd22826 commit c678667
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 25 deletions.
13 changes: 1 addition & 12 deletions Entity/ClientManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace FOS\OAuthServerBundle\Entity;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use FOS\OAuthServerBundle\Model\ClientInterface;
use FOS\OAuthServerBundle\Model\ClientManager as BaseClientManager;

Expand All @@ -25,24 +24,14 @@ class ClientManager extends BaseClientManager
*/
protected $em;

/**
* @var EntityRepository
*/
protected $repository;

/**
* @var string
*/
protected $class;

public function __construct(EntityManagerInterface $em, $class)
{
// NOTE: bug in Doctrine, hinting EntityRepository|ObjectRepository when only EntityRepository is expected
/** @var EntityRepository $repository */
$repository = $em->getRepository($class);

$this->em = $em;
$this->repository = $repository;
$this->class = $class;
}

Expand All @@ -59,7 +48,7 @@ public function getClass()
*/
public function findClientBy(array $criteria)
{
return $this->repository->findOneBy($criteria);
return $this->em->getRepository($this->class)->findOneBy($criteria);
}

/**
Expand Down
15 changes: 2 additions & 13 deletions Entity/TokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace FOS\OAuthServerBundle\Entity;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use FOS\OAuthServerBundle\Model\TokenInterface;
use FOS\OAuthServerBundle\Model\TokenManager as BaseTokenManager;

Expand All @@ -25,24 +24,14 @@ class TokenManager extends BaseTokenManager
*/
protected $em;

/**
* @var EntityRepository
*/
protected $repository;

/**
* @var string
*/
protected $class;

public function __construct(EntityManagerInterface $em, $class)
{
// NOTE: bug in Doctrine, hinting EntityRepository|ObjectRepository when only EntityRepository is expected
/** @var EntityRepository $repository */
$repository = $em->getRepository($class);

$this->em = $em;
$this->repository = $repository;
$this->class = $class;
}

Expand All @@ -59,7 +48,7 @@ public function getClass()
*/
public function findTokenBy(array $criteria)
{
return $this->repository->findOneBy($criteria);
return $this->em->getRepository($this->class)->findOneBy($criteria);
}

/**
Expand All @@ -85,7 +74,7 @@ public function deleteToken(TokenInterface $token)
*/
public function deleteExpired()
{
$qb = $this->repository->createQueryBuilder('t');
$qb = $this->em->getRepository($this->class)->createQueryBuilder('t');
$qb
->delete()
->where('t.expiresAt < ?1')
Expand Down

0 comments on commit c678667

Please sign in to comment.