mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-23 21:27:44 +03:00
Modernize ApiKey entity
This commit is contained in:
parent
ab8fa52ca4
commit
60e9443b12
4 changed files with 14 additions and 31 deletions
|
@ -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));
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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<string, ApiKeyRole> */
|
||||
private Collection $roles;
|
||||
private ?string $name = null;
|
||||
|
||||
/**
|
||||
* @param Collection<string, ApiKeyRole> $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;
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue