mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-05 23:03:11 +08:00
Created new env var to programatically provide an initial API key
This commit is contained in:
29
module/Rest/src/ApiKey/Repository/ApiKeyRepository.php
Normal file
29
module/Rest/src/ApiKey/Repository/ApiKeyRepository.php
Normal 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();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user