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

@@ -7,6 +7,7 @@ namespace Shlinkio\Shlink\Rest\Service;
use Cake\Chronos\Chronos;
use Doctrine\ORM\EntityManagerInterface;
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta;
use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
@@ -26,7 +27,7 @@ class ApiKeyService implements ApiKeyServiceInterface
?string $name = null,
RoleDefinition ...$roleDefinitions
): ApiKey {
$key = new ApiKey($expirationDate, $name);
$key = $this->buildApiKeyWithParams($expirationDate, $name);
foreach ($roleDefinitions as $definition) {
$key->registerRole($definition);
}
@@ -37,6 +38,24 @@ class ApiKeyService implements ApiKeyServiceInterface
return $key;
}
private function buildApiKeyWithParams(?Chronos $expirationDate, ?string $name): ApiKey
{
// TODO Use match expression when migrating to PHP8
if ($expirationDate === null && $name === null) {
return ApiKey::create();
}
if ($expirationDate !== null && $name !== null) {
return ApiKey::fromMeta(ApiKeyMeta::withNameAndExpirationDate($name, $expirationDate));
}
if ($name === null) {
return ApiKey::fromMeta(ApiKeyMeta::withExpirationDate($expirationDate));
}
return ApiKey::fromMeta(ApiKeyMeta::withName($name));
}
public function check(string $key): ApiKeyCheckResult
{
$apiKey = $this->getByKey($key);