Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Swift_Transport_AbstractSmtpTransport 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 Swift_Transport_AbstractSmtpTransport, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport |
||
|
|
|||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Input-Output buffer for sending/receiving SMTP commands and responses |
||
| 20 | * |
||
| 21 | * @var Swift_Transport_IoBuffer|Swift_Transport_StreamBuffer |
||
| 22 | */ |
||
| 23 | protected $_buffer; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Connection status |
||
| 27 | * |
||
| 28 | * @var bool |
||
| 29 | */ |
||
| 30 | protected $_started = false; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The domain name to use in HELO command |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $_domain = '[127.0.0.1]'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The event dispatching layer |
||
| 41 | * |
||
| 42 | * @var Swift_Events_EventDispatcher |
||
| 43 | */ |
||
| 44 | protected $_eventDispatcher; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Source Ip |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $_sourceIp; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Return an array of params for the Buffer |
||
| 55 | */ |
||
| 56 | abstract protected function _getBufferParams(); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Creates a new EsmtpTransport using the given I/O buffer. |
||
| 60 | * |
||
| 61 | * @param Swift_Transport_IoBuffer $buf |
||
| 62 | * @param Swift_Events_EventDispatcher $dispatcher |
||
| 63 | */ |
||
| 64 | 173 | public function __construct(Swift_Transport_IoBuffer $buf, Swift_Events_EventDispatcher $dispatcher) |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Set the name of the local domain which Swift will identify itself as. |
||
| 73 | * |
||
| 74 | * This should be a fully-qualified domain name and should be truly the domain |
||
| 75 | * you're using. |
||
| 76 | * |
||
| 77 | * If your server doesn't have a domain name, use the IP in square |
||
| 78 | * brackets (i.e. [127.0.0.1]). |
||
| 79 | * |
||
| 80 | * @param string $domain |
||
| 81 | * |
||
| 82 | * @return Swift_Transport_AbstractSmtpTransport |
||
| 83 | */ |
||
| 84 | 5 | public function setLocalDomain($domain) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Get the name of the domain Swift will identify as. |
||
| 93 | * |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | public function getLocalDomain() |
||
| 97 | { |
||
| 98 | return $this->_domain; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Sets the source IP. |
||
| 103 | * |
||
| 104 | * @param string $source |
||
| 105 | */ |
||
| 106 | public function setSourceIp($source) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Returns the IP used to connect to the destination. |
||
| 113 | * |
||
| 114 | * @return string |
||
| 115 | */ |
||
| 116 | public function getSourceIp() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Start the SMTP connection. |
||
| 123 | */ |
||
| 124 | 140 | public function start() |
|
| 125 | { |
||
| 126 | 140 | if (!$this->_started) { |
|
| 127 | |||
| 128 | 140 | $evt = $this->_eventDispatcher->createTransportChangeEvent($this); |
|
| 129 | 140 | if ($evt) { |
|
| 130 | |||
| 131 | 22 | $this->_eventDispatcher->dispatchEvent($evt, 'beforeTransportStarted'); |
|
| 132 | 22 | if ($evt->bubbleCancelled()) { |
|
| 133 | 3 | return; |
|
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | try { |
||
| 138 | 137 | $this->_buffer->initialize($this->_getBufferParams()); |
|
| 139 | } catch (Swift_TransportException $e) { |
||
| 140 | $this->_throwException($e); |
||
| 141 | } |
||
| 142 | 137 | $this->_readGreeting(); |
|
| 143 | 131 | $this->_doHeloCommand(); |
|
| 144 | |||
| 145 | 128 | if ($evt) { |
|
| 146 | 19 | $this->_eventDispatcher->dispatchEvent($evt, 'transportStarted'); |
|
| 147 | } |
||
| 148 | |||
| 149 | 128 | $this->_started = true; |
|
| 150 | } |
||
| 151 | 128 | } |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Test if an SMTP connection has been established. |
||
| 155 | * |
||
| 156 | * @return bool |
||
| 157 | */ |
||
| 158 | 22 | public function isStarted() |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Send the given Message. |
||
| 165 | * |
||
| 166 | * Recipient/sender data will be retrieved from the Message API. |
||
| 167 | * The return value is the number of recipients who were accepted for delivery. |
||
| 168 | * |
||
| 169 | * @param Swift_Mime_Message $message |
||
| 170 | * @param string[]|null $failedRecipients An array of failures by-reference |
||
| 171 | * |
||
| 172 | * @return int |
||
| 173 | * |
||
| 174 | * @throws Exception |
||
| 175 | * @throws Swift_TransportException |
||
| 176 | */ |
||
| 177 | 84 | public function send(Swift_Mime_Message $message, &$failedRecipients = null) |
|
| 178 | { |
||
| 179 | 84 | $sent = 0; |
|
| 180 | 84 | $failedRecipients = (array)$failedRecipients; |
|
| 181 | |||
| 182 | 84 | $evt = $this->_eventDispatcher->createSendEvent($this, $message); |
|
| 183 | 84 | if ($evt) { |
|
| 184 | |||
| 185 | 25 | $this->_eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed'); |
|
| 186 | 25 | if ($evt->bubbleCancelled()) { |
|
| 187 | 3 | return 0; |
|
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | 81 | if (!$reversePath = $this->_getReversePath($message)) { |
|
| 192 | $this->_throwException( |
||
| 193 | new Swift_TransportException( |
||
| 194 | 'Cannot send message without a sender address' |
||
| 195 | ) |
||
| 196 | ); |
||
| 197 | } |
||
| 198 | |||
| 199 | 81 | $to = (array)$message->getTo(); |
|
| 200 | 81 | $cc = (array)$message->getCc(); |
|
| 201 | 81 | $tos = array_merge($to, $cc); |
|
| 202 | 81 | $bcc = (array)$message->getBcc(); |
|
| 203 | |||
| 204 | 81 | $message->setBcc(array()); |
|
| 205 | |||
| 206 | try { |
||
| 207 | 81 | $sent += $this->_sendTo($message, $reversePath, $tos, $failedRecipients); |
|
| 208 | 54 | $sent += $this->_sendBcc($message, $reversePath, $bcc, $failedRecipients); |
|
| 209 | 30 | } catch (Exception $e) { |
|
| 210 | 30 | $message->setBcc($bcc); |
|
| 211 | 30 | throw $e; |
|
| 212 | } |
||
| 213 | |||
| 214 | 51 | $message->setBcc($bcc); |
|
| 215 | |||
| 216 | 51 | if ($evt) { |
|
| 217 | |||
| 218 | 13 | if ($sent === count($to) + count($cc) + count($bcc)) { |
|
| 219 | 13 | $evt->setResult(Swift_Events_SendEvent::RESULT_SUCCESS); |
|
| 220 | } elseif ($sent > 0) { |
||
| 221 | $evt->setResult(Swift_Events_SendEvent::RESULT_TENTATIVE); |
||
| 222 | } else { |
||
| 223 | $evt->setResult(Swift_Events_SendEvent::RESULT_FAILED); |
||
| 224 | } |
||
| 225 | |||
| 226 | 13 | $evt->setFailedRecipients($failedRecipients); |
|
| 227 | 13 | $this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed'); |
|
| 228 | } |
||
| 229 | |||
| 230 | 51 | $message->generateId(); // Make sure a new Message ID is used |
|
| 231 | |||
| 232 | 51 | return $sent; |
|
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Stop the SMTP connection. |
||
| 237 | */ |
||
| 238 | 39 | public function stop() |
|
| 239 | { |
||
| 240 | 39 | if ($this->_started) { |
|
| 241 | |||
| 242 | 16 | $evt = $this->_eventDispatcher->createTransportChangeEvent($this); |
|
| 243 | 16 | if ($evt) { |
|
| 244 | |||
| 245 | 13 | $this->_eventDispatcher->dispatchEvent($evt, 'beforeTransportStopped'); |
|
| 246 | 13 | if ($evt->bubbleCancelled()) { |
|
| 247 | 3 | return; |
|
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | try { |
||
| 252 | 13 | $this->executeCommand("QUIT\r\n", array(221)); |
|
| 253 | 6 | } catch (Swift_TransportException $e) { |
|
| 254 | } |
||
| 255 | |||
| 256 | try { |
||
| 257 | 13 | $this->_buffer->terminate(); |
|
| 258 | |||
| 259 | 13 | if ($evt) { |
|
| 260 | 13 | $this->_eventDispatcher->dispatchEvent($evt, 'transportStopped'); |
|
| 261 | } |
||
| 262 | } catch (Swift_TransportException $e) { |
||
| 263 | $this->_throwException($e); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | 36 | $this->_started = false; |
|
| 267 | 36 | } |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Register a plugin. |
||
| 271 | * |
||
| 272 | * @param Swift_Events_EventListener $plugin |
||
| 273 | */ |
||
| 274 | 3 | public function registerPlugin(Swift_Events_EventListener $plugin) |
|
| 275 | { |
||
| 276 | 3 | $this->_eventDispatcher->bindEventListener($plugin); |
|
| 277 | 3 | } |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Reset the current mail transaction. |
||
| 281 | */ |
||
| 282 | public function reset() |
||
| 283 | { |
||
| 284 | $this->executeCommand("RSET\r\n", array(250)); |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Get the IoBuffer where read/writes are occurring. |
||
| 289 | * |
||
| 290 | * @return Swift_Transport_IoBuffer |
||
| 291 | */ |
||
| 292 | 33 | public function getBuffer() |
|
| 293 | { |
||
| 294 | 33 | return $this->_buffer; |
|
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Run a command against the buffer, expecting the given response codes. |
||
| 299 | * |
||
| 300 | * If no response codes are given, the response will not be validated. |
||
| 301 | * If codes are given, an exception will be thrown on an invalid response. |
||
| 302 | * |
||
| 303 | * @param string $command |
||
| 304 | * @param int[] $codes |
||
| 305 | * @param string[] $failures An array of failures by-reference |
||
| 306 | * |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | 137 | public function executeCommand($command, $codes = array(), &$failures = null) |
|
| 310 | { |
||
| 311 | 137 | $failures = (array)$failures; |
|
| 312 | 137 | $seq = $this->_buffer->write($command); |
|
| 313 | 137 | $response = $this->_getFullResponse($seq); |
|
| 314 | |||
| 315 | 137 | $evt = $this->_eventDispatcher->createCommandEvent($this, $command, $codes); |
|
| 316 | 137 | if ($evt) { |
|
| 317 | 7 | $this->_eventDispatcher->dispatchEvent($evt, 'commandSent'); |
|
| 318 | } |
||
| 319 | |||
| 320 | 137 | $this->_assertResponseCode($response, $codes); |
|
| 321 | |||
| 322 | 131 | return $response; |
|
| 323 | } |
||
| 324 | |||
| 325 | /** Read the opening SMTP greeting */ |
||
| 326 | 137 | protected function _readGreeting() |
|
| 330 | |||
| 331 | /** Send the HELO welcome */ |
||
| 332 | 45 | protected function _doHeloCommand() |
|
| 333 | { |
||
| 334 | 45 | $this->executeCommand( |
|
| 335 | 45 | sprintf("HELO %s\r\n", $this->_domain), array(250) |
|
| 336 | ); |
||
| 337 | 42 | } |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Send the MAIL FROM command |
||
| 341 | * |
||
| 342 | * @param string $address |
||
| 343 | */ |
||
| 344 | 25 | protected function _doMailFromCommand($address) |
|
| 345 | { |
||
| 346 | 25 | $this->executeCommand( |
|
| 347 | 25 | sprintf("MAIL FROM:<%s>\r\n", $address), array(250) |
|
| 348 | ); |
||
| 349 | 24 | } |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Send the RCPT TO command |
||
| 353 | * |
||
| 354 | * @param string $address |
||
| 355 | */ |
||
| 356 | 24 | protected function _doRcptToCommand($address) |
|
| 357 | { |
||
| 358 | 24 | $this->executeCommand( |
|
| 359 | 24 | sprintf("RCPT TO:<%s>\r\n", $address), array(250, 251, 252) |
|
| 360 | ); |
||
| 361 | 20 | } |
|
| 362 | |||
| 363 | /** Send the DATA command */ |
||
| 364 | 63 | protected function _doDataCommand() |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Stream the contents of the message over the buffer |
||
| 371 | * |
||
| 372 | * @param Swift_Mime_Message $message |
||
| 373 | * |
||
| 374 | * @throws Swift_TransportException |
||
| 375 | */ |
||
| 376 | 57 | protected function _streamMessage(Swift_Mime_Message $message) |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Determine the best-use reverse path for this message |
||
| 393 | * |
||
| 394 | * @param Swift_Mime_Message $message |
||
| 395 | * |
||
| 396 | * @return mixed|null|string |
||
| 397 | */ |
||
| 398 | 85 | View Code Duplication | protected function _getReversePath(Swift_Mime_Message $message) |
| 418 | |||
| 419 | /** |
||
| 420 | * Throw a TransportException, first sending it to any listeners. |
||
| 421 | * |
||
| 422 | * @param Swift_TransportException $e |
||
| 423 | * |
||
| 424 | * @throws Swift_TransportException |
||
| 425 | */ |
||
| 426 | 55 | View Code Duplication | protected function _throwException(Swift_TransportException $e) |
| 440 | |||
| 441 | /** |
||
| 442 | * Throws an Exception if a response code is incorrect |
||
| 443 | * |
||
| 444 | * @param string $response |
||
| 445 | * @param integer[] $wanted |
||
| 446 | * |
||
| 447 | * @throws Swift_TransportException |
||
| 448 | */ |
||
| 449 | 143 | protected function _assertResponseCode($response, $wanted) |
|
| 472 | |||
| 473 | /** |
||
| 474 | * Get an entire multi-line response using its sequence number |
||
| 475 | * |
||
| 476 | * @param int $seq |
||
| 477 | * |
||
| 478 | * @return string |
||
| 479 | * @throws Swift_TransportException |
||
| 480 | */ |
||
| 481 | 143 | protected function _getFullResponse($seq) |
|
| 501 | |||
| 502 | /** |
||
| 503 | * Send an email to the given recipients from the given reverse path |
||
| 504 | * |
||
| 505 | * @param Swift_Mime_Message $message |
||
| 506 | * @param string $reversePath |
||
| 507 | * @param array $recipients |
||
| 508 | * @param array $failedRecipients |
||
| 509 | * |
||
| 510 | * @return int |
||
| 511 | * |
||
| 512 | * @throws Swift_TransportException |
||
| 513 | */ |
||
| 514 | 81 | private function _doMailTransaction($message, $reversePath, array $recipients, array &$failedRecipients) |
|
| 539 | |||
| 540 | /** |
||
| 541 | * Send a message to the given To: recipients |
||
| 542 | * |
||
| 543 | * @param Swift_Mime_Message $message |
||
| 544 | * @param $reversePath |
||
| 545 | * @param array $to |
||
| 546 | * @param array $failedRecipients |
||
| 547 | * |
||
| 548 | * @return int |
||
| 549 | * |
||
| 550 | * @throws Swift_TransportException |
||
| 551 | */ |
||
| 552 | 81 | private function _sendTo(Swift_Mime_Message $message, $reversePath, array $to, array &$failedRecipients) |
|
| 560 | |||
| 561 | /** |
||
| 562 | * Send a message to all Bcc: recipients |
||
| 563 | * |
||
| 564 | * @param Swift_Mime_Message $message |
||
| 565 | * @param $reversePath |
||
| 566 | * @param array $bcc |
||
| 567 | * @param array $failedRecipients |
||
| 568 | * |
||
| 569 | * @return int |
||
| 570 | * |
||
| 571 | * @throws Swift_TransportException |
||
| 572 | */ |
||
| 573 | 54 | private function _sendBcc(Swift_Mime_Message $message, $reversePath, array $bcc, array &$failedRecipients) |
|
| 588 | |||
| 589 | /** |
||
| 590 | * Try to determine the hostname of the server this is run on |
||
| 591 | * |
||
| 592 | * @return bool |
||
| 593 | */ |
||
| 594 | 173 | private function _lookupHostname() |
|
| 622 | |||
| 623 | /** |
||
| 624 | * Determine is the $hostname is a fully-qualified name |
||
| 625 | * |
||
| 626 | * @param $hostname |
||
| 627 | * |
||
| 628 | * @return bool |
||
| 629 | */ |
||
| 630 | private function _isFqdn($hostname) |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Destructor. |
||
| 644 | */ |
||
| 645 | 27 | public function __destruct() |
|
| 649 | } |
||
| 650 |
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.