diff --git a/module/CLI/src/Command/Api/ListKeysCommand.php b/module/CLI/src/Command/Api/ListKeysCommand.php index fab02087..40ae8eef 100644 --- a/module/CLI/src/Command/Api/ListKeysCommand.php +++ b/module/CLI/src/Command/Api/ListKeysCommand.php @@ -50,11 +50,11 @@ class ListKeysCommand extends Command $enabledOnly = $input->getOption('enabled-only'); $rows = array_map(function (ApiKey $apiKey) use ($enabledOnly) { - $expiration = $apiKey->getExpirationDate(); + $expiration = $apiKey->expirationDate; $messagePattern = $this->determineMessagePattern($apiKey); // Set columns for this row - $rowData = [sprintf($messagePattern, $apiKey), sprintf($messagePattern, $apiKey->name() ?? '-')]; + $rowData = [sprintf($messagePattern, $apiKey), sprintf($messagePattern, $apiKey->name ?? '-')]; if (! $enabledOnly) { $rowData[] = sprintf($messagePattern, $this->getEnabledSymbol($apiKey)); } diff --git a/module/CLI/src/Command/ShortUrl/ListShortUrlsCommand.php b/module/CLI/src/Command/ShortUrl/ListShortUrlsCommand.php index a318e6e4..ffcffd8b 100644 --- a/module/CLI/src/Command/ShortUrl/ListShortUrlsCommand.php +++ b/module/CLI/src/Command/ShortUrl/ListShortUrlsCommand.php @@ -233,7 +233,7 @@ class ListShortUrlsCommand extends Command } if ($input->getOption('show-api-key-name')) { $columnsMap['API Key Name'] = static fn (array $_, ShortUrl $shortUrl): ?string => - $shortUrl->authorApiKey()?->name(); + $shortUrl->authorApiKey()?->name; } return $columnsMap; diff --git a/module/Rest/src/Entity/ApiKey.php b/module/Rest/src/Entity/ApiKey.php index 9ad3fcf4..46548dcf 100644 --- a/module/Rest/src/Entity/ApiKey.php +++ b/module/Rest/src/Entity/ApiKey.php @@ -17,21 +17,17 @@ use Shlinkio\Shlink\Rest\ApiKey\Role; class ApiKey extends AbstractEntity { - private string $key; - private ?Chronos $expirationDate = null; - private bool $enabled; - /** @var Collection */ - private Collection $roles; - private ?string $name = null; - /** + * @param Collection $roles * @throws Exception */ - private function __construct(string $key) - { - $this->key = $key; - $this->enabled = true; - $this->roles = new ArrayCollection(); + private function __construct( + private string $key, + public readonly ?string $name = null, + public readonly ?Chronos $expirationDate = null, + private bool $enabled = true, + private Collection $roles = new ArrayCollection(), + ) { } /** @@ -47,10 +43,7 @@ class ApiKey extends AbstractEntity */ public static function fromMeta(ApiKeyMeta $meta): self { - $apiKey = new self($meta->key); - $apiKey->name = $meta->name; - $apiKey->expirationDate = $meta->expirationDate; - + $apiKey = new self($meta->key, $meta->name, $meta->expirationDate); foreach ($meta->roleDefinitions as $roleDefinition) { $apiKey->registerRole($roleDefinition); } @@ -58,21 +51,11 @@ class ApiKey extends AbstractEntity return $apiKey; } - public function getExpirationDate(): ?Chronos - { - return $this->expirationDate; - } - public function isExpired(): bool { return $this->expirationDate !== null && $this->expirationDate->lessThan(Chronos::now()); } - public function name(): ?string - { - return $this->name; - } - public function isEnabled(): bool { return $this->enabled; diff --git a/module/Rest/test/Service/ApiKeyServiceTest.php b/module/Rest/test/Service/ApiKeyServiceTest.php index f45e6ca5..45364070 100644 --- a/module/Rest/test/Service/ApiKeyServiceTest.php +++ b/module/Rest/test/Service/ApiKeyServiceTest.php @@ -44,8 +44,8 @@ class ApiKeyServiceTest extends TestCase ApiKeyMeta::fromParams(name: $name, expirationDate: $date, roleDefinitions: $roles), ); - self::assertEquals($date, $key->getExpirationDate()); - self::assertEquals($name, $key->name()); + self::assertEquals($date, $key->expirationDate); + self::assertEquals($name, $key->name); foreach ($roles as $roleDefinition) { self::assertTrue($key->hasRole($roleDefinition->role)); }