Complex classes like Nexcessnet_Turpentine_Model_Varnish_Admin_Socket 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Nexcessnet_Turpentine_Model_Varnish_Admin_Socket, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 66 | class Nexcessnet_Turpentine_Model_Varnish_Admin_Socket { |
||
|
|
|||
| 67 | |||
| 68 | // possible command return codes, from vcli.h |
||
| 69 | const CODE_SYNTAX = 100; |
||
| 70 | const CODE_UNKNOWN = 101; |
||
| 71 | const CODE_UNIMPL = 102; |
||
| 72 | const CODE_TOOFEW = 104; |
||
| 73 | const CODE_TOOMANY = 105; |
||
| 74 | const CODE_PARAM = 106; |
||
| 75 | const CODE_AUTH = 107; |
||
| 76 | const CODE_OK = 200; |
||
| 77 | const CODE_CANT = 300; |
||
| 78 | const CODE_COMMS = 400; |
||
| 79 | const CODE_CLOSE = 500; |
||
| 80 | |||
| 81 | const READ_CHUNK_SIZE = 1024; |
||
| 82 | // varnish default, can only be changed at Varnish startup time |
||
| 83 | // if data to write is over this limit the actual run-time limit is checked |
||
| 84 | // and used |
||
| 85 | const CLI_CMD_LENGTH_LIMIT = 16384; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Regexp to detect the varnish version number |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | const REGEXP_VARNISH_VERSION = '/^varnish\-(?P<vmajor>\d)\.(?P<vminor>\d)\.(?P<vsub>\d) revision (?P<vhash>[0-9a-f]+)$/'; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * VCL config versions, should match config select values |
||
| 95 | */ |
||
| 96 | static protected $_VERSIONS = array('2.1', '3.0', '4.0', '4.1'); |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Varnish socket connection |
||
| 100 | * |
||
| 101 | * @var resource |
||
| 102 | */ |
||
| 103 | protected $_varnishConn = null; |
||
| 104 | protected $_host = '127.0.0.1'; |
||
| 105 | protected $_port = 6082; |
||
| 106 | protected $_private = null; |
||
| 107 | protected $_authSecret = null; |
||
| 108 | protected $_timeout = 5; |
||
| 109 | protected $_version = null; //auto-detect |
||
| 110 | |||
| 111 | public function __construct(array $options = array()) { |
||
| 112 | foreach ($options as $key => $value) { |
||
| 113 | switch ($key) { |
||
| 114 | case 'host': |
||
| 115 | $this->setHost($value); |
||
| 116 | break; |
||
| 117 | case 'port': |
||
| 118 | $this->setPort($value); |
||
| 119 | break; |
||
| 120 | case 'auth_secret': |
||
| 121 | $this->setAuthSecret($value); |
||
| 122 | break; |
||
| 123 | case 'timeout': |
||
| 124 | $this->setTimeout($value); |
||
| 125 | break; |
||
| 126 | case 'version': |
||
| 127 | $this->setVersion($value); |
||
| 128 | break; |
||
| 129 | default: |
||
| 130 | break; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Provide simple Varnish methods |
||
| 137 | * |
||
| 138 | * Methods provided: |
||
| 139 | help [command] |
||
| 140 | ping [timestamp] |
||
| 141 | auth response |
||
| 142 | banner |
||
| 143 | stats |
||
| 144 | vcl.load <configname> <filename> |
||
| 145 | vcl.inline <configname> <quoted_VCLstring> |
||
| 146 | vcl.use <configname> |
||
| 147 | vcl.discard <configname> |
||
| 148 | vcl.list |
||
| 149 | vcl.show <configname> |
||
| 150 | param.show [-l] [<param>] |
||
| 151 | param.set <param> <value> |
||
| 152 | purge.url <regexp> |
||
| 153 | purge <field> <operator> <arg> [&& <field> <oper> <arg>]... |
||
| 154 | purge.list |
||
| 155 | * |
||
| 156 | * @param string $name method name |
||
| 157 | * @param array $args method args |
||
| 158 | * @return array |
||
| 159 | */ |
||
| 160 | public function __call($name, $args) { |
||
| 161 | array_unshift($args, self::CODE_OK); |
||
| 162 | array_unshift($args, $this->_translateCommandMethod($name)); |
||
| 163 | return call_user_func_array(array($this, '_command'), $args); |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Get the connection string for this socket (<host>:<port>) |
||
| 168 | * |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public function getConnectionString() { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Get the set host for this instance |
||
| 177 | * |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | public function getHost() { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Set the Varnish host name/ip to connect to |
||
| 186 | * |
||
| 187 | * @param string $host hostname or ip |
||
| 188 | */ |
||
| 189 | public function setHost($host) { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Get the port set for this instance |
||
| 197 | * |
||
| 198 | * @return int |
||
| 199 | */ |
||
| 200 | public function getPort() { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Set the Varnish admin port |
||
| 206 | * |
||
| 207 | * @param int $port |
||
| 208 | */ |
||
| 209 | public function setPort($port) { |
||
| 210 | $this->_close(); |
||
| 211 | $this->_port = (int) $port; |
||
| 212 | return $this; |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Set the Varnish admin auth secret, use null to indicate there isn't one |
||
| 217 | * |
||
| 218 | * @param string $authSecret |
||
| 219 | */ |
||
| 220 | public function setAuthSecret($authSecret = null) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Set the timeout to connect to the varnish instance |
||
| 227 | * |
||
| 228 | * @param int $timeout |
||
| 229 | */ |
||
| 230 | public function setTimeout($timeout) { |
||
| 231 | $this->_timeout = (int) $timeout; |
||
| 232 | if ( ! is_null($this->_varnishConn)) { |
||
| 233 | stream_set_timeout($this->_varnishConn, $this->_timeout); |
||
| 234 | } |
||
| 235 | return $this; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Explicitly set the version of the varnish instance we're connecting to |
||
| 240 | * |
||
| 241 | * @param string $version version from $_VERSIONS |
||
| 242 | */ |
||
| 243 | public function setVersion($version) { |
||
| 244 | if (in_array($version, self::$_VERSIONS)) { |
||
| 245 | $this->_version = $version; |
||
| 246 | } else { |
||
| 247 | Mage::throwException('Unsupported Varnish version: '.$version); |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Check if we're connected to Varnish |
||
| 253 | * |
||
| 254 | * @return boolean |
||
| 255 | */ |
||
| 256 | public function isConnected() { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Find out what version mode we're running in |
||
| 262 | * |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public function getVersion() { |
||
| 266 | if ( ! $this->isConnected()) { |
||
| 267 | $this->_connect(); |
||
| 268 | } |
||
| 269 | return $this->_version; |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Stop the Varnish instance |
||
| 274 | */ |
||
| 275 | public function quit() { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Check if Varnish has a child running or not |
||
| 282 | * |
||
| 283 | * @return boolean |
||
| 284 | */ |
||
| 285 | public function status() { |
||
| 286 | $response = $this->_command('status'); |
||
| 287 | if ( ! preg_match('~Child in state (\w+)~', $response['text'], $match)) { |
||
| 288 | return false; |
||
| 289 | } else { |
||
| 290 | return $match[1] === 'running'; |
||
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Stop the running child (if it is running) |
||
| 296 | * |
||
| 297 | * @return $this |
||
| 298 | */ |
||
| 299 | public function stop() { |
||
| 300 | if ($this->status()) { |
||
| 301 | $this->_command('stop'); |
||
| 302 | } |
||
| 303 | return $this; |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Start running the Varnish child |
||
| 308 | * |
||
| 309 | * @return $this |
||
| 310 | */ |
||
| 311 | public function start() { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Establish a connection to the configured Varnish instance |
||
| 318 | * |
||
| 319 | * @return boolean |
||
| 320 | */ |
||
| 321 | protected function _connect() { |
||
| 322 | $this->_varnishConn = fsockopen($this->_host, $this->_port, $errno, |
||
| 323 | $errstr, $this->_timeout); |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param string $bannerText |
||
| 357 | */ |
||
| 358 | protected function _determineVersion($bannerText) { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Close the connection (if we're connected) |
||
| 382 | * |
||
| 383 | * @return $this |
||
| 384 | */ |
||
| 385 | protected function _close() { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Write data to the Varnish instance, a newline is automatically appended |
||
| 395 | * |
||
| 396 | * @param string $data data to write |
||
| 397 | * @return $this |
||
| 398 | */ |
||
| 399 | protected function _write($data) { |
||
| 400 | if (is_null($this->_varnishConn)) { |
||
| 401 | $this->_connect(); |
||
| 402 | } |
||
| 403 | $data = rtrim($data).PHP_EOL; |
||
| 404 | $dataLength = strlen($data); |
||
| 405 | if ($dataLength >= self::CLI_CMD_LENGTH_LIMIT) { |
||
| 406 | $cliBufferResponse = $this->param_show('cli_buffer'); |
||
| 407 | $regexp = '~^cli_buffer\s+(\d+)\s+\[bytes\]~'; |
||
| 408 | if ($this->getVersion() === '4.0' || $this->getVersion() === '4.1') { |
||
| 409 | // Varnish4 supports "16k" style notation |
||
| 410 | $regexp = '~^cli_buffer\s+Value is:\s+(\d+)([k|m|g|b]{1})?\s+\[bytes\]~'; |
||
| 411 | } |
||
| 412 | if (preg_match($regexp, $cliBufferResponse['text'], $match)) { |
||
| 413 | $realLimit = (int) $match[1]; |
||
| 414 | if (isset($match[2])) { |
||
| 415 | $factors = array('b'=>0, 'k'=>1, 'm'=>2, 'g'=>3); |
||
| 416 | $realLimit *= pow(1024, $factors[$match[2]]); |
||
| 417 | } |
||
| 418 | } else { |
||
| 419 | Mage::helper('turpentine/debug')->logWarn( |
||
| 420 | 'Failed to determine Varnish cli_buffer limit, using default' ); |
||
| 421 | $realLimit = self::CLI_CMD_LENGTH_LIMIT; |
||
| 422 | } |
||
| 423 | if ($dataLength >= $realLimit) { |
||
| 424 | Mage::throwException(sprintf( |
||
| 425 | 'Varnish data to write over length limit by %d characters', |
||
| 426 | $dataLength - $realLimit )); |
||
| 427 | } |
||
| 428 | } |
||
| 429 | if (($byteCount = fwrite($this->_varnishConn, $data)) !== $dataLength) { |
||
| 430 | Mage::throwException(sprintf('Varnish socket write error: %d != %d', |
||
| 431 | $byteCount, $dataLength)); |
||
| 432 | } |
||
| 433 | return $this; |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Read a response from Varnish instance |
||
| 438 | * |
||
| 439 | * @return array tuple of the response (code, text) |
||
| 440 | */ |
||
| 441 | protected function _read() { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * [_command description] |
||
| 474 | * @param string $verb command name |
||
| 475 | * @param integer $okCode code that indicates command was successful |
||
| 476 | * @param string ... command args |
||
| 477 | * @return array |
||
| 478 | */ |
||
| 479 | protected function _command($verb, $okCode = 200) { |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Handle v2.1 <> v3.0 command compatibility |
||
| 511 | * |
||
| 512 | * @param string $verb command to check |
||
| 513 | * @return string |
||
| 514 | */ |
||
| 515 | protected function _translateCommandMethod($verb) { |
||
| 532 | } |
||
| 533 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.