Extracted ApiKey metadata to the ApiKeyMeta object

This commit is contained in:
Alejandro Celaya
2021-03-14 09:59:35 +01:00
parent 9b55389538
commit 0a5c265b12
36 changed files with 186 additions and 121 deletions

View File

@@ -12,6 +12,7 @@ use Happyr\DoctrineSpecification\Spec;
use Happyr\DoctrineSpecification\Specification\Specification;
use Ramsey\Uuid\Uuid;
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta;
use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition;
use Shlinkio\Shlink\Rest\ApiKey\Role;
@@ -27,7 +28,7 @@ class ApiKey extends AbstractEntity
/**
* @throws Exception
*/
public function __construct(?Chronos $expirationDate = null, ?string $name = null)
private function __construct(?string $name = null, ?Chronos $expirationDate = null)
{
$this->key = Uuid::uuid4()->toString();
$this->expirationDate = $expirationDate;
@@ -36,30 +37,21 @@ class ApiKey extends AbstractEntity
$this->roles = new ArrayCollection();
}
public static function withRoles(RoleDefinition ...$roleDefinitions): self
public static function create(): ApiKey
{
$apiKey = new self();
return new self();
}
foreach ($roleDefinitions as $roleDefinition) {
public static function fromMeta(ApiKeyMeta $meta): self
{
$apiKey = new self($meta->name(), $meta->expirationDate());
foreach ($meta->roleDefinitions() as $roleDefinition) {
$apiKey->registerRole($roleDefinition);
}
return $apiKey;
}
public static function withKey(string $key, ?Chronos $expirationDate = null, ?string $name = null): self
{
$apiKey = new self($expirationDate, $name);
$apiKey->key = $key;
return $apiKey;
}
public static function withName(string $name): self
{
return new self(null, $name);
}
public function getExpirationDate(): ?Chronos
{
return $this->expirationDate;