Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
15 / 15 |
| RedisConnector | |
100.00% |
1 / 1 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
15 / 15 |
| connection | |
100.00% |
1 / 1 |
4 | |
100.00% |
11 / 11 |
|||
| __call | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\DBConnector\Connectors; |
| 4 | |
| 5 | use Illuminate\Support\Facades\Log; |
| 6 | use Qmp\Laravel\DBConnector\Config; |
| 7 | use Predis\Client; |
| 8 | |
| 9 | class RedisConnector implements InterfaceConnector |
| 10 | { |
| 11 | protected $redis; |
| 12 | |
| 13 | protected $options = [ |
| 14 | 'timeout' => 10.0 |
| 15 | ]; |
| 16 | |
| 17 | public function connection(Config $config, $options = []) |
| 18 | { |
| 19 | $redisConfig = $config->toArray(['host', 'password', 'port', 'database']); |
| 20 | if (!isset($redisConfig['database'])) { |
| 21 | $redisConfig['database'] = 0; |
| 22 | } |
| 23 | |
| 24 | $formattedOptions = config('database.redis.options'); |
| 25 | |
| 26 | $formattedOptions = array_merge($formattedOptions ? $formattedOptions : [], $this->options, $options); |
| 27 | |
| 28 | try { |
| 29 | $this->redis = new Client($redisConfig, $formattedOptions); |
| 30 | $this->redis->connect(); |
| 31 | } catch(\Exception $e) { |
| 32 | Log::debug('Unable to connect: ' . $e->getMessage()); |
| 33 | throw new \Exception($e->getMessage()); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | public function __call($method, $arguments) |
| 38 | { |
| 39 | try { |
| 40 | return $this->redis->$method(...$arguments); |
| 41 | } catch(\Exception $e) { |
| 42 | Log::debug("method '$method' does not exist"); |
| 43 | throw new \Exception($e->getMessage()); |
| 44 | } |
| 45 | } |
| 46 | } |