Complex classes like Mail 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 Mail, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Mail implements Listener, Logger |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * Mailer instance |
||
| 30 | * |
||
| 31 | * @var Swift_Mailer |
||
| 32 | */ |
||
| 33 | protected $mailer; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Mail subject |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $subject; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * From email address |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $senderMail; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * From name |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | protected $senderName; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Transport type [mail|smtp|null] |
||
| 58 | * |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $transportType; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * List of mail recipients |
||
| 65 | * |
||
| 66 | * @var array<string> |
||
| 67 | */ |
||
| 68 | protected $recipients = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Amount of executed backups |
||
| 72 | * |
||
| 73 | * @var integer |
||
| 74 | */ |
||
| 75 | private $numBackups = 0; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Amount of executed checks |
||
| 79 | * |
||
| 80 | * @var integer |
||
| 81 | */ |
||
| 82 | private $numChecks = 0; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Amount of executed Syncs |
||
| 86 | * |
||
| 87 | * @var integer |
||
| 88 | */ |
||
| 89 | private $numSyncs = 0; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Amount of executed Crypts |
||
| 93 | * |
||
| 94 | * @var integer |
||
| 95 | */ |
||
| 96 | private $numCrypts = 0; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Amount of executed Cleanups |
||
| 100 | * |
||
| 101 | * @var integer |
||
| 102 | */ |
||
| 103 | private $numCleanups = 0; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Send mail only if there was an error |
||
| 107 | * |
||
| 108 | * @var bool |
||
| 109 | */ |
||
| 110 | private $sendOnlyOnError = false; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Returns an array of event names this subscriber wants to listen to. |
||
| 114 | * |
||
| 115 | * The array keys are event names and the value can be: |
||
| 116 | * |
||
| 117 | * * The method name to call (priority defaults to 0) |
||
| 118 | * * An array composed of the method name to call and the priority |
||
| 119 | * * An array of arrays composed of the method names to call and respective |
||
| 120 | * priorities, or 0 if unset |
||
| 121 | * |
||
| 122 | * @return array The event names to listen to |
||
| 123 | */ |
||
| 124 | 1 | public static function getSubscribedEvents() |
|
| 125 | { |
||
| 126 | return [ |
||
| 127 | 1 | 'phpbu.backup_start' => 'onBackupStart', |
|
| 128 | 'phpbu.check_start' => 'onCheckStart', |
||
| 129 | 'phpbu.crypt_start' => 'onCryptStart', |
||
| 130 | 'phpbu.sync_start' => 'onSyncStart', |
||
| 131 | 'phpbu.cleanup_start' => 'onCleanupStart', |
||
| 132 | 'phpbu.app_end' => 'onPhpbuEnd', |
||
| 133 | ]; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Setup the Logger. |
||
| 138 | * |
||
| 139 | * @see \phpbu\App\Log\Logger::setup |
||
| 140 | * @param array $options |
||
| 141 | * @throws \phpbu\App\Exception |
||
| 142 | */ |
||
| 143 | 12 | public function setup(array $options) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Handle the phpbu end event. |
||
| 164 | * |
||
| 165 | * @param \phpbu\App\Event\App\End $event |
||
| 166 | * @throws \phpbu\App\Exception |
||
| 167 | */ |
||
| 168 | 4 | public function onPhpbuEnd(Event\App\End $event) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Backup start event. |
||
| 209 | * |
||
| 210 | * @param \phpbu\App\Event\Backup\Start $event |
||
| 211 | */ |
||
| 212 | 4 | public function onBackupStart(Event\Backup\Start $event) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Check start event. |
||
| 219 | * |
||
| 220 | * @param \phpbu\App\Event\Check\Start $event |
||
| 221 | */ |
||
| 222 | 1 | public function onCheckStart(Event\Check\Start $event) |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Crypt start event. |
||
| 229 | * |
||
| 230 | * @param \phpbu\App\Event\Crypt\Start $event |
||
| 231 | */ |
||
| 232 | 1 | public function onCryptStart(Event\Crypt\Start $event) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Sync start event. |
||
| 239 | * |
||
| 240 | * @param \phpbu\App\Event\Sync\Start $event |
||
| 241 | */ |
||
| 242 | 1 | public function onSyncStart(Event\Sync\Start $event) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Cleanup start event. |
||
| 249 | * |
||
| 250 | * @param \phpbu\App\Event\Cleanup\Start $event |
||
| 251 | */ |
||
| 252 | 1 | public function onCleanupStart(Event\Cleanup\Start $event) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Create a Swift_Mailer_Transport. |
||
| 259 | * |
||
| 260 | * @param string $type |
||
| 261 | * @param array $options |
||
| 262 | * @throws \phpbu\App\Exception |
||
| 263 | * @return \Swift_Transport |
||
| 264 | */ |
||
| 265 | 11 | protected function createTransport($type, array $options) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Create Swift Smtp Transport. |
||
| 292 | * |
||
| 293 | * @param array $options |
||
| 294 | * @return \Swift_SmtpTransport |
||
| 295 | * @throws \phpbu\App\Exception |
||
| 296 | */ |
||
| 297 | 2 | protected function getSmtpTransport(array $options) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Create a Swift Sendmail Transport. |
||
| 323 | * |
||
| 324 | * @param array $options |
||
| 325 | * @return \Swift_SendmailTransport |
||
| 326 | */ |
||
| 327 | 4 | protected function getSendmailTransport(array $options) |
|
| 337 | |||
| 338 | /** |
||
| 339 | * Return mail header html |
||
| 340 | * |
||
| 341 | * @return string |
||
| 342 | */ |
||
| 343 | 4 | protected function getHeaderHtml() |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Return mail status html |
||
| 351 | * |
||
| 352 | * @param \phpbu\App\Result $result |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | 4 | protected function getStatusHtml(Result $result) |
|
| 393 | |||
| 394 | /** |
||
| 395 | * Get error information. |
||
| 396 | * |
||
| 397 | * @param \phpbu\App\Result $result |
||
| 398 | * @return string |
||
| 399 | */ |
||
| 400 | 4 | protected function getErrorHtml(Result $result) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Return backup html information. |
||
| 426 | * |
||
| 427 | * @param \phpbu\App\Result $result |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | 4 | protected function getInfoHtml(Result $result) |
|
| 514 | |||
| 515 | /** |
||
| 516 | * Return mail body footer. |
||
| 517 | * |
||
| 518 | * @return string |
||
| 519 | */ |
||
| 520 | 4 | protected function getFooterHtml() |
|
| 525 | } |
||
| 526 |