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

Don't get repository at construction time, it could be not loaded yet! #574

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 2 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
35 changes: 23 additions & 12 deletions Entity/TokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,30 @@ class TokenManager extends BaseTokenManager
*/
protected $em;

/**
* @var EntityRepository
*/
protected $repository;

/**
* @var string
*/
protected $class;

public function __construct(EntityManagerInterface $em, $class)
{
// NOTE: bug in Doctrine, hinting EntityRepository|ObjectRepository when only EntityRepository is expected
/** @var EntityRepository $repository */
$repository = $em->getRepository($class);

$this->em = $em;
$this->repository = $repository;
$this->class = $class;
}

/**
* retrocompatibility with old $repository property.
* @param $name
* @return mixed
*/
public function __get($name)
Copy link
Member

Choose a reason for hiding this comment

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

should not be public to provide BC with a protected property.

and it should trigger a deprecation warning

Copy link
Author

Choose a reason for hiding this comment

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

__get can only be public... I can't think a method to provide retrocompatibility for private property.

{
if ('repository' === $name) {
return $this->getRepository();
}

return $this->$name;
Copy link
Member

Choose a reason for hiding this comment

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

should not allow access. This gives public access to private properties

Copy link
Author

Choose a reason for hiding this comment

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

Removed!

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

/**
Expand All @@ -85,7 +88,7 @@ public function deleteToken(TokenInterface $token)
*/
public function deleteExpired()
{
$qb = $this->repository->createQueryBuilder('t');
$qb = $this->getRepository()->createQueryBuilder('t');
$qb
->delete()
->where('t.expiresAt < ?1')
Expand All @@ -94,4 +97,12 @@ public function deleteExpired()

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

/**
* @return EntityRepository
*/
protected function getRepository(): EntityRepository
{
return $this->em->getRepository($this->class);
}
}