Skip to content

Commit d0c63a7

Browse files
committed
Onthefly repo for AuthCodeManager
This avoids a database connection to be established for every request Completes fix for issue FriendsOfSymfony#422
1 parent 208110e commit d0c63a7

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

Document/AuthCodeManager.php

+5-14
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\AuthCodeInterface;
1918
use FOS\OAuthServerBundle\Model\AuthCodeManager as BaseAuthCodeManager;
2019

@@ -25,24 +24,14 @@ class AuthCodeManager extends BaseAuthCodeManager
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 findAuthCodeBy(array $criteria)
6150
{
62-
return $this->repository->findOneBy($criteria);
51+
return $this->dm->getRepository($this->class)->findOneBy($criteria);
6352
}
6453

6554
/**
@@ -85,8 +74,10 @@ public function deleteAuthCode(AuthCodeInterface $authCode)
8574
*/
8675
public function deleteExpired()
8776
{
88-
$result = $this
89-
->repository
77+
/** @var \Doctrine\ODM\MongoDB\DocumentRepository $repository */
78+
$repository = $this->dm->getRepository($this->class);
79+
80+
$result = $repository
9081
->createQueryBuilder()
9182
->remove()
9283
->field('expiresAt')->lt(time())

Tests/Document/AuthCodeManagerTest.php

+14-7
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,6 @@ public function setUp()
6565
;
6666
$this->className = 'TestClassName'.\random_bytes(5);
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 AuthCodeManager($this->documentManager, $this->className);
7669

7770
parent::setUp();
@@ -95,6 +88,13 @@ public function testFindAuthCodeBy()
9588
\random_bytes(10),
9689
];
9790

91+
$this->documentManager
92+
->expects($this->once())
93+
->method('getRepository')
94+
->with($this->className)
95+
->willReturn($this->repository)
96+
;
97+
9898
$this->repository
9999
->expects($this->once())
100100
->method('findOneBy')
@@ -155,6 +155,13 @@ public function testDeleteAuthCode()
155155

156156
public function testDeleteExpired()
157157
{
158+
$this->documentManager
159+
->expects($this->once())
160+
->method('getRepository')
161+
->with($this->className)
162+
->willReturn($this->repository)
163+
;
164+
158165
$queryBuilder = $this->getMockBuilder(Builder::class)
159166
->disableOriginalConstructor()
160167
->getMock()

0 commit comments

Comments
 (0)