Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions src/Cluster/Transport/EventField.php

This file was deleted.

17 changes: 10 additions & 7 deletions src/Cluster/Transport/Pdo/PdoInvalidationTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Infocyph\CacheLayer\Cluster\Event\InvalidationEvent;
use Infocyph\CacheLayer\Cluster\Event\InvalidationEventType;
use Infocyph\CacheLayer\Cluster\Exception\ClusterTransportException;
use Infocyph\CacheLayer\Cluster\Transport\EventField;
use Infocyph\CacheLayer\Cluster\Transport\InvalidationTransportInspectorInterface;
use Infocyph\CacheLayer\Cluster\Transport\TransactionalInvalidationTransportInterface;
use PDO;
Expand Down Expand Up @@ -330,12 +329,16 @@ private function pruneSql(): string

private function requiredString(mixed $row, string $key): string
{
return EventField::requiredString(
$row,
$key,
'Invalid PDO transport event row.',
"Invalid {$key} returned by PDO transport.",
);
if (!is_array($row)) {
throw new ClusterTransportException('Invalid PDO transport event row.');
}

$value = $row[$key] ?? null;
if (!is_string($value) || $value === '') {
throw new ClusterTransportException("Invalid {$key} returned by PDO transport.");
}

return $value;
}

private function timestampFromValue(mixed $value): int
Expand Down
16 changes: 10 additions & 6 deletions src/Cluster/Transport/RedisStreamInvalidationTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,16 @@ private function eventFromFields(string $id, array $fields): InvalidationEvent

private function field(mixed $fields, string $name): string
{
return EventField::requiredString(
$fields,
$name,
'Redis Stream event fields must be an array.',
"Redis Stream event has an invalid {$name} field.",
);
if (!is_array($fields)) {
throw new ClusterTransportException('Redis Stream event fields must be an array.');
}

$value = $fields[$name] ?? null;
if (!is_string($value) || $value === '') {
throw new ClusterTransportException("Redis Stream event has an invalid {$name} field.");
}

return $value;
}

/**
Expand Down
28 changes: 23 additions & 5 deletions src/Support/RedisConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,37 @@ final class RedisConnection
public static function connect(string $dsn): \Redis
{
$parts = parse_url($dsn);
if (!is_array($parts)) {
$scheme = is_array($parts) ? $parts['scheme'] ?? null : null;
$host = is_array($parts) ? $parts['host'] ?? null : null;
if (
!is_string($scheme)
|| !in_array($scheme, ['redis', 'valkey'], true)
|| !is_string($host)
|| $host === ''
) {
throw new InvalidArgumentException('Invalid Redis-compatible DSN.');
}

$path = $parts['path'] ?? '';
$database = $path === '' || $path === '/'
? null
: ltrim($path, '/');
if ($database !== null && !ctype_digit($database)) {
throw new InvalidArgumentException('Invalid Redis-compatible DSN.');
}

$port = $parts['port'] ?? 6379;
if ($port === 0) {
throw new InvalidArgumentException('Invalid Redis-compatible DSN.');
}

$connection = new \Redis();
$host = is_string($parts['host'] ?? null) ? $parts['host'] : '127.0.0.1';
$port = is_int($parts['port'] ?? null) ? $parts['port'] : 6379;
$connection->connect($host, $port);
if (is_string($parts['pass'] ?? null) && $parts['pass'] !== '') {
$connection->auth($parts['pass']);
}
if (is_string($parts['path'] ?? null) && $parts['path'] !== '/') {
$connection->select((int) ltrim($parts['path'], '/'));
if ($database !== null) {
$connection->select((int) $database);
}

return $connection;
Expand Down
12 changes: 9 additions & 3 deletions tests/Support/RedisConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@

use Infocyph\CacheLayer\Support\RedisConnection;

test('redis connection rejects malformed DSNs before creating a client', function () {
expect(fn () => RedisConnection::connect('redis://'))
test('redis connection rejects malformed DSNs before creating a client', function (string $dsn) {
expect(fn () => RedisConnection::connect($dsn))
->toThrow(\InvalidArgumentException::class, 'Invalid Redis-compatible DSN.');
});
})->with([
'missing host' => 'redis://',
'missing scheme' => '127.0.0.1:6379',
'unsupported scheme' => 'http://127.0.0.1:6379',
'invalid database' => 'redis://127.0.0.1/database',
'invalid port' => 'redis://127.0.0.1:0',
]);