Lines
80.00%
32 / 40
Methods
33.33%
1 / 3
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| configure | 71.42% | 5 / 7 | 0.00% | 0 / 1 | 4.37 | ||
| validateConnection | 68.42% | 13 / 19 | 0.00% | 0 / 1 | 4.50 | ||
| buildSocketTarget | 100.00% | 14 / 14 | 100.00% | 1 / 1 | 7 | ||
| 24 | class RedisSessionHandler | |
| 25 | { | |
| 26 | public const string DEFAULT_DSN = 'tcp://redis:6379?database=0'; | |
| 27 | ||
| 28 | /* @mago-expect lint:no-ini-set - registering the Redis session handler requires runtime session settings */ | |
| 29 | public static function configure(string $dsn = '', bool $validate = false): void | |
| 30 | { | |
| 31 | if (!extension_loaded('redis')) { | |
| 32 | throw new RuntimeException('Redis session handler requires the PHP redis extension (ext-redis).'); | |
| 33 | } | |
| 34 | ||
| 35 | $redisDsn = trim($dsn) !== '' ? trim($dsn) : self::DEFAULT_DSN; | |
| 36 | ||
| 37 | if ($validate) { | |
| 38 | self::validateConnection($redisDsn); | |
| 39 | } | |
| 40 | ||
| 41 | ini_set('session.save_handler', value: 'redis'); | |
| 42 | ini_set('session.save_path', value: $redisDsn); | |
| 43 | } | |
| 44 | ||
| 45 | private const float MAX_TIMEOUT_SECONDS = 3.0; | |
| 46 | ||
| 47 | public static function validateConnection(string $dsn, float $timeoutSeconds = 1.0): void | |
| 48 | { | |
| 49 | $timeoutSeconds = min(max($timeoutSeconds, 0.1), self::MAX_TIMEOUT_SECONDS); | |
| 50 | ||
| 51 | [$socketTarget, $displayTarget] = self::buildSocketTarget($dsn); | |
| 52 | ||
| 53 | $errno = 0; | |
| 54 | $errorString = ''; | |
| 55 | /* @mago-expect lint:no-error-control-operator - connection failure is reported via $errno/$errorString below */ | |
| 56 | $connection = @stream_socket_client( | |
| 57 | $socketTarget, | |
| 58 | $errno, | |
| 59 | $errorString, | |
| 60 | $timeoutSeconds, | |
| 61 | STREAM_CLIENT_CONNECT, | |
| 62 | ); | |
| 63 | ||
| 64 | if ($connection === false) { | |
| 65 | throw new RuntimeException(sprintf('Redis connection failed for %s.', $displayTarget)); | |
| 66 | } | |
| 67 | ||
| 68 | // Verify the service speaks Redis by sending PING and checking for +PONG | |
| 69 | fwrite($connection, data: "PING\r\n"); | |
| 70 | stream_set_timeout($connection, (int) ceil($timeoutSeconds)); | |
| 71 | $response = fgets($connection, length: 128); | |
| 72 | fclose($connection); | |
| 73 | ||
| 74 | if ($response === false || !str_starts_with(trim($response), '+PONG')) { | |
| 75 | throw new RuntimeException(sprintf('Redis connection failed for %s.', $displayTarget)); | |
| 76 | } | |
| 77 | } | |
| 78 | ||
| 79 | /** | |
| 80 | * @return array{0: string, 1: string} | |
| 81 | */ | |
| 82 | private static function buildSocketTarget(string $dsn): array | |
| 83 | { | |
| 84 | $parsedUrl = parse_url($dsn); | |
| 85 | if (!is_array($parsedUrl) || !array_key_exists('scheme', $parsedUrl)) { | |
| 86 | throw new RuntimeException('Invalid Redis DSN for sessions.'); | |
| 87 | } | |
| 88 | ||
| 89 | $scheme = strtolower($parsedUrl['scheme']); | |
| 90 | if ($scheme === 'redis' || $scheme === 'tcp') { | |
| 91 | $host = $parsedUrl['host'] ?? '127.0.0.1'; | |
| 92 | $port = (int) ($parsedUrl['port'] ?? 6379); | |
| 93 | return [sprintf('tcp://%s:%d', $host, $port), sprintf('%s:%d', $host, $port)]; | |
| 94 | } | |
| 95 | ||
| 96 | if ($scheme === 'unix') { | |
| 97 | $path = $parsedUrl['path'] ?? ''; | |
| 98 | if ($path === '') { | |
| 99 | throw new RuntimeException('Invalid Redis unix socket DSN for sessions.'); | |
| 100 | } | |
| 101 | ||
| 102 | return ['unix://' . $path, $path]; | |
| 103 | } | |
| 104 | ||
| 105 | throw new RuntimeException(sprintf('Unsupported Redis DSN scheme "%s" for sessions.', $scheme)); | |
| 106 | } | |
| 107 | } |