Complex classes like Email 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 Email, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class Email extends ViewableData |
||
| 31 | { |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string $from Email-Address |
||
| 35 | */ |
||
| 36 | protected $from; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string $to Email-Address. Use comma-separation to pass multiple email-addresses. |
||
| 40 | */ |
||
| 41 | protected $to; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string $subject Subject of the email |
||
| 45 | */ |
||
| 46 | protected $subject; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Passed straight into {@link $ss_template} as $Body variable. |
||
| 50 | * |
||
| 51 | * @var string $body HTML content of the email. |
||
| 52 | */ |
||
| 53 | protected $body; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * If not set, defaults to converting the HTML-body with {@link Convert::xml2raw()}. |
||
| 57 | * |
||
| 58 | * @var string $plaintext_body Optional string for plaintext emails. |
||
| 59 | */ |
||
| 60 | protected $plaintext_body; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var string $cc |
||
| 64 | */ |
||
| 65 | protected $cc; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var string $bcc |
||
| 69 | */ |
||
| 70 | protected $bcc; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var array $customHeaders A map of header-name -> header-value |
||
| 74 | */ |
||
| 75 | protected $customHeaders = array(); |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var array $attachments Internal, use {@link attachFileFromString()} or {@link attachFile()} |
||
| 79 | */ |
||
| 80 | protected $attachments = array(); |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var boolean $parseVariables_done |
||
| 84 | */ |
||
| 85 | protected $parseVariables_done = false; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var string $ss_template The name of the used template (without *.ss extension) |
||
| 89 | */ |
||
| 90 | protected $ss_template = 'GenericEmail'; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Used in the same way than {@link ViewableData->customize()}. |
||
| 94 | * |
||
| 95 | * @var ViewableData_Customised $template_data Additional data available in a template. |
||
| 96 | */ |
||
| 97 | protected $template_data; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * This will be set in the config on a site-by-site basis |
||
| 101 | * |
||
| 102 | * @config |
||
| 103 | * @var string The default administrator email address. |
||
| 104 | */ |
||
| 105 | private static $admin_email = ''; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Send every email generated by the Email class to the given address. |
||
| 109 | * |
||
| 110 | * It will also add " [addressed to (email), cc to (email), bcc to (email)]" to the end of the subject line |
||
| 111 | * |
||
| 112 | * To set this, set Email.send_all_emails_to in your yml config file. |
||
| 113 | * It can also be set in _ss_environment.php with SS_SEND_ALL_EMAILS_TO. |
||
| 114 | * |
||
| 115 | * @config |
||
| 116 | * @var string $send_all_emails_to Email-Address |
||
| 117 | */ |
||
| 118 | private static $send_all_emails_to; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Send every email generated by the Email class *from* the given address. |
||
| 122 | * It will also add " [, from to (email)]" to the end of the subject line |
||
| 123 | * |
||
| 124 | * To set this, set Email.send_all_emails_from in your yml config file. |
||
| 125 | * It can also be set in _ss_environment.php with SS_SEND_ALL_EMAILS_FROM. |
||
| 126 | * |
||
| 127 | * @config |
||
| 128 | * @var string $send_all_emails_from Email-Address |
||
| 129 | */ |
||
| 130 | private static $send_all_emails_from; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @config |
||
| 134 | * @var string BCC every email generated by the Email class to the given address. |
||
| 135 | */ |
||
| 136 | private static $bcc_all_emails_to; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @config |
||
| 140 | * @var string CC every email generated by the Email class to the given address. |
||
| 141 | */ |
||
| 142 | private static $cc_all_emails_to; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Create a new email. |
||
| 146 | * |
||
| 147 | * @param string|null $from |
||
| 148 | * @param string|null $to |
||
| 149 | * @param string|null $subject |
||
| 150 | * @param string|null $body |
||
| 151 | * @param string|null $bounceHandlerURL |
||
| 152 | * @param string|null $cc |
||
| 153 | * @param string|null $bcc |
||
| 154 | */ |
||
| 155 | public function __construct( |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get the mailer. |
||
| 193 | * |
||
| 194 | * @return Mailer |
||
| 195 | */ |
||
| 196 | public static function mailer() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Attach a file based on provided raw data. |
||
| 203 | * |
||
| 204 | * @param string $data The raw file data (not encoded). |
||
| 205 | * @param string $attachedFilename Name of the file that should appear once it's sent as a separate attachment. |
||
| 206 | * @param string|null $mimeType MIME type to use when attaching file. If not provided, will attempt to infer via HTTP::get_mime_type(). |
||
| 207 | * @return $this |
||
| 208 | */ |
||
| 209 | public function attachFileFromString($data, $attachedFilename, $mimeType = null) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Attach the specified file to this email message. |
||
| 221 | * |
||
| 222 | * @param string $filename Relative or full path to file you wish to attach to this email message. |
||
| 223 | * @param string|null $attachedFilename Name of the file that should appear once it's sent as a separate attachment. |
||
| 224 | * @param string|null $mimeType MIME type to use when attaching file. If not provided, will attempt to infer via HTTP::get_mime_type(). |
||
| 225 | * @return $this |
||
| 226 | */ |
||
| 227 | public function attachFile($filename, $attachedFilename = null, $mimeType = null) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @return string|null |
||
| 243 | */ |
||
| 244 | public function Subject() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @return string|null |
||
| 251 | */ |
||
| 252 | public function Body() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @return string|null |
||
| 259 | */ |
||
| 260 | public function To() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @return string|null |
||
| 267 | */ |
||
| 268 | public function From() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @return string|null |
||
| 275 | */ |
||
| 276 | public function Cc() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @return string|null |
||
| 283 | */ |
||
| 284 | public function Bcc() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param string $val |
||
| 291 | * @return $this |
||
| 292 | */ |
||
| 293 | public function setSubject($val) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param string $val |
||
| 301 | * @return $this |
||
| 302 | */ |
||
| 303 | public function setBody($val) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param string $val |
||
| 311 | * @return $this |
||
| 312 | */ |
||
| 313 | public function setTo($val) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param string $val |
||
| 321 | * @return $this |
||
| 322 | */ |
||
| 323 | public function setFrom($val) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @param string $val |
||
| 331 | * @return $this |
||
| 332 | */ |
||
| 333 | public function setCc($val) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param string $val |
||
| 341 | * @return $this |
||
| 342 | */ |
||
| 343 | public function setBcc($val) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Set the "Reply-To" header with an email address. |
||
| 351 | * |
||
| 352 | * @param string $val |
||
| 353 | * @return $this |
||
| 354 | */ |
||
| 355 | public function setReplyTo($val) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Add a custom header to this email message. Useful for implementing all those cool features that we didn't think of. |
||
| 363 | * |
||
| 364 | * IMPORTANT: If the specified header already exists, the provided value will be appended! |
||
| 365 | * |
||
| 366 | * @todo Should there be an option to replace instead of append? Or maybe a new method ->setCustomHeader()? |
||
| 367 | * |
||
| 368 | * @param string $headerName |
||
| 369 | * @param string $headerValue |
||
| 370 | * @return $this |
||
| 371 | */ |
||
| 372 | public function addCustomHeader($headerName, $headerValue) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @return string |
||
| 391 | */ |
||
| 392 | public function BaseURL() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Get an HTML string for debugging purposes. |
||
| 399 | * |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | public function debug() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Set template name (without *.ss extension). |
||
| 417 | * |
||
| 418 | * @param string $template |
||
| 419 | * @return $this |
||
| 420 | */ |
||
| 421 | public function setTemplate($template) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | public function getTemplate() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @return Email|ViewableData_Customised |
||
| 437 | */ |
||
| 438 | protected function templateData() |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Used by {@link SSViewer} templates to detect if we're rendering an email template rather than a page template |
||
| 458 | */ |
||
| 459 | public function IsEmail() |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Populate this email template with values. This may be called many times. |
||
| 466 | * |
||
| 467 | * @param array|ViewableData $data |
||
| 468 | * @return $this |
||
| 469 | */ |
||
| 470 | public function populateTemplate($data) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Load all the template variables into the internal variables, including |
||
| 487 | * the template into body. Called before send() or debugSend() |
||
| 488 | * $isPlain=true will cause the template to be ignored, otherwise the GenericEmail template will be used |
||
| 489 | * and it won't be plain email :) |
||
| 490 | * |
||
| 491 | * @param bool $isPlain |
||
| 492 | * @return $this |
||
| 493 | */ |
||
| 494 | protected function parseVariables($isPlain = false) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Send the email in plaintext. |
||
| 530 | * |
||
| 531 | * @see send() for sending emails with HTML content. |
||
| 532 | * @uses Mailer->sendPlain() |
||
| 533 | * |
||
| 534 | * @param string $messageID Optional message ID so the message can be identified in bounces etc. |
||
| 535 | * @return mixed Success of the sending operation from an MTA perspective. Doesn't actually give any indication if |
||
| 536 | * the mail has been delivered to the recipient properly). See Mailer->sendPlain() for return type details. |
||
| 537 | */ |
||
| 538 | public function sendPlain($messageID = null) |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Send an email with HTML content. |
||
| 612 | * |
||
| 613 | * @see sendPlain() for sending plaintext emails only. |
||
| 614 | * @uses Mailer->sendHTML() |
||
| 615 | * |
||
| 616 | * @param string $messageID Optional message ID so the message can be identified in bounces etc. |
||
| 617 | * @return mixed Success of the sending operation from an MTA perspective. Doesn't actually give any indication if |
||
| 618 | * the mail has been delivered to the recipient properly). See Mailer->sendPlain() for return type details. |
||
| 619 | */ |
||
| 620 | public function send($messageID = null) |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Validates the email address to get as close to RFC 822 compliant as possible. |
||
| 704 | * |
||
| 705 | * @param string $email |
||
| 706 | * @return bool |
||
| 707 | * |
||
| 708 | * @copyright Cal Henderson <[email protected]> |
||
| 709 | * This code is licensed under a Creative Commons Attribution-ShareAlike 2.5 License |
||
| 710 | * http://creativecommons.org/licenses/by-sa/2.5/ |
||
| 711 | */ |
||
| 712 | public static function is_valid_address($email) |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Encode an email-address to help protect it from spam bots. At the moment only simple string substitutions, which |
||
| 733 | * are not 100% safe from email harvesting. |
||
| 734 | * |
||
| 735 | * @todo Integrate javascript-based solution |
||
| 736 | * |
||
| 737 | * @param string $email Email-address |
||
| 738 | * @param string $method Method for obfuscating/encoding the address |
||
| 739 | * - 'direction': Reverse the text and then use CSS to put the text direction back to normal |
||
| 740 | * - 'visible': Simple string substitution ('@' to '[at]', '.' to '[dot], '-' to [dash]) |
||
| 741 | * - 'hex': Hexadecimal URL-Encoding - useful for mailto: links |
||
| 742 | * @return string |
||
| 743 | */ |
||
| 744 | public static function obfuscate($email, $method = 'visible') |
||
| 768 | } |
||
| 769 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: