Skip to content

Commit 208110e

Browse files
committed
Onthefly repo for ClientManager and TokenManager
This avoids a database connection to be established for every request Fixes issue #422
1 parent dd22826 commit 208110e

8 files changed

+54
-80
lines changed

Document/ClientManager.php

+1-12
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace FOS\OAuthServerBundle\Document;
1515

1616
use Doctrine\ODM\MongoDB\DocumentManager;
17-
use Doctrine\ODM\MongoDB\DocumentRepository;
1817
use FOS\OAuthServerBundle\Model\ClientInterface;
1918
use FOS\OAuthServerBundle\Model\ClientManager as BaseClientManager;
2019

@@ -25,24 +24,14 @@ class ClientManager extends BaseClientManager
2524
*/
2625
protected $dm;
2726

28-
/**
29-
* @var DocumentRepository
30-
*/
31-
protected $repository;
32-
3327
/**
3428
* @var string
3529
*/
3630
protected $class;
3731

3832
public function __construct(DocumentManager $dm, $class)
3933
{
40-
// NOTE: bug in Doctrine, hinting DocumentRepository|ObjectRepository when only DocumentRepository is expected
41-
/** @var DocumentRepository $repository */
42-
$repository = $dm->getRepository($class);
43-
4434
$this->dm = $dm;
45-
$this->repository = $repository;
4635
$this->class = $class;
4736
}
4837

@@ -59,7 +48,7 @@ public function getClass()
5948
*/
6049
public function findClientBy(array $criteria)
6150
{
62-
return $this->repository->findOneBy($criteria);
51+
return $this->dm->getRepository($this->class)->findOneBy($criteria);
6352
}
6453

6554
/**

Document/TokenManager.php

+5-13
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,14 @@ class TokenManager extends BaseTokenManager
2525
*/
2626
protected $dm;
2727

28-
/**
29-
* @var DocumentRepository
30-
*/
31-
protected $repository;
32-
3328
/**
3429
* @var string
3530
*/
3631
protected $class;
3732

3833
public function __construct(DocumentManager $dm, $class)
3934
{
40-
// NOTE: bug in Doctrine, hinting DocumentRepository|ObjectRepository when only DocumentRepository is expected
41-
/** @var DocumentRepository $repository */
42-
$repository = $dm->getRepository($class);
43-
4435
$this->dm = $dm;
45-
$this->repository = $repository;
4636
$this->class = $class;
4737
}
4838

@@ -59,7 +49,7 @@ public function getClass()
5949
*/
6050
public function findTokenBy(array $criteria)
6151
{
62-
return $this->repository->findOneBy($criteria);
52+
return $this->dm->getRepository($this->class)->findOneBy($criteria);
6353
}
6454

6555
/**
@@ -85,8 +75,10 @@ public function deleteToken(TokenInterface $token)
8575
*/
8676
public function deleteExpired()
8777
{
88-
$result = $this
89-
->repository
78+
// NOTE: bug in Doctrine, hinting DocumentRepository|ObjectRepository when only DocumentRepository is expected
79+
/** @var DocumentRepository $repository */
80+
$repository = $this->dm->getRepository($this->class);
81+
$result = $repository
9082
->createQueryBuilder()
9183
->remove()
9284
->field('expiresAt')->lt(time())

Entity/ClientManager.php

+1-12
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace FOS\OAuthServerBundle\Entity;
1515

1616
use Doctrine\ORM\EntityManagerInterface;
17-
use Doctrine\ORM\EntityRepository;
1817
use FOS\OAuthServerBundle\Model\ClientInterface;
1918
use FOS\OAuthServerBundle\Model\ClientManager as BaseClientManager;
2019

@@ -25,24 +24,14 @@ class ClientManager extends BaseClientManager
2524
*/
2625
protected $em;
2726

28-
/**
29-
* @var EntityRepository
30-
*/
31-
protected $repository;
32-
3327
/**
3428
* @var string
3529
*/
3630
protected $class;
3731

3832
public function __construct(EntityManagerInterface $em, $class)
3933
{
40-
// NOTE: bug in Doctrine, hinting EntityRepository|ObjectRepository when only EntityRepository is expected
41-
/** @var EntityRepository $repository */
42-
$repository = $em->getRepository($class);
43-
4434
$this->em = $em;
45-
$this->repository = $repository;
4635
$this->class = $class;
4736
}
4837

@@ -59,7 +48,7 @@ public function getClass()
5948
*/
6049
public function findClientBy(array $criteria)
6150
{
62-
return $this->repository->findOneBy($criteria);
51+
return $this->em->getRepository($this->class)->findOneBy($criteria);
6352
}
6453

6554
/**

Entity/TokenManager.php

+5-12
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,14 @@ class TokenManager extends BaseTokenManager
2525
*/
2626
protected $em;
2727

28-
/**
29-
* @var EntityRepository
30-
*/
31-
protected $repository;
32-
3328
/**
3429
* @var string
3530
*/
3631
protected $class;
3732

3833
public function __construct(EntityManagerInterface $em, $class)
3934
{
40-
// NOTE: bug in Doctrine, hinting EntityRepository|ObjectRepository when only EntityRepository is expected
41-
/** @var EntityRepository $repository */
42-
$repository = $em->getRepository($class);
43-
4435
$this->em = $em;
45-
$this->repository = $repository;
4636
$this->class = $class;
4737
}
4838

@@ -59,7 +49,7 @@ public function getClass()
5949
*/
6050
public function findTokenBy(array $criteria)
6151
{
62-
return $this->repository->findOneBy($criteria);
52+
return $this->em->getRepository($this->class)->findOneBy($criteria);
6353
}
6454

6555
/**
@@ -85,7 +75,10 @@ public function deleteToken(TokenInterface $token)
8575
*/
8676
public function deleteExpired()
8777
{
88-
$qb = $this->repository->createQueryBuilder('t');
78+
// NOTE: bug in Doctrine, hinting EntityRepository|ObjectRepository when only EntityRepository is expected
79+
/** @var EntityRepository $repository */
80+
$repository = $this->em->getRepository($this->class);
81+
$qb = $repository->createQueryBuilder('t');
8982
$qb
9083
->delete()
9184
->where('t.expiresAt < ?1')

Tests/Document/ClientManagerTest.php

+7-8
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,6 @@ public function setUp()
6161
;
6262
$this->className = 'RandomClassName'.\random_bytes(5);
6363

64-
$this->documentManager
65-
->expects($this->once())
66-
->method('getRepository')
67-
->with($this->className)
68-
->willReturn($this->repository)
69-
;
70-
7164
$this->instance = new ClientManager($this->documentManager, $this->className);
7265

7366
parent::setUp();
@@ -76,7 +69,6 @@ public function setUp()
7669
public function testConstructWillSetParameters()
7770
{
7871
$this->assertAttributeSame($this->documentManager, 'dm', $this->instance);
79-
$this->assertAttributeSame($this->repository, 'repository', $this->instance);
8072
$this->assertAttributeSame($this->className, 'class', $this->instance);
8173
}
8274

@@ -92,6 +84,13 @@ public function testFindClientBy()
9284
\random_bytes(5),
9385
];
9486

87+
$this->documentManager
88+
->expects($this->once())
89+
->method('getRepository')
90+
->with($this->className)
91+
->willReturn($this->repository)
92+
;
93+
9594
$this->repository
9695
->expects($this->once())
9796
->method('findOneBy')

Tests/Document/TokenManagerTest.php

+14-7
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,6 @@ public function setUp()
6565
->getMock()
6666
;
6767

68-
$this->documentManager
69-
->expects($this->once())
70-
->method('getRepository')
71-
->with($this->className)
72-
->willReturn($this->repository)
73-
;
74-
7568
$this->instance = new TokenManager($this->documentManager, $this->className);
7669
}
7770

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

