mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-10 09:13:11 +08:00
Created new env var to programatically provide an initial API key
This commit is contained in:
31
module/Rest/src/ApiKey/InitialApiKeyDelegator.php
Normal file
31
module/Rest/src/ApiKey/InitialApiKeyDelegator.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Rest\ApiKey;
|
||||
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Mezzio\Application;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Shlinkio\Shlink\Rest\ApiKey\Repository\ApiKeyRepositoryInterface;
|
||||
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||
|
||||
class InitialApiKeyDelegator
|
||||
{
|
||||
public function __invoke(ContainerInterface $container, string $serviceName, callable $callback): Application
|
||||
{
|
||||
$initialApiKey = $container->get('config')['initial_api_key'] ?? null;
|
||||
if ($initialApiKey !== null) {
|
||||
$this->createInitialApiKey($initialApiKey, $container);
|
||||
}
|
||||
|
||||
return $callback();
|
||||
}
|
||||
|
||||
private function createInitialApiKey(string $initialApiKey, ContainerInterface $container): void
|
||||
{
|
||||
/** @var ApiKeyRepositoryInterface $repo */
|
||||
$repo = $container->get(EntityManager::class)->getRepository(ApiKey::class);
|
||||
$repo->createInitialApiKey($initialApiKey);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user