Skip to content

Added function to 'lazyload' the repository #580

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

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions Entity/ClientManager.php
Original file line number Diff line number Diff line change
@@ -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;
}

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

/**
@@ -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
@@ -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;
}

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

/**
@@ -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
@@ -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);

@@ -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);
}

@@ -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));
}

24 changes: 16 additions & 8 deletions Tests/Entity/TokenManagerTest.php
Original file line number Diff line number Diff line change
@@ -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);
}

@@ -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));
}

@@ -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());
}
}