Skip to content

Commit

Permalink
Repository for ClientManager and TokenManager is now instanciated on …
Browse files Browse the repository at this point in the history
…the fly, not in the constructor anymore

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 25b47fa
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 41 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
15 changes: 7 additions & 8 deletions Tests/Entity/ClientManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,6 @@ public function setUp()
;
$this->className = 'RandomClassName'.\random_bytes(5);

$this->entityManager
->expects($this->once())
->method('getRepository')
->with($this->className)
->willReturn($this->repository)
;

$this->instance = new ClientManager($this->entityManager, $this->className);

parent::setUp();
Expand All @@ -72,7 +65,6 @@ public function setUp()
public function testConstructWillSetParameters()
{
$this->assertAttributeSame($this->entityManager, 'em', $this->instance);
$this->assertAttributeSame($this->repository, 'repository', $this->instance);
$this->assertAttributeSame($this->className, 'class', $this->instance);
}

Expand All @@ -88,6 +80,13 @@ public function testFindClientBy()
];
$randomResult = \random_bytes(5);

$this->entityManager
->expects($this->once())
->method('getRepository')
->with($this->className)
->willReturn($this->repository)
;

$this->repository
->expects($this->once())
->method('findOneBy')
Expand Down
22 changes: 14 additions & 8 deletions Tests/Entity/TokenManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,12 @@ public function setUp()
->getMock()
;

$this->entityManager
->expects($this->once())
->method('getRepository')
->with($this->className)
->willReturn($this->repository)
;

$this->instance = new TokenManager($this->entityManager, $this->className);
}

public function testConstructWillSetParameters()
{
$this->assertAttributeSame($this->entityManager, 'em', $this->instance);
$this->assertAttributeSame($this->repository, 'repository', $this->instance);
$this->assertAttributeSame($this->className, 'class', $this->instance);
}

Expand Down Expand Up @@ -112,6 +104,13 @@ public function testFindTokenBy()
\random_bytes(5),
];

$this->entityManager
->expects($this->once())
->method('getRepository')
->with($this->className)
->willReturn($this->repository)
;

$this->repository
->expects($this->once())
->method('findOneBy')
Expand Down Expand Up @@ -174,6 +173,13 @@ public function testDeleteExpired()
{
$randomResult = \random_bytes(10);

$this->entityManager
->expects($this->once())
->method('getRepository')
->with($this->className)
->willReturn($this->repository)
;

$queryBuilder = $this->getMockBuilder(QueryBuilder::class)
->disableOriginalConstructor()
->getMock()
Expand Down

0 comments on commit 25b47fa

Please sign in to comment.