1
0
Fork 0
mirror of https://github.com/shlinkio/shlink.git synced 2025-03-31 22:05:06 +03:00
shlink/module/Rest/src/ApiKey/Repository/ApiKeyRepository.php
2022-09-11 12:33:28 +02:00

32 lines
1.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\ApiKey\Repository;
use Doctrine\DBAL\LockMode;
use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepository;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
class ApiKeyRepository extends EntitySpecificationRepository implements ApiKeyRepositoryInterface
{
public function createInitialApiKey(string $apiKey): void
{
$em = $this->getEntityManager();
$em->wrapInTransaction(function () use ($apiKey, $em): void {
// Ideally this would be a SELECT COUNT(...), but MsSQL and Postgres do not allow locking on aggregates
// Because of that we check if at least one result exists
$firstResult = $em->createQueryBuilder()->select('a.id')
->from(ApiKey::class, 'a')
->setMaxResults(1)
->getQuery()
->setLockMode(LockMode::PESSIMISTIC_WRITE)
->getOneOrNullResult();
if ($firstResult === null) {
$em->persist(ApiKey::fromKey($apiKey));
$em->flush();
}
});
}
}