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