Skip to content

Commit

Permalink
refactor: SslContext and its instantiation
Browse files Browse the repository at this point in the history
This refactors some of the `SslContext` implementation to have better readability, less complexity (regarding camel case handling, etc.) and explicit context serialization.

Signed-off-by: Maximilian Bösing <[email protected]>
Signed-off-by: Christian Braun <[email protected]>
  • Loading branch information
boesing authored and cbraunVBM committed Apr 19, 2024
1 parent 100a06c commit 62ae4a1
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 238 deletions.
42 changes: 29 additions & 13 deletions src/RedisClusterOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

use Laminas\Cache\Exception\RuntimeException;
use Laminas\Cache\Storage\Adapter\Exception\InvalidRedisClusterConfigurationException;
use Laminas\Stdlib\AbstractOptions;
use Traversable;

use function is_array;
use function iterator_to_array;

final class RedisClusterOptions extends AdapterOptions
{
Expand Down Expand Up @@ -55,7 +58,6 @@ final class RedisClusterOptions extends AdapterOptions

private string $password = '';

/** @psalm-var SslContext|null */
private ?SslContext $sslContext = null;

/**
Expand All @@ -81,6 +83,31 @@ public function __construct($options = null)
}
}

/**
* {@inheritDoc}
*/
public function setFromArray($options)
{
if ($options instanceof AbstractOptions) {
$options = $options->toArray();
} elseif ($options instanceof Traversable) {
$options = iterator_to_array($options);
}

$sslContext = $options['sslContext'] ?? $options['ssl_context'] ?? null;
unset($options['sslContext'], $options['ssl_context']);
if (is_array($sslContext)) {
/** @psalm-suppress MixedArgumentTypeCoercion Trust upstream that they verify the array beforehand. */
$sslContext = SslContext::fromSslContextArray($sslContext);
}

if ($sslContext instanceof SslContext) {
$options['ssl_context'] = $sslContext;
}

return parent::setFromArray($options);
}

public function setTimeout(float $timeout): void
{
$this->timeout = $timeout;
Expand Down Expand Up @@ -228,24 +255,13 @@ public function setPassword(string $password): void
$this->password = $password;
}

/**
* @psalm-return SslContext|null
*/
public function getSslContext(): ?SslContext
{
return $this->sslContext;
}

/**
* @psalm-param array<non-empty-string,mixed>|SslContext|null $sslContext
*/
public function setSslContext(array|SslContext|null $sslContext): void
public function setSslContext(SslContext|null $sslContext): void
{
if (is_array($sslContext)) {
$data = $sslContext;
$sslContext = new SslContext();
$sslContext->exchangeArray($data);
}
$this->sslContext = $sslContext;
}
}
4 changes: 2 additions & 2 deletions src/RedisClusterResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private function createRedisResource(RedisClusterOptions $options): RedisCluster
$options->getReadTimeout(),
$options->isPersistent(),
$password,
$options->getSslContext()?->getArrayCopy()
$options->getSslContext()?->toSslContextArray()
);
}

Expand Down Expand Up @@ -138,7 +138,7 @@ private function createRedisResourceFromName(
$readTimeout,
$persistent,
$password,
$sslContext?->getArrayCopy()
$sslContext?->toSslContextArray()
);
}

Expand Down
Loading

0 comments on commit 62ae4a1

Please sign in to comment.