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 Feb 1, 2018
1 parent dd22826 commit 208110e
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 80 deletions.
13 changes: 1 addition & 12 deletions Document/ClientManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace FOS\OAuthServerBundle\Document;

use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\DocumentRepository;
use FOS\OAuthServerBundle\Model\ClientInterface;
use FOS\OAuthServerBundle\Model\ClientManager as BaseClientManager;

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

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

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

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

$this->dm = $dm;
$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->dm->getRepository($this->class)->findOneBy($criteria);
}

/**
Expand Down
18 changes: 5 additions & 13 deletions Document/TokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,14 @@ class TokenManager extends BaseTokenManager
*/
protected $dm;

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

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

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

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

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

/**
Expand All @@ -85,8 +75,10 @@ public function deleteToken(TokenInterface $token)
*/
public function deleteExpired()
{
$result = $this
->repository
// NOTE: bug in Doctrine, hinting DocumentRepository|ObjectRepository when only DocumentRepository is expected
/** @var DocumentRepository $repository */
$repository = $this->dm->getRepository($this->class);
$result = $repository
->createQueryBuilder()
->remove()
->field('expiresAt')->lt(time())
Expand Down
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
17 changes: 5 additions & 12 deletions Entity/TokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,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 +49,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 +75,10 @@ public function deleteToken(TokenInterface $token)
*/
public function deleteExpired()
{
$qb = $this->repository->createQueryBuilder('t');
// NOTE: bug in Doctrine, hinting EntityRepository|ObjectRepository when only EntityRepository is expected
/** @var EntityRepository $repository */
$repository = $this->em->getRepository($this->class);
$qb = $repository->createQueryBuilder('t');
$qb
->delete()
->where('t.expiresAt < ?1')
Expand Down
15 changes: 7 additions & 8 deletions Tests/Document/ClientManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,6 @@ public function setUp()
;
$this->className = 'RandomClassName'.\random_bytes(5);

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

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

parent::setUp();
Expand All @@ -76,7 +69,6 @@ public function setUp()
public function testConstructWillSetParameters()
{
$this->assertAttributeSame($this->documentManager, 'dm', $this->instance);
$this->assertAttributeSame($this->repository, 'repository', $this->instance);
$this->assertAttributeSame($this->className, 'class', $this->instance);
}

Expand All @@ -92,6 +84,13 @@ public function testFindClientBy()
\random_bytes(5),
];

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

$this->repository
->expects($this->once())
->method('findOneBy')
Expand Down
21 changes: 14 additions & 7 deletions Tests/Document/TokenManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,6 @@ public function setUp()
->getMock()
;

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

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

Expand All @@ -80,6 +73,13 @@ public function testFindTokenByToken()
$randomToken = \random_bytes(5);
$randomResult = \random_bytes(5);

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

$this->repository
->expects($this->once())
->method('findOneBy')
Expand Down Expand Up @@ -145,6 +145,13 @@ public function testDeleteToken()

public function testDeleteExpired()
{
$this->documentManager
->expects($this->once())
->method('getRepository')
->with($this->className)
->willReturn($this->repository)
;

$queryBuilder = $this->getMockBuilder(Builder::class)
->disableOriginalConstructor()
->getMock()
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 208110e

Please sign in to comment.