|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @project phpredis-bloom |
| 4 | + * @author Rafael Campoy <[email protected]> |
| 5 | + * @copyright 2019 Rafael Campoy <[email protected]> |
| 6 | + * @license MIT |
| 7 | + * @link https://github.com/averias/php-rejson |
| 8 | + * |
| 9 | + * Copyright and license information, is included in |
| 10 | + * the LICENSE file that is distributed with this source code. |
| 11 | + */ |
| 12 | + |
| 13 | +namespace Averias\RedisBloom\Adapter; |
| 14 | + |
| 15 | +use Averias\RedisBloom\Connection\ConnectionOptions; |
| 16 | +use Averias\RedisBloom\Enum\ResponseParser; |
| 17 | +use Averias\RedisBloom\Exception\ConnectionException; |
| 18 | +use Averias\RedisBloom\Exception\ResponseException; |
| 19 | +use Redis; |
| 20 | +use Exception; |
| 21 | + |
| 22 | +class RedisClientAdapter implements RedisClientAdapterInterface |
| 23 | +{ |
| 24 | + /** @var Redis */ |
| 25 | + protected $redis; |
| 26 | + |
| 27 | + /** @var ConnectionOptions */ |
| 28 | + protected $connectionOptions; |
| 29 | + |
| 30 | + /** |
| 31 | + * @param Redis $redis |
| 32 | + * @param ConnectionOptions $connectionOptions |
| 33 | + * @throws ConnectionException |
| 34 | + */ |
| 35 | + public function __construct(Redis $redis, ConnectionOptions $connectionOptions) |
| 36 | + { |
| 37 | + $this->redis = $redis; |
| 38 | + $this->connectionOptions = $connectionOptions; |
| 39 | + $this->setConnection(); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @param string $command |
| 44 | + * @param string $key |
| 45 | + * @param array $params |
| 46 | + * @return mixed |
| 47 | + * @throws ResponseException |
| 48 | + */ |
| 49 | + public function executeBloomCommand(string $command, string $key, array $params = []) |
| 50 | + { |
| 51 | + $response = $this->executeRawCommand($command, $key, ...$params); |
| 52 | + |
| 53 | + if ($response === false) { |
| 54 | + $error = $this->redis->getLastError() ?? 'unknown'; |
| 55 | + throw new ResponseException( |
| 56 | + sprintf("something was wrong when executing %s command, possible reasons: %s", $command, $error) |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + return $this->parseResponse($command, $response); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @param string $commandName |
| 65 | + * @param mixed ...$arguments |
| 66 | + * @return mixed |
| 67 | + * @throws ResponseException |
| 68 | + */ |
| 69 | + public function executeRawCommand(string $commandName, ...$arguments) |
| 70 | + { |
| 71 | + try { |
| 72 | + $this->checkConnection(); |
| 73 | + return $this->redis->rawCommand($commandName, ...$arguments); |
| 74 | + } catch (Exception $e) { |
| 75 | + throw new ResponseException( |
| 76 | + sprintf( |
| 77 | + 'the following error happened when executing the command "%s": %s', |
| 78 | + $commandName, |
| 79 | + $e->getMessage() |
| 80 | + ) |
| 81 | + ); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @param string $methodName |
| 87 | + * @param array $arguments |
| 88 | + * @return mixed |
| 89 | + * @throws ResponseException |
| 90 | + */ |
| 91 | + public function executeCommandByName(string $methodName, array $arguments = []) |
| 92 | + { |
| 93 | + try { |
| 94 | + $this->checkConnection(); |
| 95 | + return call_user_func_array([$this->redis, $methodName], $arguments); |
| 96 | + } catch (Exception $e) { |
| 97 | + throw new ResponseException( |
| 98 | + sprintf( |
| 99 | + 'the following error happened when executing command "%s" with param "%s": %s', |
| 100 | + $methodName, |
| 101 | + implode(' ', $arguments), |
| 102 | + $e->getMessage() |
| 103 | + ) |
| 104 | + ); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @throws ConnectionException |
| 110 | + */ |
| 111 | + protected function setConnection(): void |
| 112 | + { |
| 113 | + if (!$this->connect()) { |
| 114 | + throw new ConnectionException( |
| 115 | + sprintf("connection to Redis server failed, reason: %s", $this->redis->getLastError()) |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + if ($this->connectionOptions->getDatabase() != 0) { |
| 120 | + $this->redis->select($this->connectionOptions->getDatabase()); |
| 121 | + } |
| 122 | + |
| 123 | + $this->redis->setOption(Redis::OPT_REPLY_LITERAL, 1); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * @return bool |
| 128 | + */ |
| 129 | + protected function connect(): bool |
| 130 | + { |
| 131 | + $connectionValues = $this->connectionOptions->getConnectionValues(); |
| 132 | + if ($this->connectionOptions->isPersistent()) { |
| 133 | + return $this->redis->pconnect(...$connectionValues); |
| 134 | + } |
| 135 | + |
| 136 | + return $this->redis->connect(...$connectionValues); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * @throws ConnectionException |
| 141 | + */ |
| 142 | + protected function checkConnection(): void |
| 143 | + { |
| 144 | + if (!$this->redis->isConnected()) { |
| 145 | + $this->setConnection(); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + protected function parseResponse(string $command, $response) |
| 150 | + { |
| 151 | + $responseParsers = ResponseParser::RESPONSE_PARSER; |
| 152 | + if (isset($responseParsers[$command])) { |
| 153 | + $className = $responseParsers[$command]; |
| 154 | + $parser = new $className(); |
| 155 | + return $parser->parse($response); |
| 156 | + } |
| 157 | + |
| 158 | + return $response; |
| 159 | + } |
| 160 | +} |
0 commit comments