Skip to content

Commit

Permalink
refactor getting authentication details for the Redis cluster
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Brabants <[email protected]>
  • Loading branch information
robin-brabants committed Nov 20, 2024
1 parent 11115cc commit 89ff261
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
5 changes: 5 additions & 0 deletions src/Exception/InvalidRedisClusterConfigurationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ public static function fromInvalidSeedsConfiguration(string $seed): self
)
);
}

public static function fromMissingRequiredPassword(): self
{
return new self('If a Redis user is provided, a password has to be configured as well.');
}
}
22 changes: 5 additions & 17 deletions src/RedisClusterOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ final class RedisClusterOptions extends AdapterOptions
/** @psalm-var array<positive-int,mixed> */
private array $libOptions = [];

private ?string $username = null;
private string $user = '';

private string $password = '';

Expand Down Expand Up @@ -250,17 +250,17 @@ public function getLibOption(int $option, mixed $default = null): mixed
return $this->libOptions[$option] ?? $default;
}

public function getUsername(): ?string
public function getUser(): string
{
return $this->username;
return $this->user;
}

/**
* @psalm-param non-empty-string $username

Check failure on line 259 in src/RedisClusterOptions.php

View workflow job for this annotation

GitHub Actions / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

UnusedDocblockParam

src/RedisClusterOptions.php:259:38: UnusedDocblockParam: Docblock parameter $username in docblock for Laminas\Cache\Storage\Adapter\RedisClusterOptions::setUser does not have a counterpart in signature parameter list (see https://psalm.dev/319)
*/
public function setUsername(string $username): void
public function setUser(string $user): void

Check failure on line 261 in src/RedisClusterOptions.php

View workflow job for this annotation

GitHub Actions / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

PossiblyUnusedMethod

src/RedisClusterOptions.php:261:21: PossiblyUnusedMethod: Cannot find any calls to method Laminas\Cache\Storage\Adapter\RedisClusterOptions::setUser (see https://psalm.dev/087)
{
$this->username = $username;
$this->user = $user;
}

public function getPassword(): string
Expand All @@ -276,18 +276,6 @@ public function setPassword(string $password): void
$this->password = $password;
}

/**
* @return array<int,string|null>|string|null
*/
public function getAuthentication(): array|string|null
{
$password = $this->password === '' ? null : $this->password;
if ($this->username !== null && $this->username !== '') {
return [$this->username, $password];
}
return $password;
}

public function getSslContext(): ?SslContext
{
return $this->sslContext;
Expand Down
20 changes: 17 additions & 3 deletions src/RedisClusterResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,26 @@ public function getResource(): RedisClusterFromExtension

private function createRedisResource(RedisClusterOptions $options): RedisClusterFromExtension
{
$user = $options->getUser();
$password = $options->getPassword();

if ($user !== '' && $password !== '') {
$authentication = [$user, $password];
} elseif ($user !== '' && $password === '') {
throw InvalidRedisClusterConfigurationException::fromMissingRequiredPassword();
} elseif ($user === '' && $password !== '') {
$authentication = [$password];
} else {
$authentication = null;
}

if ($options->hasName()) {
return $this->createRedisResourceFromName(
$options->getName(),
$options->getTimeout(),
$options->getReadTimeout(),
$options->isPersistent(),
$options->getAuthentication(),
$authentication,
$options->getSslContext()
);
}
Expand All @@ -72,20 +85,21 @@ private function createRedisResource(RedisClusterOptions $options): RedisCluster
$options->getTimeout(),

Check failure on line 85 in src/RedisClusterResourceManager.php

View workflow job for this annotation

GitHub Actions / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

InvalidArgument

src/RedisClusterResourceManager.php:85:13: InvalidArgument: Argument 6 of RedisCluster::__construct expects null|string, but list{0: non-empty-string, 1?: non-empty-string}|null provided (see https://psalm.dev/004)
$options->getReadTimeout(),
$options->isPersistent(),
$options->getAuthentication(),
$authentication,
$options->getSslContext()?->toSslContextArray()
);
}

/**
* @psalm-param non-empty-string $name
* @psalm-param array{0: non-empty-string, 1?: non-empty-string}|null $fallbackAuthentication
*/
private function createRedisResourceFromName(
string $name,
float $fallbackTimeout,
float $fallbackReadTimeout,
bool $persistent,
array|string|null $fallbackAuthentication,
array|null $fallbackAuthentication,
?SslContext $sslContext
): RedisClusterFromExtension {
try {
Expand Down
6 changes: 3 additions & 3 deletions test/unit/RedisClusterOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function testCanHandleOptionsWithNodename(): void
self::assertEquals(2.0, $options->getReadTimeout());
self::assertFalse($options->isPersistent());
self::assertEquals('1.0', $options->getRedisVersion());
self::assertNull($options->getUsername());
self::assertEmpty($options->getUser());
self::assertEquals('secret', $options->getPassword());
}

Expand All @@ -84,15 +84,15 @@ public function testCanHandleOptionsWithSeeds(): void
'read_timeout' => 2.0,
'persistent' => false,
'redis_version' => '1.0',
'username' => 'user',
'user' => 'user',

Check failure on line 87 in test/unit/RedisClusterOptionsTest.php

View workflow job for this annotation

GitHub Actions / QA Checks (PHPCodeSniffer [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-ac...

Expected 10 spaces before double arrow; 6 found
]);

self::assertEquals(['localhost:1234'], $options->getSeeds());
self::assertEquals(1.0, $options->getTimeout());
self::assertEquals(2.0, $options->getReadTimeout());
self::assertFalse($options->isPersistent());
self::assertEquals('1.0', $options->getRedisVersion());
self::assertEquals('user', $options->getUsername());
self::assertEquals('user', $options->getUser());
self::assertEquals('', $options->getPassword());
}

Expand Down

0 comments on commit 89ff261

Please sign in to comment.