Skip to content

Commit 25b47fa

Browse files
committed
Repository for ClientManager and TokenManager is now instanciated on the fly, not in the constructor anymore
This avoids a database connection to be established for every request Fixes issue #422
1 parent dd22826 commit 25b47fa

File tree

4 files changed

+24
-41
lines changed

4 files changed

+24
-41
lines changed

Entity/ClientManager.php

Lines changed: 1 addition & 12 deletions
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

Lines changed: 2 additions & 13 deletions
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\TokenInterface;
1918
use FOS\OAuthServerBundle\Model\TokenManager as BaseTokenManager;
2019

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

6554
/**
@@ -85,7 +74,7 @@ public function deleteToken(TokenInterface $token)
8574
*/
8675
public function deleteExpired()
8776
{
88-
$qb = $this->repository->createQueryBuilder('t');
77+
$qb = $this->em->getRepository($this->class)->createQueryBuilder('t');
8978
$qb
9079
->delete()
9180
->where('t.expiresAt < ?1')

Tests/Entity/ClientManagerTest.php

Lines changed: 7 additions & 8 deletions
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

Lines changed: 14 additions & 8 deletions
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)