76+
$this->documentManager
77+
->expects($this->once())
78+
->method('getRepository')
79+
->with($this->className)
80+
->willReturn($this->repository)
81+
;
82+
8383
$this->repository
8484
->expects($this->once())
8585
->method('findOneBy')
@@ -145,6 +145,13 @@ public function testDeleteToken()
145145

146146
public function testDeleteExpired()
147147
{
148+
$this->documentManager
149+
->expects($this->once())
150+
->method('getRepository')
151+
->with($this->className)
152+
->willReturn($this->repository)
153+
;
154+
148155
$queryBuilder = $this->getMockBuilder(Builder::class)
149156
->disableOriginalConstructor()
150157
->getMock()

Tests/Entity/ClientManagerTest.php

+7-8
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,6 @@ public function setUp()
5757
;
5858
$this->className = 'RandomClassName'.\random_bytes(5);
5959

60-
$this->entityManager
61-
->expects($this->once())
62-
->method('getRepository')
63-
->with($this->className)
64-
->willReturn($this->repository)
65-
;
66-
6760
$this->instance = new ClientManager($this->entityManager, $this->className);
6861

6962
parent::setUp();
@@ -72,7 +65,6 @@ public function setUp()
7265
public function testConstructWillSetParameters()
7366
{
7467
$this->assertAttributeSame($this->entityManager, 'em', $this->instance);
75-
$this->assertAttributeSame($this->repository, 'repository', $this->instance);
7668
$this->assertAttributeSame($this->className, 'class', $this->instance);
7769
}
7870

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

83+
$this->entityManager
84+
->expects($this->once())
85+
->method('getRepository')
86+
->with($this->className)
87+
->willReturn($this->repository)
88+
;
89+
9190
$this->repository
9291
->expects($this->once())
9392
->method('findOneBy')

Tests/Entity/TokenManagerTest.php

+14-8
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,12 @@ public function setUp()
6363
->getMock()
6464
;
6565

66-
$this->entityManager
67-
->expects($this->once())
68-
->method('getRepository')
69-
->with($this->className)
70-
->willReturn($this->repository)
71-
;
72-
7366
$this->instance = new TokenManager($this->entityManager, $this->className);
7467
}
7568

7669
public function testConstructWillSetParameters()
7770
{
7871
$this->assertAttributeSame($this->entityManager, 'em', $this->instance);
79-
$this->assertAttributeSame($this->repository, 'repository', $this->instance);
8072
$this->assertAttributeSame($this->className, 'class', $this->instance);
8173
}
8274

@@ -112,6 +104,13 @@ public function testFindTokenBy()
112104
\random_bytes(5),
113105
];
114106

107+
$this->entityManager
108+
->expects($this->once())
109+
->method('getRepository')
110+
->with($this->className)
111+
->willReturn($this->repository)
112+
;
113+
115114
$this->repository
116115
->expects($this->once())
117116
->method('findOneBy')
@@ -174,6 +173,13 @@ public function testDeleteExpired()
174173
{
175174
$randomResult = \random_bytes(10);
176175

176+
$this->entityManager
177+
->expects($this->once())
178+
->method('getRepository')
179+
->with($this->className)
180+
->willReturn($this->repository)
181+
;
182+
177183
$queryBuilder = $this->getMockBuilder(QueryBuilder::class)
178184
->disableOriginalConstructor()
179185
->getMock()

0 commit comments

Comments
 (0)