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 |
||
25 | class Email extends ViewableData { |
||
26 | |||
27 | /** |
||
28 | * @var string $from Email-Address |
||
29 | */ |
||
30 | protected $from; |
||
31 | |||
32 | /** |
||
33 | * @var string $to Email-Address. Use comma-separation to pass multiple email-addresses. |
||
34 | */ |
||
35 | protected $to; |
||
36 | |||
37 | /** |
||
38 | * @var string $subject Subject of the email |
||
39 | */ |
||
40 | protected $subject; |
||
41 | |||
42 | /** |
||
43 | * Passed straight into {@link $ss_template} as $Body variable. |
||
44 | * |
||
45 | * @var string $body HTML content of the email. |
||
46 | */ |
||
47 | protected $body; |
||
48 | |||
49 | /** |
||
50 | * If not set, defaults to converting the HTML-body with {@link Convert::xml2raw()}. |
||
51 | * |
||
52 | * @var string $plaintext_body Optional string for plaintext emails. |
||
53 | */ |
||
54 | protected $plaintext_body; |
||
55 | |||
56 | /** |
||
57 | * @var string $cc |
||
58 | */ |
||
59 | protected $cc; |
||
60 | |||
61 | /** |
||
62 | * @var string $bcc |
||
63 | */ |
||
64 | protected $bcc; |
||
65 | |||
66 | /** |
||
67 | * @var array $customHeaders A map of header-name -> header-value |
||
68 | */ |
||
69 | protected $customHeaders = array(); |
||
70 | |||
71 | /** |
||
72 | * @var array $attachments Internal, use {@link attachFileFromString()} or {@link attachFile()} |
||
73 | */ |
||
74 | protected $attachments = array(); |
||
75 | |||
76 | /** |
||
77 | * @var boolean $parseVariables_done |
||
78 | */ |
||
79 | protected $parseVariables_done = false; |
||
80 | |||
81 | /** |
||
82 | * @var string $ss_template The name of the used template (without *.ss extension) |
||
83 | */ |
||
84 | protected $ss_template = 'GenericEmail'; |
||
85 | |||
86 | /** |
||
87 | * Used in the same way than {@link ViewableData->customize()}. |
||
88 | * |
||
89 | * @var ViewableData_Customised $template_data Additional data available in a template. |
||
90 | */ |
||
91 | protected $template_data; |
||
92 | |||
93 | /** |
||
94 | * This will be set in the config on a site-by-site basis |
||
95 | * |
||
96 | * @config |
||
97 | * @var string The default administrator email address. |
||
98 | */ |
||
99 | private static $admin_email = ''; |
||
100 | |||
101 | /** |
||
102 | * Send every email generated by the Email class to the given address. |
||
103 | * |
||
104 | * It will also add " [addressed to (email), cc to (email), bcc to (email)]" to the end of the subject line |
||
105 | * |
||
106 | * To set this, set Email.send_all_emails_to in your yml config file. |
||
107 | * It can also be set in _ss_environment.php with SS_SEND_ALL_EMAILS_TO. |
||
108 | * |
||
109 | * @config |
||
110 | * @var string $send_all_emails_to Email-Address |
||
111 | */ |
||
112 | private static $send_all_emails_to; |
||
113 | |||
114 | /** |
||
115 | * Send every email generated by the Email class *from* the given address. |
||
116 | * It will also add " [, from to (email)]" to the end of the subject line |
||
117 | * |
||
118 | * To set this, set Email.send_all_emails_from in your yml config file. |
||
119 | * It can also be set in _ss_environment.php with SS_SEND_ALL_EMAILS_FROM. |
||
120 | * |
||
121 | * @config |
||
122 | * @var string $send_all_emails_from Email-Address |
||
123 | */ |
||
124 | private static $send_all_emails_from; |
||
125 | |||
126 | /** |
||
127 | * @config |
||
128 | * @var string BCC every email generated by the Email class to the given address. |
||
129 | */ |
||
130 | private static $bcc_all_emails_to; |
||
131 | |||
132 | /** |
||
133 | * @config |
||
134 | * @var string CC every email generated by the Email class to the given address. |
||
135 | */ |
||
136 | private static $cc_all_emails_to; |
||
137 | |||
138 | /** |
||
139 | * Create a new email. |
||
140 | * |
||
141 | * @param string|null $from |
||
142 | * @param string|null $to |
||
143 | * @param string|null $subject |
||
144 | * @param string|null $body |
||
145 | * @param string|null $bounceHandlerURL |
||
146 | * @param string|null $cc |
||
147 | * @param string|null $bcc |
||
148 | */ |
||
149 | public function __construct($from = null, $to = null, $subject = null, $body = null, $bounceHandlerURL = null, |
||
165 | |||
166 | /** |
||
167 | * Get the mailer. |
||
168 | * |
||
169 | * @return Mailer |
||
170 | */ |
||
171 | public static function mailer() { |
||
174 | |||
175 | /** |
||
176 | * @deprecated since version 4.0 |
||
177 | */ |
||
178 | public static function set_mailer(Mailer $mailer) { |
||
182 | |||
183 | /** |
||
184 | * Attach a file based on provided raw data. |
||
185 | * |
||
186 | * @param string $data The raw file data (not encoded). |
||
187 | * @param string $attachedFilename Name of the file that should appear once it's sent as a separate attachment. |
||
188 | * @param string|null $mimeType MIME type to use when attaching file. If not provided, will attempt to infer via HTTP::get_mime_type(). |
||
189 | * @return $this |
||
190 | */ |
||
191 | public function attachFileFromString($data, $attachedFilename, $mimeType = null) { |
||
199 | |||
200 | /** |
||
201 | * @deprecated since version 4.0 |
||
202 | */ |
||
203 | public function setBounceHandlerURL($bounceHandlerURL) { |
||
206 | |||
207 | /** |
||
208 | * Attach the specified file to this email message. |
||
209 | * |
||
210 | * @param string $filename Relative or full path to file you wish to attach to this email message. |
||
211 | * @param string|null $attachedFilename Name of the file that should appear once it's sent as a separate attachment. |
||
212 | * @param string|null $mimeType MIME type to use when attaching file. If not provided, will attempt to infer via HTTP::get_mime_type(). |
||
213 | * @return $this |
||
214 | */ |
||
215 | public function attachFile($filename, $attachedFilename = null, $mimeType = null) { |
||
225 | |||
226 | /** |
||
227 | * @return string|null |
||
228 | */ |
||
229 | public function Subject() { |
||
232 | |||
233 | /** |
||
234 | * @return string|null |
||
235 | */ |
||
236 | public function Body() { |
||
239 | |||
240 | /** |
||
241 | * @return string|null |
||
242 | */ |
||
243 | public function To() { |
||
246 | |||
247 | /** |
||
248 | * @return string|null |
||
249 | */ |
||
250 | public function From() { |
||
253 | |||
254 | /** |
||
255 | * @return string|null |
||
256 | */ |
||
257 | public function Cc() { |
||
260 | |||
261 | /** |
||
262 | * @return string|null |
||
263 | */ |
||
264 | public function Bcc() { |
||
267 | |||
268 | /** |
||
269 | * @param string $val |
||
270 | * @return $this |
||
271 | */ |
||
272 | public function setSubject($val) { |
||
276 | |||
277 | /** |
||
278 | * @param string $val |
||
279 | * @return $this |
||
280 | */ |
||
281 | public function setBody($val) { |
||
285 | |||
286 | /** |
||
287 | * @param string $val |
||
288 | * @return $this |
||
289 | */ |
||
290 | public function setTo($val) { |
||
294 | |||
295 | /** |
||
296 | * @param string $val |
||
297 | * @return $this |
||
298 | */ |
||
299 | public function setFrom($val) { |
||
303 | |||
304 | /** |
||
305 | * @param string $val |
||
306 | * @return $this |
||
307 | */ |
||
308 | public function setCc($val) { |
||
312 | |||
313 | /** |
||
314 | * @param string $val |
||
315 | * @return $this |
||
316 | */ |
||
317 | public function setBcc($val) { |
||
321 | |||
322 | /** |
||
323 | * Set the "Reply-To" header with an email address. |
||
324 | * |
||
325 | * @param string $val |
||
326 | * @return $this |
||
327 | */ |
||
328 | public function setReplyTo($val) { |
||
332 | |||
333 | /** |
||
334 | * @param string $email |
||
335 | * @return $this |
||
336 | * @deprecated 4.0 Use the "setReplyTo" method instead |
||
337 | */ |
||
338 | public function replyTo($email) { |
||
343 | |||
344 | /** |
||
345 | * Add a custom header to this email message. Useful for implementing all those cool features that we didn't think of. |
||
346 | * |
||
347 | * IMPORTANT: If the specified header already exists, the provided value will be appended! |
||
348 | * |
||
349 | * @todo Should there be an option to replace instead of append? Or maybe a new method ->setCustomHeader()? |
||
350 | * |
||
351 | * @param string $headerName |
||
352 | * @param string $headerValue |
||
353 | * @return $this |
||
354 | */ |
||
355 | public function addCustomHeader($headerName, $headerValue) { |
||
370 | |||
371 | /** |
||
372 | * @return string |
||
373 | */ |
||
374 | public function BaseURL() { |
||
377 | |||
378 | /** |
||
379 | * Get an HTML string for debugging purposes. |
||
380 | * |
||
381 | * @return string |
||
382 | */ |
||
383 | public function debug() { |
||
394 | |||
395 | /** |
||
396 | * Set template name (without *.ss extension). |
||
397 | * |
||
398 | * @param string $template |
||
399 | * @return $this |
||
400 | */ |
||
401 | public function setTemplate($template) { |
||
405 | |||
406 | /** |
||
407 | * @return string |
||
408 | */ |
||
409 | public function getTemplate() { |
||
412 | |||
413 | /** |
||
414 | * @return $this |
||
415 | */ |
||
416 | protected function templateData() { |
||
432 | |||
433 | /** |
||
434 | * Used by {@link SSViewer} templates to detect if we're rendering an email template rather than a page template |
||
435 | */ |
||
436 | public function IsEmail() { |
||
439 | |||
440 | /** |
||
441 | * Populate this email template with values. This may be called many times. |
||
442 | * |
||
443 | * @param array|ViewableData $data |
||
444 | * @return $this |
||
445 | */ |
||
446 | public function populateTemplate($data) { |
||
457 | |||
458 | /** |
||
459 | * Load all the template variables into the internal variables, including |
||
460 | * the template into body. Called before send() or debugSend() |
||
461 | * $isPlain=true will cause the template to be ignored, otherwise the GenericEmail template will be used |
||
462 | * and it won't be plain email :) |
||
463 | * |
||
464 | * @param bool $isPlain |
||
465 | * @return $this |
||
466 | */ |
||
467 | protected function parseVariables($isPlain = false) { |
||
497 | |||
498 | /** |
||
499 | * @param string $address |
||
500 | * @return bool |
||
501 | * @deprecated 4.0 Use the "is_valid_address" method instead |
||
502 | */ |
||
503 | public static function validEmailAddress($address) { |
||
507 | |||
508 | /** |
||
509 | * Send the email in plaintext. |
||
510 | * |
||
511 | * @see send() for sending emails with HTML content. |
||
512 | * @uses Mailer->sendPlain() |
||
513 | * |
||
514 | * @param string $messageID Optional message ID so the message can be identified in bounces etc. |
||
515 | * @return mixed Success of the sending operation from an MTA perspective. Doesn't actually give any indication if |
||
516 | * the mail has been delivered to the recipient properly). See Mailer->sendPlain() for return type details. |
||
517 | */ |
||
518 | public function sendPlain($messageID = null) { |
||
572 | |||
573 | /** |
||
574 | * Send an email with HTML content. |
||
575 | * |
||
576 | * @see sendPlain() for sending plaintext emails only. |
||
577 | * @uses Mailer->sendHTML() |
||
578 | * |
||
579 | * @param string $messageID Optional message ID so the message can be identified in bounces etc. |
||
580 | * @return mixed Success of the sending operation from an MTA perspective. Doesn't actually give any indication if |
||
581 | * the mail has been delivered to the recipient properly). See Mailer->sendPlain() for return type details. |
||
582 | */ |
||
583 | public function send($messageID = null) { |
||
641 | |||
642 | /** |
||
643 | * Used as a default sender address in the {@link Email} class |
||
644 | * unless overwritten. Also shown to users on live environments |
||
645 | * as a contact address on system error pages. |
||
646 | * |
||
647 | * Used by {@link Email->send()}, {@link Email->sendPlain()}, {@link Debug->friendlyError()}. |
||
648 | * |
||
649 | * @deprecated 4.0 Use the "Email.admin_email" config setting instead |
||
650 | * @param string $newEmail |
||
651 | */ |
||
652 | public static function setAdminEmail($newEmail) { |
||
656 | |||
657 | /** |
||
658 | * @deprecated 4.0 Use the "Email.admin_email" config setting instead |
||
659 | * @return string |
||
660 | */ |
||
661 | public static function getAdminEmail() { |
||
665 | |||
666 | /** |
||
667 | * Send every email generated by the Email class to the given address. |
||
668 | * It will also add " [addressed to (email), cc to (email), bcc to (email)]" to the end of the subject line |
||
669 | * This can be used when testing, by putting a command like this in your _config.php file |
||
670 | * |
||
671 | * if(!Director::isLive()) Email::send_all_emails_to("[email protected]") |
||
672 | * |
||
673 | * @deprecated 4.0 Use the "Email.send_all_emails_to" config setting instead |
||
674 | */ |
||
675 | public static function send_all_emails_to($emailAddress) { |
||
679 | |||
680 | /** |
||
681 | * CC every email generated by the Email class to the given address. |
||
682 | * It won't affect the original delivery in the same way that send_all_emails_to does. It just adds a CC header |
||
683 | * with the given email address. Note that you can only call this once - subsequent calls will overwrite the |
||
684 | * configuration variable. |
||
685 | * |
||
686 | * This can be used when you have a system that relies heavily on email and you want someone to be checking all |
||
687 | * correspondence. |
||
688 | * |
||
689 | * if(Director::isLive()) Email::cc_all_emails_to("[email protected]") |
||
690 | * |
||
691 | * @deprecated 4.0 Use the "Email.cc_all_emails_to" config setting instead |
||
692 | */ |
||
693 | public static function cc_all_emails_to($emailAddress) { |
||
697 | |||
698 | /** |
||
699 | * BCC every email generated by the Email class to the given address. |
||
700 | * It won't affect the original delivery in the same way that send_all_emails_to does. It just adds a BCC header |
||
701 | * with the given email address. Note that you can only call this once - subsequent calls will overwrite the |
||
702 | * configuration variable. |
||
703 | * |
||
704 | * This can be used when you have a system that relies heavily on email and you want someone to be checking all |
||
705 | * correspondence. |
||
706 | * |
||
707 | * if(Director::isLive()) Email::cc_all_emails_to("[email protected]") |
||
708 | * |
||
709 | * @deprecated 4.0 Use the "Email.bcc_all_emails_to" config setting instead |
||
710 | */ |
||
711 | public static function bcc_all_emails_to($emailAddress) { |
||
715 | |||
716 | /** |
||
717 | * Validates the email address to get as close to RFC 822 compliant as possible. |
||
718 | * |
||
719 | * @param string $email |
||
720 | * @return bool |
||
721 | * |
||
722 | * @copyright Cal Henderson <[email protected]> |
||
723 | * This code is licensed under a Creative Commons Attribution-ShareAlike 2.5 License |
||
724 | * http://creativecommons.org/licenses/by-sa/2.5/ |
||
725 | */ |
||
726 | public static function is_valid_address($email){ |
||
743 | |||
744 | /** |
||
745 | * Encode an email-address to help protect it from spam bots. At the moment only simple string substitutions, which |
||
746 | * are not 100% safe from email harvesting. |
||
747 | * |
||
748 | * @todo Integrate javascript-based solution |
||
749 | * |
||
750 | * @param string $email Email-address |
||
751 | * @param string $method Method for obfuscating/encoding the address |
||
752 | * - 'direction': Reverse the text and then use CSS to put the text direction back to normal |
||
753 | * - 'visible': Simple string substitution ('@' to '[at]', '.' to '[dot], '-' to [dash]) |
||
754 | * - 'hex': Hexadecimal URL-Encoding - useful for mailto: links |
||
755 | * @return string |
||
756 | */ |
||
757 | public static function obfuscate($email, $method = 'visible') { |
||
777 | } |
||
778 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.