Updated to readonly public props on as many models as possible

This commit is contained in:
Alejandro Celaya
2022-04-23 14:00:47 +02:00
parent e79391907a
commit bca3e62ced
74 changed files with 249 additions and 494 deletions

View File

@@ -32,7 +32,7 @@ class ListTagsAction extends AbstractRestAction
$params = TagsParams::fromRawData($request->getQueryParams());
$apiKey = AuthenticationMiddleware::apiKeyFromRequest($request);
if (! $params->withStats()) {
if (! $params->withStats) {
return new JsonResponse([
'tags' => $this->serializePaginator($this->tagService->listTags($params, $apiKey)),
]);
@@ -41,7 +41,7 @@ class ListTagsAction extends AbstractRestAction
// This part is deprecated. To get tags with stats, the /tags/stats endpoint should be used instead
$tagsInfo = $this->tagService->tagsInfo($params, $apiKey);
$rawTags = $this->serializePaginator($tagsInfo, null, 'stats');
$rawTags['data'] = map($tagsInfo, static fn (TagInfo $info) => $info->tag());
$rawTags['data'] = map($tagsInfo, static fn (TagInfo $info) => $info->tag);
return new JsonResponse(['tags' => $rawTags]);
}

View File

@@ -8,11 +8,13 @@ use Cake\Chronos\Chronos;
final class ApiKeyMeta
{
/**
* @param RoleDefinition[] $roleDefinitions
*/
private function __construct(
private ?string $name,
private ?Chronos $expirationDate,
/** @var RoleDefinition[] */
private array $roleDefinitions,
public readonly ?string $name,
public readonly ?Chronos $expirationDate,
public readonly array $roleDefinitions,
) {
}
@@ -35,22 +37,4 @@ final class ApiKeyMeta
{
return new self(null, null, $roleDefinitions);
}
public function name(): ?string
{
return $this->name;
}
public function expirationDate(): ?Chronos
{
return $this->expirationDate;
}
/**
* @return RoleDefinition[]
*/
public function roleDefinitions(): array
{
return $this->roleDefinitions;
}
}

View File

@@ -9,7 +9,7 @@ use Shlinkio\Shlink\Rest\ApiKey\Role;
final class RoleDefinition
{
private function __construct(private string $roleName, private array $meta)
private function __construct(public readonly string $roleName, public readonly array $meta)
{
}
@@ -25,14 +25,4 @@ final class RoleDefinition
['domain_id' => $domain->getId(), 'authority' => $domain->getAuthority()],
);
}
public function roleName(): string
{
return $this->roleName;
}
public function meta(): array
{
return $this->meta;
}
}

View File

@@ -44,8 +44,8 @@ class ApiKey extends AbstractEntity
public static function fromMeta(ApiKeyMeta $meta): self
{
$apiKey = new self($meta->name(), $meta->expirationDate());
foreach ($meta->roleDefinitions() as $roleDefinition) {
$apiKey = new self($meta->name, $meta->expirationDate);
foreach ($meta->roleDefinitions as $roleDefinition) {
$apiKey->registerRole($roleDefinition);
}
@@ -137,21 +137,16 @@ class ApiKey extends AbstractEntity
public function registerRole(RoleDefinition $roleDefinition): void
{
$roleName = $roleDefinition->roleName();
$meta = $roleDefinition->meta();
$roleName = $roleDefinition->roleName;
$meta = $roleDefinition->meta;
if ($this->hasRole($roleName)) {
/** @var ApiKeyRole $role */
$role = $this->roles->get($roleName);
$role->updateMeta($meta);
} else {
$role = new ApiKeyRole($roleDefinition->roleName(), $roleDefinition->meta(), $this);
$role = new ApiKeyRole($roleDefinition->roleName, $roleDefinition->meta, $this);
$this->roles[$roleName] = $role;
}
}
public function removeRole(string $roleName): void
{
$this->roles->remove($roleName);
}
}

View File

@@ -49,7 +49,7 @@ class AuthenticationMiddleware implements MiddlewareInterface, StatusCodeInterfa
throw VerifyAuthenticationException::forInvalidApiKey();
}
return $handler->handle($request->withAttribute(ApiKey::class, $result->apiKey()));
return $handler->handle($request->withAttribute(ApiKey::class, $result->apiKey));
}
public static function apiKeyFromRequest(Request $request): ApiKey

View File

@@ -8,7 +8,7 @@ use Shlinkio\Shlink\Rest\Entity\ApiKey;
final class ApiKeyCheckResult
{
public function __construct(private ?ApiKey $apiKey = null)
public function __construct(public readonly ?ApiKey $apiKey = null)
{
}
@@ -16,9 +16,4 @@ final class ApiKeyCheckResult
{
return $this->apiKey !== null && $this->apiKey->isValid();
}
public function apiKey(): ?ApiKey
{
return $this->apiKey;
}
}