Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added function to 'lazyload' the repository #580

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions Entity/ClientManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class ClientManager extends BaseClientManager
public function __construct(ObjectManager $em, $class)
{
$this->em = $em;
$this->repository = $em->getRepository($class);
$this->class = $class;
}

Expand All @@ -54,7 +53,7 @@ public function getClass()
*/
public function findClientBy(array $criteria)
{
return $this->repository->findOneBy($criteria);
return $this->getRepository()->findOneBy($criteria);
}

/**
Expand All @@ -74,4 +73,16 @@ public function deleteClient(ClientInterface $client)
$this->em->remove($client);
$this->em->flush();
}

/**
* @return EntityRepository
*/
private function getRepository()
{
if(is_null($this->repository))
{
$this->repository = $this->em->getRepository($this->class);
}
return $this->repository;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

}
}
17 changes: 14 additions & 3 deletions Entity/TokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class TokenManager extends BaseTokenManager
public function __construct(ObjectManager $em, $class)
{
$this->em = $em;
$this->repository = $em->getRepository($class);
$this->class = $class;
}

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

/**
Expand All @@ -80,12 +79,24 @@ public function deleteToken(TokenInterface $token)
*/
public function deleteExpired()
{
$qb = $this->repository->createQueryBuilder('t');
$qb = $this->getRepository()->createQueryBuilder('t');
$qb
->delete()
->where('t.expiresAt < ?1')
->setParameters(array(1 => time()));

return $qb->getQuery()->execute();
}

/**
* @return EntityRepository
*/
private function getRepository()
{
if(is_null($this->repository))
{
$this->repository = $this->em->getRepository($this->class);
}
return $this->repository;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a line break missing :)

}
}
15 changes: 8 additions & 7 deletions Tests/Entity/ClientManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,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);

Expand All @@ -61,7 +55,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 @@ -84,6 +77,14 @@ public function testFindClientBy()
->willReturn($randomResult)
;


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

$this->assertSame($randomResult, $this->instance->findClientBy($criteria));
}

Expand Down
24 changes: 16 additions & 8 deletions Tests/Entity/TokenManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,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 @@ -116,6 +108,14 @@ public function testFindTokenBy()
->willReturn($randomResult)
;


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

$this->assertSame($randomResult, $this->instance->findTokenBy($criteria));
}

Expand Down Expand Up @@ -223,6 +223,14 @@ public function testDeleteExpired()
->willReturn($randomResult)
;


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

$this->assertSame($randomResult, $this->instance->deleteExpired());
}
}