| Total Complexity | 59 |
| Total Lines | 370 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like PhpiredisSocketConnection often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PhpiredisSocketConnection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 49 | class PhpiredisSocketConnection extends AbstractConnection |
||
| 50 | { |
||
| 51 | private $reader; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * {@inheritdoc} |
||
| 55 | */ |
||
| 56 | public function __construct(ParametersInterface $parameters) |
||
| 57 | { |
||
| 58 | $this->assertExtensions(); |
||
| 59 | |||
| 60 | parent::__construct($parameters); |
||
| 61 | |||
| 62 | $this->reader = $this->createReader(); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Disconnects from the server and destroys the underlying resource and the |
||
| 67 | * protocol reader resource when PHP's garbage collector kicks in. |
||
| 68 | */ |
||
| 69 | public function __destruct() |
||
| 70 | { |
||
| 71 | parent::__destruct(); |
||
| 72 | |||
| 73 | phpiredis_reader_destroy($this->reader); |
||
|
|
|||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Checks if the socket and phpiredis extensions are loaded in PHP. |
||
| 78 | */ |
||
| 79 | protected function assertExtensions() |
||
| 80 | { |
||
| 81 | if (!extension_loaded('sockets')) { |
||
| 82 | throw new NotSupportedException( |
||
| 83 | 'The "sockets" extension is required by this connection backend.' |
||
| 84 | ); |
||
| 85 | } |
||
| 86 | |||
| 87 | if (!extension_loaded('phpiredis')) { |
||
| 88 | throw new NotSupportedException( |
||
| 89 | 'The "phpiredis" extension is required by this connection backend.' |
||
| 90 | ); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * {@inheritdoc} |
||
| 96 | */ |
||
| 97 | protected function assertParameters(ParametersInterface $parameters) |
||
| 98 | { |
||
| 99 | switch ($parameters->scheme) { |
||
| 100 | case 'tcp': |
||
| 101 | case 'redis': |
||
| 102 | case 'unix': |
||
| 103 | break; |
||
| 104 | |||
| 105 | default: |
||
| 106 | throw new InvalidArgumentException("Invalid scheme: '$parameters->scheme'."); |
||
| 107 | } |
||
| 108 | |||
| 109 | if (isset($parameters->persistent)) { |
||
| 110 | throw new NotSupportedException( |
||
| 111 | 'Persistent connections are not supported by this connection backend.' |
||
| 112 | ); |
||
| 113 | } |
||
| 114 | |||
| 115 | return $parameters; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Creates a new instance of the protocol reader resource. |
||
| 120 | * |
||
| 121 | * @return resource |
||
| 122 | */ |
||
| 123 | private function createReader() |
||
| 124 | { |
||
| 125 | $reader = phpiredis_reader_create(); |
||
| 126 | |||
| 127 | phpiredis_reader_set_status_handler($reader, $this->getStatusHandler()); |
||
| 128 | phpiredis_reader_set_error_handler($reader, $this->getErrorHandler()); |
||
| 129 | |||
| 130 | return $reader; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Returns the underlying protocol reader resource. |
||
| 135 | * |
||
| 136 | * @return resource |
||
| 137 | */ |
||
| 138 | protected function getReader() |
||
| 139 | { |
||
| 140 | return $this->reader; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Returns the handler used by the protocol reader for inline responses. |
||
| 145 | * |
||
| 146 | * @return Closure |
||
| 147 | */ |
||
| 148 | protected function getStatusHandler() |
||
| 149 | { |
||
| 150 | static $statusHandler; |
||
| 151 | |||
| 152 | if (!$statusHandler) { |
||
| 153 | $statusHandler = function ($payload) { |
||
| 154 | return StatusResponse::get($payload); |
||
| 155 | }; |
||
| 156 | } |
||
| 157 | |||
| 158 | return $statusHandler; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Returns the handler used by the protocol reader for error responses. |
||
| 163 | * |
||
| 164 | * @return Closure |
||
| 165 | */ |
||
| 166 | protected function getErrorHandler() |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Helper method used to throw exceptions on socket errors. |
||
| 181 | */ |
||
| 182 | private function emitSocketError() |
||
| 183 | { |
||
| 184 | $errno = socket_last_error(); |
||
| 185 | $errstr = socket_strerror($errno); |
||
| 186 | |||
| 187 | $this->disconnect(); |
||
| 188 | |||
| 189 | $this->onConnectionError(trim($errstr), $errno); |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Gets the address of an host from connection parameters. |
||
| 194 | * |
||
| 195 | * @param ParametersInterface $parameters Parameters used to initialize the connection. |
||
| 196 | * |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | protected static function getAddress(ParametersInterface $parameters) |
||
| 200 | { |
||
| 201 | if (filter_var($host = $parameters->host, FILTER_VALIDATE_IP)) { |
||
| 202 | return $host; |
||
| 203 | } |
||
| 204 | |||
| 205 | if ($host === $address = gethostbyname($host)) { |
||
| 206 | return false; |
||
| 207 | } |
||
| 208 | |||
| 209 | return $address; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * {@inheritdoc} |
||
| 214 | */ |
||
| 215 | protected function createResource() |
||
| 216 | { |
||
| 217 | $parameters = $this->parameters; |
||
| 218 | |||
| 219 | if ($parameters->scheme === 'unix') { |
||
| 220 | $address = $parameters->path; |
||
| 221 | $domain = AF_UNIX; |
||
| 222 | $protocol = 0; |
||
| 223 | } else { |
||
| 224 | if (false === $address = self::getAddress($parameters)) { |
||
| 225 | $this->onConnectionError("Cannot resolve the address of '$parameters->host'."); |
||
| 226 | } |
||
| 227 | |||
| 228 | $domain = filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ? AF_INET6 : AF_INET; |
||
| 229 | $protocol = SOL_TCP; |
||
| 230 | } |
||
| 231 | |||
| 232 | if (false === $socket = @socket_create($domain, SOCK_STREAM, $protocol)) { |
||
| 233 | $this->emitSocketError(); |
||
| 234 | } |
||
| 235 | |||
| 236 | $this->setSocketOptions($socket, $parameters); |
||
| 237 | $this->connectWithTimeout($socket, $address, $parameters); |
||
| 238 | |||
| 239 | return $socket; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Sets options on the socket resource from the connection parameters. |
||
| 244 | * |
||
| 245 | * @param resource $socket Socket resource. |
||
| 246 | * @param ParametersInterface $parameters Parameters used to initialize the connection. |
||
| 247 | */ |
||
| 248 | private function setSocketOptions($socket, ParametersInterface $parameters) |
||
| 249 | { |
||
| 250 | if ($parameters->scheme !== 'unix') { |
||
| 251 | if (!socket_set_option($socket, SOL_TCP, TCP_NODELAY, 1)) { |
||
| 252 | $this->emitSocketError(); |
||
| 253 | } |
||
| 254 | |||
| 255 | if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) { |
||
| 256 | $this->emitSocketError(); |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | if (isset($parameters->read_write_timeout)) { |
||
| 261 | $rwtimeout = (float) $parameters->read_write_timeout; |
||
| 262 | $timeoutSec = floor($rwtimeout); |
||
| 263 | $timeoutUsec = ($rwtimeout - $timeoutSec) * 1000000; |
||
| 264 | |||
| 265 | $timeout = [ |
||
| 266 | 'sec' => $timeoutSec, |
||
| 267 | 'usec' => $timeoutUsec, |
||
| 268 | ]; |
||
| 269 | |||
| 270 | if (!socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, $timeout)) { |
||
| 271 | $this->emitSocketError(); |
||
| 272 | } |
||
| 273 | |||
| 274 | if (!socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, $timeout)) { |
||
| 275 | $this->emitSocketError(); |
||
| 276 | } |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Opens the actual connection to the server with a timeout. |
||
| 282 | * |
||
| 283 | * @param resource $socket Socket resource. |
||
| 284 | * @param string $address IP address (DNS-resolved from hostname) |
||
| 285 | * @param ParametersInterface $parameters Parameters used to initialize the connection. |
||
| 286 | * |
||
| 287 | * @return void |
||
| 288 | */ |
||
| 289 | private function connectWithTimeout($socket, $address, ParametersInterface $parameters) |
||
| 322 | } |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * {@inheritdoc} |
||
| 327 | */ |
||
| 328 | public function connect() |
||
| 329 | { |
||
| 330 | if (parent::connect() && $this->initCommands) { |
||
| 331 | foreach ($this->initCommands as $command) { |
||
| 332 | $response = $this->executeCommand($command); |
||
| 333 | |||
| 334 | if ($response instanceof ErrorResponseInterface) { |
||
| 335 | $this->onConnectionError("`{$command->getId()}` failed: {$response->getMessage()}", 0); |
||
| 336 | } |
||
| 337 | } |
||
| 338 | } |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * {@inheritdoc} |
||
| 343 | */ |
||
| 344 | public function disconnect() |
||
| 345 | { |
||
| 346 | if ($this->isConnected()) { |
||
| 347 | phpiredis_reader_reset($this->reader); |
||
| 348 | socket_close($this->getResource()); |
||
| 349 | |||
| 350 | parent::disconnect(); |
||
| 351 | } |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * {@inheritdoc} |
||
| 356 | */ |
||
| 357 | protected function write($buffer) |
||
| 358 | { |
||
| 359 | $socket = $this->getResource(); |
||
| 360 | |||
| 361 | while (($length = strlen($buffer)) > 0) { |
||
| 362 | $written = socket_write($socket, $buffer, $length); |
||
| 363 | |||
| 364 | if ($length === $written) { |
||
| 365 | return; |
||
| 366 | } |
||
| 367 | |||
| 368 | if ($written === false) { |
||
| 369 | $this->onConnectionError('Error while writing bytes to the server.'); |
||
| 370 | } |
||
| 371 | |||
| 372 | $buffer = substr($buffer, $written); |
||
| 373 | } |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * {@inheritdoc} |
||
| 378 | */ |
||
| 379 | public function read() |
||
| 398 | } |
||
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * {@inheritdoc} |
||
| 403 | */ |
||
| 404 | public function writeRequest(CommandInterface $command) |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * {@inheritdoc} |
||
| 414 | */ |
||
| 415 | public function __wakeup() |
||
| 419 | } |
||
| 420 | } |
||
| 421 |