From b070767d0a318fc706ce28c3c29f73a404bbe95f Mon Sep 17 00:00:00 2001 From: Michael Schiemer Date: Mon, 3 Nov 2025 00:43:28 +0100 Subject: [PATCH] refactor: simplify Redis configuration initialization - Use RedisConfig::fromEnvironment() in LoggerInitializer - Remove fallback logic in QueueInitializer, always use connection pool - Make RedisConfig constructor private - Clean up Redis connection error message --- src/Framework/Logging/LoggerInitializer.php | 2 +- src/Framework/Queue/QueueInitializer.php | 16 +++------------- src/Framework/Redis/RedisConfig.php | 2 +- src/Framework/Redis/RedisConnection.php | 2 +- 4 files changed, 6 insertions(+), 16 deletions(-) diff --git a/src/Framework/Logging/LoggerInitializer.php b/src/Framework/Logging/LoggerInitializer.php index 9d86956f..97f19929 100644 --- a/src/Framework/Logging/LoggerInitializer.php +++ b/src/Framework/Logging/LoggerInitializer.php @@ -121,7 +121,7 @@ final readonly class LoggerInitializer */ private function createQueue(Environment $env): Queue { - $redisConfig = new RedisConfig(host: $env->getString(EnvKey::REDIS_HOST, 'redis'), database: 2); + $redisConfig = RedisConfig::fromEnvironment($env); $redisConnection = new RedisConnection($redisConfig, 'queue'); return new RedisQueue($redisConnection, 'commands'); diff --git a/src/Framework/Queue/QueueInitializer.php b/src/Framework/Queue/QueueInitializer.php index 6a3c0b8d..523fda5d 100644 --- a/src/Framework/Queue/QueueInitializer.php +++ b/src/Framework/Queue/QueueInitializer.php @@ -32,19 +32,9 @@ final readonly class QueueInitializer } // Try to use Redis connection pool if available, otherwise create direct connection - $redisConnection = null; - if ($this->container->has(RedisConnectionPool::class)) { - $pool = $this->container->get(RedisConnectionPool::class); - $redisConnection = $pool->getQueueConnection(); - } else { - // Fallback to direct connection creation - $redisConfig = new RedisConfig( - host: 'redis', - port: 6379, - database: 2 // Use DB 2 for queue - ); - $redisConnection = new RedisConnection($redisConfig, 'queue'); - } + + $pool = $this->container->get(RedisConnectionPool::class); + $redisConnection = $pool->getQueueConnection(); return new RedisQueue( connection: $redisConnection, diff --git a/src/Framework/Redis/RedisConfig.php b/src/Framework/Redis/RedisConfig.php index 76fae191..0f6c3418 100644 --- a/src/Framework/Redis/RedisConfig.php +++ b/src/Framework/Redis/RedisConfig.php @@ -12,7 +12,7 @@ use App\Framework\Config\EnvKey; */ final readonly class RedisConfig { - public function __construct( + private function __construct( public string $host = 'redis', public int $port = 6379, public ?string $password = null, diff --git a/src/Framework/Redis/RedisConnection.php b/src/Framework/Redis/RedisConnection.php index 8aa6591a..6f6e63a0 100644 --- a/src/Framework/Redis/RedisConnection.php +++ b/src/Framework/Redis/RedisConnection.php @@ -112,7 +112,7 @@ final class RedisConnection implements RedisConnectionInterface $this->connected = false; throw new RedisConnectionException( - "Failed to connect to Redis ({$this->name}): " . $e->getMessage() . " with Host: {$this->config->host} and Password: {$this->config->password} should be {$_ENV['REDIS_HOST']} and {$_ENV['REDIS_PASSWORD']}", + "Failed to connect to Redis ({$this->name}): " . $e->getMessage() . " with Host: {$this->config->host} and Password: {$this->config->password}", previous: $e ); }