| Total Complexity | 7 |
| Total Lines | 105 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 8 | class Adapter |
||
| 9 | { |
||
| 10 | const _REDIS = 'redis'; |
||
| 11 | const _HOST = 'host'; |
||
| 12 | const _PORT = 'port'; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * redis adapter config |
||
| 16 | * |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | protected $config; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * redis instance |
||
| 23 | * |
||
| 24 | * @var \Redis |
||
| 25 | */ |
||
| 26 | protected $instance; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * error |
||
| 30 | * |
||
| 31 | * @var Boolean |
||
| 32 | */ |
||
| 33 | protected $error; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * error code |
||
| 37 | * |
||
| 38 | * @var int |
||
| 39 | */ |
||
| 40 | protected $errorCode; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * error message |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $errorMessage; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * instanciate |
||
| 51 | * |
||
| 52 | * @param Config $config |
||
| 53 | */ |
||
| 54 | public function __construct(Config $config) |
||
| 55 | { |
||
| 56 | $this->config = $config->getSettings(self::_REDIS); |
||
| 57 | $this->error = false; |
||
| 58 | $this->errorCode = 0; |
||
| 59 | $this->errorMessage = ''; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * return redis client instance |
||
| 64 | * |
||
| 65 | * @return Redis |
||
| 66 | */ |
||
| 67 | public function getClient(): Redis |
||
| 68 | { |
||
| 69 | if (is_null($this->instance)) { |
||
| 70 | try { |
||
| 71 | $this->instance = new Redis(); |
||
| 72 | $this->instance->connect( |
||
| 73 | $this->config[self::_HOST], |
||
| 74 | $this->config[self::_PORT] |
||
| 75 | ); |
||
| 76 | } catch (\RedisException $e) { |
||
| 77 | $this->error = true; |
||
| 78 | $this->errorCode = $e->getCode(); |
||
| 79 | $this->errorMessage = $e->getMessage(); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | return $this->instance; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * return true if error |
||
| 87 | * |
||
| 88 | * @return boolean |
||
| 89 | */ |
||
| 90 | public function isError(): bool |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * return code error |
||
| 97 | * |
||
| 98 | * @return int |
||
| 99 | */ |
||
| 100 | public function getErrorCode(): int |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * return error message |
||
| 107 | * |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | public function getErrorMessage(): string |
||
| 113 | } |
||
| 114 | } |
||
| 115 |