Modernize ApiKey entity

This commit is contained in:
Alejandro Celaya 2024-03-18 18:33:56 +01:00
parent ab8fa52ca4
commit 60e9443b12
4 changed files with 14 additions and 31 deletions

View file

@ -50,11 +50,11 @@ class ListKeysCommand extends Command
$enabledOnly = $input->getOption('enabled-only'); $enabledOnly = $input->getOption('enabled-only');
$rows = array_map(function (ApiKey $apiKey) use ($enabledOnly) { $rows = array_map(function (ApiKey $apiKey) use ($enabledOnly) {
$expiration = $apiKey->getExpirationDate(); $expiration = $apiKey->expirationDate;
$messagePattern = $this->determineMessagePattern($apiKey); $messagePattern = $this->determineMessagePattern($apiKey);
// Set columns for this row // Set columns for this row
$rowData = [sprintf($messagePattern, $apiKey), sprintf($messagePattern, $apiKey->name() ?? '-')]; $rowData = [sprintf($messagePattern, $apiKey), sprintf($messagePattern, $apiKey->name ?? '-')];
if (! $enabledOnly) { if (! $enabledOnly) {
$rowData[] = sprintf($messagePattern, $this->getEnabledSymbol($apiKey)); $rowData[] = sprintf($messagePattern, $this->getEnabledSymbol($apiKey));
} }

View file

@ -233,7 +233,7 @@ class ListShortUrlsCommand extends Command
} }
if ($input->getOption('show-api-key-name')) { if ($input->getOption('show-api-key-name')) {
$columnsMap['API Key Name'] = static fn (array $_, ShortUrl $shortUrl): ?string => $columnsMap['API Key Name'] = static fn (array $_, ShortUrl $shortUrl): ?string =>
$shortUrl->authorApiKey()?->name(); $shortUrl->authorApiKey()?->name;
} }
return $columnsMap; return $columnsMap;

View file

@ -17,21 +17,17 @@ use Shlinkio\Shlink\Rest\ApiKey\Role;
class ApiKey extends AbstractEntity class ApiKey extends AbstractEntity
{ {
private string $key;
private ?Chronos $expirationDate = null;
private bool $enabled;
/** @var Collection<string, ApiKeyRole> */
private Collection $roles;
private ?string $name = null;
/** /**
* @param Collection<string, ApiKeyRole> $roles
* @throws Exception * @throws Exception
*/ */
private function __construct(string $key) private function __construct(
{ private string $key,
$this->key = $key; public readonly ?string $name = null,
$this->enabled = true; public readonly ?Chronos $expirationDate = null,
$this->roles = new ArrayCollection(); private bool $enabled = true,
private Collection $roles = new ArrayCollection(),
) {
} }
/** /**
@ -47,10 +43,7 @@ class ApiKey extends AbstractEntity
*/ */
public static function fromMeta(ApiKeyMeta $meta): self public static function fromMeta(ApiKeyMeta $meta): self
{ {
$apiKey = new self($meta->key); $apiKey = new self($meta->key, $meta->name, $meta->expirationDate);
$apiKey->name = $meta->name;
$apiKey->expirationDate = $meta->expirationDate;
foreach ($meta->roleDefinitions as $roleDefinition) { foreach ($meta->roleDefinitions as $roleDefinition) {
$apiKey->registerRole($roleDefinition); $apiKey->registerRole($roleDefinition);
} }
@ -58,21 +51,11 @@ class ApiKey extends AbstractEntity
return $apiKey; return $apiKey;
} }
public function getExpirationDate(): ?Chronos
{
return $this->expirationDate;
}
public function isExpired(): bool public function isExpired(): bool
{ {
return $this->expirationDate !== null && $this->expirationDate->lessThan(Chronos::now()); return $this->expirationDate !== null && $this->expirationDate->lessThan(Chronos::now());
} }
public function name(): ?string
{
return $this->name;
}
public function isEnabled(): bool public function isEnabled(): bool
{ {
return $this->enabled; return $this->enabled;

View file

@ -44,8 +44,8 @@ class ApiKeyServiceTest extends TestCase
ApiKeyMeta::fromParams(name: $name, expirationDate: $date, roleDefinitions: $roles), ApiKeyMeta::fromParams(name: $name, expirationDate: $date, roleDefinitions: $roles),
); );
self::assertEquals($date, $key->getExpirationDate()); self::assertEquals($date, $key->expirationDate);
self::assertEquals($name, $key->name()); self::assertEquals($name, $key->name);
foreach ($roles as $roleDefinition) { foreach ($roles as $roleDefinition) {
self::assertTrue($key->hasRole($roleDefinition->role)); self::assertTrue($key->hasRole($roleDefinition->role));
} }