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
{
$this->getEntityManager()->wrapInTransaction(function () use ($apiKey): void {
$qb = $this->getEntityManager()->createQueryBuilder();
$amountOfApiKeys = $qb->select('COUNT(a.id)')
$em = $this->getEntityManager();
$em->wrapInTransaction(function () use ($apiKey, $em): void {
$amountOfApiKeys = $em->createQueryBuilder()->select('COUNT(a.id)')
->from(ApiKey::class, 'a')
->getQuery()
->setLockMode(LockMode::PESSIMISTIC_WRITE)
->getSingleScalarResult();
if ($amountOfApiKeys === 0) {
$this->getEntityManager()->persist(ApiKey::fromKey($apiKey));
$this->getEntityManager()->flush();
$em->persist(ApiKey::fromKey($apiKey));
$em->flush();
}
});
}