| Total Complexity | 8 |
| Total Lines | 124 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| 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 server hostname |
||
| 16 | * |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | protected $host; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * redis server port |
||
| 23 | * |
||
| 24 | * @var integer |
||
| 25 | */ |
||
| 26 | protected $port; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * redis instance |
||
| 30 | * |
||
| 31 | * @var \Redis |
||
| 32 | */ |
||
| 33 | protected $instance; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * error |
||
| 37 | * |
||
| 38 | * @var Boolean |
||
| 39 | */ |
||
| 40 | protected $error; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * error code |
||
| 44 | * |
||
| 45 | * @var int |
||
| 46 | */ |
||
| 47 | protected $errorCode; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * error message |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | protected $errorMessage; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * instanciate |
||
| 58 | * |
||
| 59 | * @param Config $config |
||
| 60 | */ |
||
| 61 | 11 | public function __construct(Config $config) |
|
| 62 | { |
||
| 63 | 11 | $config = $config->getSettings(self::_REDIS); |
|
| 64 | 11 | $this->applyConfig($config[self::_HOST], $config[self::_PORT]); |
|
| 65 | 11 | $this->error = false; |
|
| 66 | 11 | $this->errorCode = 0; |
|
| 67 | 11 | $this->errorMessage = ''; |
|
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * return redis client instance |
||
| 72 | * |
||
| 73 | * @return Redis |
||
| 74 | */ |
||
| 75 | 3 | public function getClient(): Redis |
|
| 76 | { |
||
| 77 | 3 | if (is_null($this->instance)) { |
|
| 78 | try { |
||
| 79 | 3 | $this->instance = new Redis(); |
|
| 80 | 3 | @$this->instance->connect($this->host, $this->port); |
|
|
|
|||
| 81 | 2 | } catch (\RedisException $e) { |
|
| 82 | 2 | $this->error = true; |
|
| 83 | 2 | $this->errorMessage = $e->getMessage(); |
|
| 84 | 2 | $this->errorCode = 1; |
|
| 85 | } |
||
| 86 | } |
||
| 87 | 3 | return $this->instance; |
|
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * return true if error |
||
| 92 | * |
||
| 93 | * @return boolean |
||
| 94 | */ |
||
| 95 | 3 | public function isError(): bool |
|
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * return code error |
||
| 102 | * |
||
| 103 | * @return int |
||
| 104 | */ |
||
| 105 | 3 | public function getErrorCode(): int |
|
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * return error message |
||
| 112 | * |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | 3 | public function getErrorMessage(): string |
|
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * set redis server config |
||
| 122 | * |
||
| 123 | * @param string $host |
||
| 124 | * @param integer $port |
||
| 125 | * @return Adapter |
||
| 126 | */ |
||
| 127 | 1 | protected function applyConfig(string $host, int $port): Adapter |
|
| 132 | } |
||
| 133 | } |
||
| 134 |
If you suppress an error, we recommend checking for the error condition explicitly: