mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-11 01:33:11 +08:00
Improved public API in ApiKey entity, reducing anemic model
This commit is contained in:
@@ -36,21 +36,11 @@ class ApiKey extends AbstractEntity
|
||||
*/
|
||||
private $enabled;
|
||||
|
||||
public function __construct()
|
||||
public function __construct(?Chronos $expirationDate = null)
|
||||
{
|
||||
$this->enabled = true;
|
||||
$this->key = $this->generateV4Uuid();
|
||||
}
|
||||
|
||||
public function getKey(): string
|
||||
{
|
||||
return $this->key;
|
||||
}
|
||||
|
||||
public function setKey(string $key): self
|
||||
{
|
||||
$this->key = $key;
|
||||
return $this;
|
||||
$this->expirationDate = $expirationDate;
|
||||
$this->enabled = true;
|
||||
}
|
||||
|
||||
public function getExpirationDate(): ?Chronos
|
||||
@@ -58,12 +48,6 @@ class ApiKey extends AbstractEntity
|
||||
return $this->expirationDate;
|
||||
}
|
||||
|
||||
public function setExpirationDate(Chronos $expirationDate): self
|
||||
{
|
||||
$this->expirationDate = $expirationDate;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isExpired(): bool
|
||||
{
|
||||
if ($this->expirationDate === null) {
|
||||
@@ -78,15 +62,10 @@ class ApiKey extends AbstractEntity
|
||||
return $this->enabled;
|
||||
}
|
||||
|
||||
public function setEnabled(bool $enabled): self
|
||||
{
|
||||
$this->enabled = $enabled;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function disable(): self
|
||||
{
|
||||
return $this->setEnabled(false);
|
||||
$this->enabled = false;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,6 +78,6 @@ class ApiKey extends AbstractEntity
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->getKey();
|
||||
return $this->key;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,11 +23,7 @@ class ApiKeyService implements ApiKeyServiceInterface
|
||||
|
||||
public function create(?Chronos $expirationDate = null): ApiKey
|
||||
{
|
||||
$key = new ApiKey();
|
||||
if ($expirationDate !== null) {
|
||||
$key->setExpirationDate($expirationDate);
|
||||
}
|
||||
|
||||
$key = new ApiKey($expirationDate);
|
||||
$this->em->persist($key);
|
||||
$this->em->flush();
|
||||
|
||||
@@ -57,6 +53,9 @@ class ApiKeyService implements ApiKeyServiceInterface
|
||||
return $apiKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ApiKey[]
|
||||
*/
|
||||
public function listKeys(bool $enabledOnly = false): array
|
||||
{
|
||||
$conditions = $enabledOnly ? ['enabled' => true] : [];
|
||||
|
||||
@@ -18,6 +18,9 @@ interface ApiKeyServiceInterface
|
||||
*/
|
||||
public function disable(string $key): ApiKey;
|
||||
|
||||
/**
|
||||
* @return ApiKey[]
|
||||
*/
|
||||
public function listKeys(bool $enabledOnly = false): array;
|
||||
|
||||
public function getByKey(string $key): ?ApiKey;
|
||||
|
||||
Reference in New Issue
Block a user