|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * Copyright MacFJA |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 9 | + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the |
| 10 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to |
| 11 | + * permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the |
| 14 | + * Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 17 | + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 18 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 19 | + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | + */ |
| 21 | + |
| 22 | +namespace MacFJA\RediSearch\Redis\Client; |
| 23 | + |
| 24 | +use MacFJA\RediSearch\Redis\Client; |
| 25 | +use MacFJA\RediSearch\Redis\Command; |
| 26 | +use RuntimeException; |
| 27 | + |
| 28 | +/** |
| 29 | + * @codeCoverageIgnore |
| 30 | + */ |
| 31 | +class TinyRedisClient extends AbstractClient |
| 32 | +{ |
| 33 | + /** @var \TinyRedisClient */ |
| 34 | + private $redis; |
| 35 | + |
| 36 | + public function __construct(\TinyRedisClient $redis) |
| 37 | + { |
| 38 | + if (!self::supports($redis)) { |
| 39 | + throw new RuntimeException($this->getMissingMessage( |
| 40 | + 'TinyRedis', |
| 41 | + false, |
| 42 | + [\TinyRedisClient::class => ['__call']] |
| 43 | + )); |
| 44 | + } |
| 45 | + $this->redis = $redis; |
| 46 | + } |
| 47 | + |
| 48 | + public function pipeline(Command ...$commands): array |
| 49 | + { |
| 50 | + false === static::$disableNotice |
| 51 | + && trigger_error('Warning, \\TinyRedisClient don\'t use a real Redis Pipeline', E_USER_NOTICE); |
| 52 | + |
| 53 | + return array_map(function (Command $command) { |
| 54 | + return $this->execute($command); |
| 55 | + }, $commands); |
| 56 | + } |
| 57 | + |
| 58 | + public function execute(Command $command) |
| 59 | + { |
| 60 | + $result = $this->redis->__call($command->getId(), $command->getArguments()); |
| 61 | + |
| 62 | + return $command->parseResponse($result); |
| 63 | + } |
| 64 | + |
| 65 | + public function executeRaw(...$args) |
| 66 | + { |
| 67 | + $command = array_shift($args); |
| 68 | + |
| 69 | + return $this->redis->__call($command, $args); |
| 70 | + } |
| 71 | + |
| 72 | + public static function supports($redis): bool |
| 73 | + { |
| 74 | + return $redis instanceof \TinyRedisClient |
| 75 | + && method_exists($redis, '__call'); |
| 76 | + } |
| 77 | + |
| 78 | + public static function make($redis): Client |
| 79 | + { |
| 80 | + return new self($redis); |
| 81 | + } |
| 82 | + |
| 83 | + protected function doPipeline(Command ...$commands): array |
| 84 | + { |
| 85 | + return []; |
| 86 | + } |
| 87 | +} |
0 commit comments