Reduced duplication in ApiKeyRepository

This commit is contained in:
Alejandro Celaya 2022-09-11 11:59:49 +02:00
parent f5138385be
commit c841e57db5

View File

@ -12,17 +12,17 @@ class ApiKeyRepository extends EntitySpecificationRepository implements ApiKeyRe
{ {
public function createInitialApiKey(string $apiKey): void public function createInitialApiKey(string $apiKey): void
{ {
$this->getEntityManager()->wrapInTransaction(function () use ($apiKey): void { $em = $this->getEntityManager();
$qb = $this->getEntityManager()->createQueryBuilder(); $em->wrapInTransaction(function () use ($apiKey, $em): void {
$amountOfApiKeys = $qb->select('COUNT(a.id)') $amountOfApiKeys = $em->createQueryBuilder()->select('COUNT(a.id)')
->from(ApiKey::class, 'a') ->from(ApiKey::class, 'a')
->getQuery() ->getQuery()
->setLockMode(LockMode::PESSIMISTIC_WRITE) ->setLockMode(LockMode::PESSIMISTIC_WRITE)
->getSingleScalarResult(); ->getSingleScalarResult();
if ($amountOfApiKeys === 0) { if ($amountOfApiKeys === 0) {
$this->getEntityManager()->persist(ApiKey::fromKey($apiKey)); $em->persist(ApiKey::fromKey($apiKey));
$this->getEntityManager()->flush(); $em->flush();
} }
}); });
} }