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;
}
}

View File

@@ -44,9 +44,9 @@ class DomainRedirectsRequestTest extends TestCase
$notFound = $request->toNotFoundRedirects($defaults);
self::assertEquals($expectedAuthority, $request->authority());
self::assertEquals($expectedBaseUrlRedirect, $notFound->baseUrlRedirect());
self::assertEquals($expectedRegular404Redirect, $notFound->regular404Redirect());
self::assertEquals($expectedInvalidShortUrlRedirect, $notFound->invalidShortUrlRedirect());
self::assertEquals($expectedBaseUrlRedirect, $notFound->baseUrlRedirect);
self::assertEquals($expectedRegular404Redirect, $notFound->regular404Redirect);
self::assertEquals($expectedInvalidShortUrlRedirect, $notFound->invalidShortUrlRedirect);
}
public function provideValidData(): iterable

View File

@@ -36,9 +36,11 @@ class ResolveShortUrlActionTest extends TestCase
{
$shortCode = 'abc123';
$apiKey = ApiKey::create();
$this->urlResolver->resolveShortUrl(new ShortUrlIdentifier($shortCode), $apiKey)->willReturn(
ShortUrl::withLongUrl('http://domain.com/foo/bar'),
)->shouldBeCalledOnce();
$this->urlResolver->resolveShortUrl(
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
$apiKey,
)->willReturn(ShortUrl::withLongUrl('http://domain.com/foo/bar'))
->shouldBeCalledOnce();
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode)->withAttribute(ApiKey::class, $apiKey);
$response = $this->action->handle($request);

View File

@@ -38,7 +38,7 @@ class ShortUrlVisitsActionTest extends TestCase
{
$shortCode = 'abc123';
$this->visitsHelper->visitsForShortUrl(
new ShortUrlIdentifier($shortCode),
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
Argument::type(VisitsParams::class),
Argument::type(ApiKey::class),
)->willReturn(new Paginator(new ArrayAdapter([])))
@@ -52,7 +52,7 @@ class ShortUrlVisitsActionTest extends TestCase
public function paramsAreReadFromQuery(): void
{
$shortCode = 'abc123';
$this->visitsHelper->visitsForShortUrl(new ShortUrlIdentifier($shortCode), new VisitsParams(
$this->visitsHelper->visitsForShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode), new VisitsParams(
DateRange::withEndDate(Chronos::parse('2016-01-01 00:00:00')),
3,
10,

View File

@@ -16,8 +16,8 @@ class RoleDefinitionTest extends TestCase
{
$definition = RoleDefinition::forAuthoredShortUrls();
self::assertEquals(Role::AUTHORED_SHORT_URLS, $definition->roleName());
self::assertEquals([], $definition->meta());
self::assertEquals(Role::AUTHORED_SHORT_URLS, $definition->roleName);
self::assertEquals([], $definition->meta);
}
/** @test */
@@ -26,7 +26,7 @@ class RoleDefinitionTest extends TestCase
$domain = Domain::withAuthority('foo.com')->setId('123');
$definition = RoleDefinition::forDomain($domain);
self::assertEquals(Role::DOMAIN_SPECIFIC, $definition->roleName());
self::assertEquals(['domain_id' => '123', 'authority' => 'foo.com'], $definition->meta());
self::assertEquals(Role::DOMAIN_SPECIFIC, $definition->roleName);
self::assertEquals(['domain_id' => '123', 'authority' => 'foo.com'], $definition->meta);
}
}

View File

@@ -46,7 +46,7 @@ class ApiKeyServiceTest extends TestCase
self::assertEquals($date, $key->getExpirationDate());
self::assertEquals($name, $key->name());
foreach ($roles as $roleDefinition) {
self::assertTrue($key->hasRole($roleDefinition->roleName()));
self::assertTrue($key->hasRole($roleDefinition->roleName));
}
}
@@ -77,7 +77,7 @@ class ApiKeyServiceTest extends TestCase
$result = $this->service->check('12345');
self::assertFalse($result->isValid());
self::assertSame($invalidKey, $result->apiKey());
self::assertSame($invalidKey, $result->apiKey);
}
public function provideInvalidApiKeys(): iterable
@@ -100,7 +100,7 @@ class ApiKeyServiceTest extends TestCase
$result = $this->service->check('12345');
self::assertTrue($result->isValid());
self::assertSame($apiKey, $result->apiKey());
self::assertSame($apiKey, $result->apiKey);
}
/** @test */