Complex classes like EmailReminder_NotificationSchedule 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 EmailReminder_NotificationSchedule, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class EmailReminder_NotificationSchedule extends DataObject |
||
| 4 | { |
||
| 5 | |||
| 6 | /** |
||
| 7 | * @var int |
||
| 8 | */ |
||
| 9 | private static $grace_days = 10; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * @var string |
||
| 13 | */ |
||
| 14 | private static $default_data_object = 'Member'; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | private static $default_date_field = ''; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | private static $default_email_field = ''; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | private static $replaceable_record_fields = array('FirstName', 'Surname', 'Email'); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | private static $include_method = 'EmailReminderInclude'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | private static $exclude_method = 'EmailReminderExclude'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private static $mail_out_class = 'EmailReminder_DailyMailOut'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | private static $disabled_checkbox_label = 'Disable'; |
||
| 50 | |||
| 51 | private static $singular_name = 'Email Reminder Schedule'; |
||
|
|
|||
| 52 | public function i18n_singular_name() |
||
| 56 | |||
| 57 | private static $plural_name = 'Email Reminder Schedules'; |
||
| 58 | public function i18n_plural_name() |
||
| 62 | |||
| 63 | private static $db = array( |
||
| 64 | 'DataObject' => 'Varchar(100)', |
||
| 65 | 'EmailField' => 'Varchar(100)', |
||
| 66 | 'DateField' => 'Varchar(100)', |
||
| 67 | 'Days' => 'Int', |
||
| 68 | 'RepeatDays' => 'Int', |
||
| 69 | 'BeforeAfter' => "Enum('before,after,immediately','before')", |
||
| 70 | 'EmailFrom' => 'Varchar(100)', |
||
| 71 | 'EmailSubject' => 'Varchar(100)', |
||
| 72 | 'Content' => 'HTMLText', |
||
| 73 | 'Disable' => 'Boolean', |
||
| 74 | 'SendTestTo' => 'Text' |
||
| 75 | ); |
||
| 76 | |||
| 77 | |||
| 78 | private static $has_many = array( |
||
| 79 | 'EmailsSent' => 'EmailReminder_EmailRecord' |
||
| 80 | ); |
||
| 81 | |||
| 82 | private static $summary_fields = array( |
||
| 83 | 'EmailSubject', |
||
| 84 | 'BeforeAfter', |
||
| 85 | 'Days' |
||
| 86 | ); |
||
| 87 | |||
| 88 | private static $field_labels = array( |
||
| 89 | 'DataObject' => 'Class/Table', |
||
| 90 | 'Days' => 'Days Before/After Date', |
||
| 91 | 'RepeatDays' => 'Repeat cycle days', |
||
| 92 | 'BeforeAfter' => 'When to send' |
||
| 93 | ); |
||
| 94 | |||
| 95 | public function populateDefaults() |
||
| 107 | |||
| 108 | public function getCMSFields() |
||
| 109 | { |
||
| 110 | $fields = parent::getCMSFields(); |
||
| 111 | |||
| 112 | $emailsSentField = $fields->dataFieldByName('EmailsSent'); |
||
| 113 | $fields->removeFieldFromTab('Root', 'EmailsSent'); |
||
| 114 | |||
| 115 | $disableLabel = $this->Config()->get('disabled_checkbox_label'); |
||
| 116 | $fields->addFieldToTab( |
||
| 117 | 'Root.Main', |
||
| 118 | CheckboxField::create('Disable', $disableLabel)->setDescription("If checked this email will not be sent during the daily mail out, however it can still be sent programatically") |
||
| 119 | ); |
||
| 120 | $fields->addFieldToTab( |
||
| 121 | 'Root.Main', |
||
| 122 | $dataObjecField = DropdownField::create( |
||
| 123 | 'DataObject', |
||
| 124 | 'Table/Class Name', |
||
| 125 | $this->dataObjectOptions() |
||
| 126 | ) |
||
| 127 | ->setRightTitle('Type a valid table/class name') |
||
| 128 | ); |
||
| 129 | if ($this->Config()->get('default_data_object')) { |
||
| 130 | $fields->replaceField('DataObject', $dataObjecField->performReadonlyTransformation()); |
||
| 131 | } |
||
| 132 | |||
| 133 | |||
| 134 | |||
| 135 | $fields->addFieldToTab( |
||
| 136 | 'Root.Main', |
||
| 137 | $emailFieldField = DropdownField::create( |
||
| 138 | 'EmailField', |
||
| 139 | 'Email Field', |
||
| 140 | $this->emailFieldOptions() |
||
| 141 | ) |
||
| 142 | ->setRightTitle('Select the field that will contain a valid email address') |
||
| 143 | ->setEmptyString('[ Please select ]') |
||
| 144 | ); |
||
| 145 | if ($this->Config()->get('default_email_field')) { |
||
| 146 | $fields->replaceField('EmailField', $emailFieldField->performReadonlyTransformation()); |
||
| 147 | } |
||
| 148 | |||
| 149 | $fields->addFieldToTab( |
||
| 150 | 'Root.Main', |
||
| 151 | $dateFieldField = DropdownField::create( |
||
| 152 | 'DateField', |
||
| 153 | 'Date Field', |
||
| 154 | $this->dateFieldOptions() |
||
| 155 | ) |
||
| 156 | ->setRightTitle('Select a valid Date field to calculate when reminders should be sent') |
||
| 157 | ->setEmptyString('[ Please select ]') |
||
| 158 | ); |
||
| 159 | if ($this->Config()->get('default_date_field')) { |
||
| 160 | $fields->replaceField('DateField', $dateFieldField->performReadonlyTransformation()); |
||
| 161 | } |
||
| 162 | |||
| 163 | $fields->removeFieldsFromTab( |
||
| 164 | 'Root.Main', |
||
| 165 | array('Days', 'BeforeAfter', 'RepeatDays') |
||
| 166 | ); |
||
| 167 | $fields->addFieldsToTab( |
||
| 168 | 'Root.Main', |
||
| 169 | array( |
||
| 170 | DropdownField::create('BeforeAfter', 'Before / After Expiration', array('before' => 'before', 'after' => 'after', 'immediately' => 'immediately')) |
||
| 171 | ->setRightTitle('Are the days listed above before or after the actual expiration date.'), |
||
| 172 | NumericField::create('Days', 'Days') |
||
| 173 | ->setRightTitle('How many days in advance (before) or in arrears (after) of the expiration date should this email be sent? </br>This field is ignored if set to send immediately.'), |
||
| 174 | NumericField::create('RepeatDays', 'Repeat Cycle Days') |
||
| 175 | ->setRightTitle( |
||
| 176 | ' |
||
| 177 | Number of days after which the same reminder can be sent to the same email address. |
||
| 178 | <br />We allow an e-mail to be sent to one specific email address for one specific reminder only once. |
||
| 179 | <br />In this field you can indicate for how long we will apply this rule.' |
||
| 180 | ) |
||
| 181 | ) |
||
| 182 | ); |
||
| 183 | |||
| 184 | if ($this->BeforeAfter === 'immediately') { |
||
| 185 | $fields->removeFieldsFromTab( |
||
| 186 | 'Root.Main', |
||
| 187 | array('Days', 'RepeatDays') |
||
| 188 | ); |
||
| 189 | } |
||
| 190 | |||
| 191 | $fields->addFieldsToTab( |
||
| 192 | 'Root.EmailContent', |
||
| 193 | array( |
||
| 194 | TextField::create('EmailFrom', 'Email From Address') |
||
| 195 | ->setRightTitle('The email from address, eg: "My Company <[email protected]>"'), |
||
| 196 | $subjectField = TextField::create('EmailSubject', 'Email Subject Line') |
||
| 197 | ->setRightTitle('The subject of the email'), |
||
| 198 | $contentField = HTMLEditorField::create('Content', 'Email Content') |
||
| 199 | ->SetRows(20) |
||
| 200 | ) |
||
| 201 | ); |
||
| 202 | if ($obj = $this->getReplacerObject()) { |
||
| 203 | $html = $obj->replaceHelpList($asHTML = true); |
||
| 204 | $otherFieldsThatCanBeUsed = $this->getFieldsFromDataObject(array('*')); |
||
| 205 | $replaceableFields = $this->Config()->get('replaceable_record_fields'); |
||
| 206 | if (count($otherFieldsThatCanBeUsed)) { |
||
| 207 | $html .= '<h3>You can also use the record fields (not replaced in tests):</h3><ul>'; |
||
| 208 | foreach ($otherFieldsThatCanBeUsed as $key => $value) { |
||
| 209 | if (in_array($key, $replaceableFields)) { |
||
| 210 | $html .= '<li><strong>$'.$key.'</strong> <span>'.$value.'</span></li>'; |
||
| 211 | } |
||
| 212 | } |
||
| 213 | } |
||
| 214 | $html .= '</ul>'; |
||
| 215 | $subjectField->setRightTitle('for replacement options, please see below ...'); |
||
| 216 | $contentField->setRightTitle($html); |
||
| 217 | } |
||
| 218 | $fields->addFieldsToTab( |
||
| 219 | 'Root.Sent', |
||
| 220 | array( |
||
| 221 | TextareaField::create('SendTestTo', 'Send test email to ...') |
||
| 222 | ->setRightTitle( |
||
| 223 | ' |
||
| 224 | Separate emails by commas, a test email will be sent every time you save this Email Reminder, if you do not want test emails to be sent make sure this field is empty |
||
| 225 | ' |
||
| 226 | ) |
||
| 227 | ->SetRows(3) |
||
| 228 | ) |
||
| 229 | ); |
||
| 230 | if ($emailsSentField) { |
||
| 231 | $config = $emailsSentField->getConfig(); |
||
| 232 | $config->removeComponentsByType('GridFieldAddExistingAutocompleter'); |
||
| 233 | $fields->addFieldToTab( |
||
| 234 | 'Root.Sent', |
||
| 235 | $emailsSentField |
||
| 236 | ); |
||
| 237 | } |
||
| 238 | $records = $this->CurrentRecords(); |
||
| 239 | if ($records && !$this->Disable) { |
||
| 240 | $fields->addFieldsToTab( |
||
| 241 | 'Root.Review', |
||
| 242 | array( |
||
| 243 | GridField::create( |
||
| 244 | 'CurrentRecords', |
||
| 245 | 'Today we are sending to ...', |
||
| 246 | $records |
||
| 247 | ), |
||
| 248 | LiteralField::create( |
||
| 249 | 'SampleSelectStatement', |
||
| 250 | '<h3>Here is a sample statement used to select records:</h3> |
||
| 251 | <pre>'.$this->whereStatementForDays().'</pre>' |
||
| 252 | ), |
||
| 253 | LiteralField::create( |
||
| 254 | 'SampleFieldDataForRecords', |
||
| 255 | '<h3>sample of '.$this->DateField.' field values:</h3> |
||
| 256 | <li>'.implode('</li><li>', $this->SampleFieldDataForRecords()).'</li>' |
||
| 257 | ) |
||
| 258 | ) |
||
| 259 | ); |
||
| 260 | } |
||
| 261 | return $fields; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @return array |
||
| 266 | */ |
||
| 267 | protected function dataObjectOptions() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @return array |
||
| 274 | */ |
||
| 275 | protected function emailFieldOptions() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @return array |
||
| 282 | */ |
||
| 283 | protected function dateFieldOptions() |
||
| 287 | |||
| 288 | |||
| 289 | /** |
||
| 290 | * list of database fields available |
||
| 291 | * @param array $fieldTypeMatchArray - strpos filter |
||
| 292 | * @return array |
||
| 293 | */ |
||
| 294 | protected function getFieldsFromDataObject($fieldTypeMatchArray = array()) |
||
| 318 | |||
| 319 | |||
| 320 | /** |
||
| 321 | * Test if valid classname has been set |
||
| 322 | * @param null |
||
| 323 | * @return Boolean |
||
| 324 | */ |
||
| 325 | public function hasValidDataObject() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Test if valid fields have been set |
||
| 332 | * @param null |
||
| 333 | * @return Boolean |
||
| 334 | */ |
||
| 335 | public function hasValidDataObjectFields() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | public function getTitle() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @return boolean |
||
| 363 | */ |
||
| 364 | public function hasValidFields() |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @return boolean |
||
| 380 | */ |
||
| 381 | public function validate() |
||
| 395 | |||
| 396 | public function onBeforeWrite() |
||
| 407 | |||
| 408 | public function onAfterWrite() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * |
||
| 422 | * @return null | EmailReminder_ReplacerClassInterface |
||
| 423 | */ |
||
| 424 | public function getReplacerObject() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * |
||
| 433 | * @return null | ScheduledTask |
||
| 434 | */ |
||
| 435 | public function getMailOutObject() |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @param int $limit |
||
| 450 | * @return array |
||
| 451 | */ |
||
| 452 | public function SampleFieldDataForRecords($limit = 200) |
||
| 466 | |||
| 467 | public function CMSEditLink() |
||
| 473 | |||
| 474 | |||
| 475 | /** |
||
| 476 | * @return DataList | null |
||
| 477 | */ |
||
| 478 | public function CurrentRecords() |
||
| 535 | |||
| 536 | /** |
||
| 537 | * BeforeAfter = 'after' |
||
| 538 | * Days = 3 |
||
| 539 | * GraceDays = 2 |
||
| 540 | * -> minDays = -5 days start of day |
||
| 541 | * -> maxDays = -3 days end of day |
||
| 542 | * |
||
| 543 | * @return string |
||
| 544 | */ |
||
| 545 | protected function whereStatementForDays() |
||
| 568 | } |
||
| 569 |