mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-09 08:43:13 +08:00
Extracted ApiKey metadata to the ApiKeyMeta object
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user