Skip to content

Commit

Permalink
Refactor findAllForUserEntity and findOneByCredentialId methods
Browse files Browse the repository at this point in the history
The methods findAllForUserEntity and findOneByCredentialId in DoctrineCredentialSourceRepository have been refactored to use the QueryBuilder instead of previous findBy/findOneBy methods for better readability and control over SQL queries. The findOneByCredentialId now also sets a limit to the query results.
  • Loading branch information
Spomky committed Jul 12, 2024
1 parent 611f380 commit 180f4f2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
10 changes: 10 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,16 @@ parameters:
count: 1
path: src/symfony/src/Doctrine/Type/TrustPathDataType.php

-
message: "#^Method Webauthn\\\\Bundle\\\\Repository\\\\DoctrineCredentialSourceRepository\\:\\:findAllForUserEntity\\(\\) should return array\\<Webauthn\\\\PublicKeyCredentialSource\\> but returns mixed\\.$#"
count: 1
path: src/symfony/src/Repository/DoctrineCredentialSourceRepository.php

-
message: "#^Method Webauthn\\\\Bundle\\\\Repository\\\\DoctrineCredentialSourceRepository\\:\\:findOneByCredentialId\\(\\) should return Webauthn\\\\PublicKeyCredentialSource\\|null but returns mixed\\.$#"
count: 1
path: src/symfony/src/Repository/DoctrineCredentialSourceRepository.php

-
message: "#^Property Webauthn\\\\Bundle\\\\Security\\\\Authentication\\\\Token\\\\WebauthnToken\\:\\:\\$extensions \\(Webauthn\\\\AuthenticationExtensions\\\\AuthenticationExtensions\\|null\\) does not accept mixed\\.$#"
count: 1
Expand Down
26 changes: 17 additions & 9 deletions src/symfony/src/Repository/DoctrineCredentialSourceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,28 @@ public function saveCredentialSource(PublicKeyCredentialSource $publicKeyCredent
->flush();
}

/**
* @return array<T>
*/
public function findAllForUserEntity(PublicKeyCredentialUserEntity $publicKeyCredentialUserEntity): array
{
return $this->findBy([
'userHandle' => $publicKeyCredentialUserEntity->id,
]);
return $this->getEntityManager()
->createQueryBuilder()
->from($this->class, 'c')
->select('c')
->where('c.userHandle = :userHandle')
->setParameter(':userHandle', $publicKeyCredentialUserEntity->id)
->getQuery()
->execute();
}

public function findOneByCredentialId(string $publicKeyCredentialId): ?PublicKeyCredentialSource
{
return $this->findOneBy([
'publicKeyCredentialId' => base64_encode($publicKeyCredentialId),
]);
return $this->getEntityManager()
->createQueryBuilder()
->from($this->class, 'c')
->select('c')
->where('c.publicKeyCredentialId = :publicKeyCredentialId')
->setParameter(':publicKeyCredentialId', base64_encode($publicKeyCredentialId))
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();
}
}

0 comments on commit 180f4f2

Please sign in to comment.