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) |
|
| 169 | { |
||
| 170 | 4 | $result = $event->getResult(); |
|
| 171 | 4 | $allGood = $result->allOk(); |
|
| 172 | |||
| 173 | 4 | if (!$this->sendOnlyOnError || !$allGood) { |
|
| 174 | 4 | $header = $this->getHeaderHtml(); |
|
| 175 | 4 | $status = $this->getStatusHtml($result); |
|
| 176 | 4 | $errors = $this->getErrorHtml($result); |
|
| 177 | 4 | $info = $this->getInfoHtml($result); |
|
| 178 | 4 | $footer = $this->getFooterHtml(); |
|
| 179 | 4 | $body = '<html><body '. TPL::getSnippet('sBody') . '>' |
|
| 180 | 4 | . $header |
|
| 181 | 4 | . $status |
|
| 182 | 4 | . $errors |
|
| 183 | 4 | . $info |
|
| 184 | 4 | . $footer |
|
| 185 | 4 | . '</body></html>'; |
|
| 186 | 4 | $sent = null; |
|
| 187 | |||
| 188 | try { |
||
| 189 | /** @var \Swift_Message $message */ |
||
| 190 | 4 | $message = Swift_Message::newInstance(); |
|
| 191 | 4 | $message->setSubject($this->subject) |
|
| 192 | 4 | ->setFrom($this->senderMail, $this->senderName) |
|
| 193 | 4 | ->setTo($this->recipients) |
|
| 194 | 4 | ->setBody($body, 'text/html'); |
|
| 195 | |||
| 196 | 4 | $sent = $this->mailer->send($message); |
|
| 197 | } catch (\Exception $e) { |
||
| 198 | throw new Exception($e->getMessage()); |
||
| 199 | } |
||
| 200 | 4 | if (!$sent) { |
|
| 201 | throw new Exception('mail could not be sent'); |
||
| 202 | } |
||
| 203 | } |
||
| 204 | 4 | } |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Backup start event. |
||
| 208 | * |
||
| 209 | * @param \phpbu\App\Event\Backup\Start $event |
||
| 210 | */ |
||
| 211 | 4 | public function onBackupStart(Event\Backup\Start $event) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Check start event. |
||
| 218 | * |
||
| 219 | * @param \phpbu\App\Event\Check\Start $event |
||
| 220 | */ |
||
| 221 | 1 | public function onCheckStart(Event\Check\Start $event) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Crypt start event. |
||
| 228 | * |
||
| 229 | * @param \phpbu\App\Event\Crypt\Start $event |
||
| 230 | */ |
||
| 231 | 1 | public function onCryptStart(Event\Crypt\Start $event) |
|
| 232 | { |
||
| 233 | 1 | $this->numCrypts++; |
|
| 234 | 1 | } |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Sync start event. |
||
| 238 | * |
||
| 239 | * @param \phpbu\App\Event\Sync\Start $event |
||
| 240 | */ |
||
| 241 | 1 | public function onSyncStart(Event\Sync\Start $event) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Cleanup start event. |
||
| 248 | * |
||
| 249 | * @param \phpbu\App\Event\Cleanup\Start $event |
||
| 250 | */ |
||
| 251 | 1 | public function onCleanupStart(Event\Cleanup\Start $event) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Create a Swift_Mailer_Transport. |
||
| 258 | * |
||
| 259 | * @param string $type |
||
| 260 | * @param array $options |
||
| 261 | * @throws \phpbu\App\Exception |
||
| 262 | * @return \Swift_Transport |
||
| 263 | */ |
||
| 264 | 11 | protected function createTransport($type, array $options) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Create Swift Smtp Transport. |
||
| 291 | * |
||
| 292 | * @param array $options |
||
| 293 | * @return \Swift_SmtpTransport |
||
| 294 | * @throws \phpbu\App\Exception |
||
| 295 | */ |
||
| 296 | 2 | protected function getSmtpTransport(array $options) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Create a Swift Sendmail Transport. |
||
| 322 | * |
||
| 323 | * @param array $options |
||
| 324 | * @return \Swift_SendmailTransport |
||
| 325 | */ |
||
| 326 | 4 | protected function getSendmailTransport(array $options) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Return mail header html |
||
| 339 | * |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | 4 | protected function getHeaderHtml() |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Return mail status html |
||
| 350 | * |
||
| 351 | * @param \phpbu\App\Result $result |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | 4 | protected function getStatusHtml(Result $result) |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Get error information. |
||
| 395 | * |
||
| 396 | * @param \phpbu\App\Result $result |
||
| 397 | * @return string |
||
| 398 | */ |
||
| 399 | 4 | protected function getErrorHtml(Result $result) |
|
| 422 | |||
| 423 | /** |
||
| 424 | * Return backup html information. |
||
| 425 | * |
||
| 426 | * @param \phpbu\App\Result $result |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | 4 | protected function getInfoHtml(Result $result) |
|
| 513 | |||
| 514 | /** |
||
| 515 | * Return mail body footer. |
||
| 516 | * |
||
| 517 | * @return string |
||
| 518 | */ |
||
| 519 | 4 | protected function getFooterHtml() |
|
| 524 | } |
||
| 525 |