Created new env var to programatically provide an initial API key

This commit is contained in:
Alejandro Celaya
2022-09-11 10:45:03 +02:00
parent e6ee4ceae2
commit f5138385be
9 changed files with 119 additions and 10 deletions

View File

@@ -0,0 +1,29 @@
<?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
{
$this->getEntityManager()->wrapInTransaction(function () use ($apiKey): void {
$qb = $this->getEntityManager()->createQueryBuilder();
$amountOfApiKeys = $qb->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();
}
});
}
}