Skip to content

Commit

Permalink
Fix class-string extend issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Sep 29, 2023
1 parent 02842ac commit 226dcca
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
47 changes: 18 additions & 29 deletions src/symfony/src/Repository/DoctrineCredentialSourceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,42 @@

namespace Webauthn\Bundle\Repository;

use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use InvalidArgumentException;
use Webauthn\PublicKeyCredentialSource;
use Webauthn\PublicKeyCredentialUserEntity;

class DoctrineCredentialSourceRepository implements PublicKeyCredentialSourceRepositoryInterface, CanSaveCredentialSource, ServiceEntityRepositoryInterface
/**
* @template T of PublicKeyCredentialSource
* @template-extends ServiceEntityRepository<T>
*/
class DoctrineCredentialSourceRepository extends ServiceEntityRepository implements PublicKeyCredentialSourceRepositoryInterface, CanSaveCredentialSource
{
private readonly EntityManagerInterface $manager;

private readonly string $class;

/**
* @param class-string $class The class name of the entity this repository manages
*/
public function __construct(ManagerRegistry $registry, string $class)
{
is_subclass_of($class, PublicKeyCredentialSource::class) || throw new InvalidArgumentException(sprintf(
'Invalid class. Must be an instance of "Webauthn\PublicKeyCredentialSource", got "%s" instead.',
$class
));
$manager = $registry->getManagerForClass($class);
$manager instanceof EntityManagerInterface || throw new InvalidArgumentException(sprintf(
'Could not find the entity manager for class "%s". Check your Doctrine configuration to make sure it is configured to load this entity\'s metadata.',
$class
));
$this->class = $class;
$this->manager = $manager;
parent::__construct($registry, $class);
}

public function saveCredentialSource(PublicKeyCredentialSource $publicKeyCredentialSource): void
{
$this->manager->persist($publicKeyCredentialSource);
$this->manager->flush();
$this->getEntityManager()
->persist($publicKeyCredentialSource);
$this->getEntityManager()
->flush();
}

public function findAllForUserEntity(PublicKeyCredentialUserEntity $publicKeyCredentialUserEntity): array
{
$qb = $this->manager->createQueryBuilder();

return $qb->select('c')
->from($this->getClass(), 'c')
return $this->getEntityManager()
->createQueryBuilder()->select('c')
->where('c.userHandle = :userHandle')
->setParameter(':userHandle', $publicKeyCredentialUserEntity->id)
->getQuery()
Expand All @@ -55,24 +48,20 @@ public function findAllForUserEntity(PublicKeyCredentialUserEntity $publicKeyCre

public function findOneByCredentialId(string $publicKeyCredentialId): ?PublicKeyCredentialSource
{
$qb = $this->manager->createQueryBuilder();

return $qb->select('c')
->from($this->getClass(), 'c')
return $this->getEntityManager()
->createQueryBuilder()->select('c')
->where('c.publicKeyCredentialId = :publicKeyCredentialId')
->setParameter(':publicKeyCredentialId', base64_encode($publicKeyCredentialId))
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();
}

/**
* @deprecated since 4.7.2 and will be removed in 5.0.0
*/
protected function getClass(): string
{
return $this->class;
}

protected function getEntityManager(): EntityManagerInterface
{
return $this->manager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Webauthn\PublicKeyCredentialSource;
use Webauthn\PublicKeyCredentialUserEntity;

/**
* @template T of PublicKeyCredentialSource
*/
interface PublicKeyCredentialSourceRepositoryInterface
{
/**
Expand Down

0 comments on commit 226dcca

Please sign in to comment.