| Total Complexity | 54 |
| Total Lines | 410 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 1 |
Complex classes like Emailing 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.
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 Emailing, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class Emailing extends DataObject |
||
| 40 | { |
||
| 41 | private static $table_name = 'Emailing'; |
||
| 42 | |||
| 43 | private static $db = array( |
||
| 44 | 'Subject' => 'Varchar(255)', |
||
| 45 | 'Recipients' => 'Varchar(255)', |
||
| 46 | 'RecipientsList' => 'Text', |
||
| 47 | 'Sender' => 'Varchar(255)', |
||
| 48 | 'LastSent' => 'Datetime', |
||
| 49 | // Content |
||
| 50 | 'Content' => 'HTMLText', |
||
| 51 | 'Callout' => 'HTMLText', |
||
| 52 | ); |
||
| 53 | private static $summary_fields = array( |
||
| 54 | 'Subject', 'LastSent' |
||
| 55 | ); |
||
| 56 | private static $searchable_fields = array( |
||
| 57 | 'Subject', |
||
| 58 | ); |
||
| 59 | private static $translate = array( |
||
| 60 | 'Subject', 'Content', 'Callout' |
||
| 61 | ); |
||
| 62 | |||
| 63 | public function getTitle() |
||
| 64 | { |
||
| 65 | return $this->Subject; |
||
| 66 | } |
||
| 67 | |||
| 68 | public function getCMSActions() |
||
| 69 | { |
||
| 70 | $actions = parent::getCMSActions(); |
||
| 71 | $label = _t('Emailing.SEND', 'Send'); |
||
| 72 | $sure = _t('Emailing.SURE', 'Are you sure?'); |
||
| 73 | $onclick = 'return confirm(\'' . $sure . '\');'; |
||
| 74 | |||
| 75 | $sanitisedModel = str_replace('\\', '-', Emailing::class); |
||
| 76 | $adminSegment = EmailTemplatesAdmin::config()->url_segment; |
||
| 77 | $link = '/admin/' . $adminSegment . '/' . $sanitisedModel . '/SendEmailing/?id=' . $this->ID; |
||
| 78 | $btnContent = '<a href="' . $link . '" id="action_doSend" onclick="' . $onclick . '" class="btn action btn-info font-icon-angle-double-right">'; |
||
| 79 | $btnContent .= '<span class="btn__title">' . $label . '</span></a>'; |
||
| 80 | $actions->push(new LiteralField('doSend', $btnContent)); |
||
| 81 | return $actions; |
||
| 82 | } |
||
| 83 | |||
| 84 | public function getCMSFields() |
||
| 85 | { |
||
| 86 | $fields = parent::getCMSFields(); |
||
| 87 | |||
| 88 | // Do not allow changing subsite |
||
| 89 | $fields->removeByName('SubsiteID'); |
||
| 90 | |||
| 91 | // Recipients |
||
| 92 | $recipientsList = $this->listRecipients(); |
||
| 93 | $fields->replaceField('Recipients', $Recipients = new DropdownField('Recipients', null, $recipientsList)); |
||
| 94 | $Recipients->setDescription(_t('Emailing.EMAIL_COUNT', "Email will be sent to {count} members", ['count' => $this->getAllRecipients()->count()])); |
||
| 95 | |||
| 96 | $fields->dataFieldByName('Callout')->setRows(5); |
||
| 97 | |||
| 98 | if ($this->ID) { |
||
| 99 | $fields->addFieldToTab('Root.Preview', $this->previewTab()); |
||
| 100 | } |
||
| 101 | |||
| 102 | $fields->addFieldsToTab('Root.Settings', new TextField('Sender')); |
||
|
|
|||
| 103 | $fields->addFieldsToTab('Root.Settings', new ReadonlyField('LastSent')); |
||
| 104 | $fields->addFieldsToTab('Root.Settings', $RecipientsList = new TextareaField('RecipientsList')); |
||
| 105 | $RecipientsList->setDescription(_t('Emailing.RECIPIENTSLISTHELP', 'A list of IDs or emails on each line or separated by commas. Select "Selected members" to use this list')); |
||
| 106 | |||
| 107 | if ($this->ID) { |
||
| 108 | $invalidRecipients = $this->listRecipientsWithInvalidEmails(); |
||
| 109 | if (!empty($invalidRecipients)) { |
||
| 110 | $invalidRecipientsContent = ''; |
||
| 111 | foreach ($invalidRecipients as $ir) { |
||
| 112 | $invalidRecipientsContent .= "<h3>" . $ir->FirstName . ' ' . $ir->Surname . '</h3>'; |
||
| 113 | $invalidRecipientsContent .= "<p>" . $ir->Email . "</p>"; |
||
| 114 | $invalidRecipientsContent .= "<hr/>"; |
||
| 115 | } |
||
| 116 | $fields->addFieldsToTab('Root.InvalidRecipients', new LiteralField("InvalidRecipients", $invalidRecipientsContent)); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | return $fields; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return DataList |
||
| 125 | */ |
||
| 126 | public function getAllRecipients() |
||
| 127 | { |
||
| 128 | $list = null; |
||
| 129 | $locales = self::getMembersLocales(); |
||
| 130 | foreach ($locales as $locale) { |
||
| 131 | if ($this->Recipients == $locale . '_MEMBERS') { |
||
| 132 | $list = Member::get()->filter('Locale', $locale); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | $recipients = $this->Recipients; |
||
| 136 | if (!$list) { |
||
| 137 | switch ($recipients) { |
||
| 138 | case 'ALL_MEMBERS': |
||
| 139 | $list = Member::get()->exclude('Email', ''); |
||
| 140 | break; |
||
| 141 | case 'SELECTED_MEMBERS': |
||
| 142 | $IDs = $this->getNormalizedRecipientsList(); |
||
| 143 | if (empty($IDs)) { |
||
| 144 | $IDs = 0; |
||
| 145 | } |
||
| 146 | $list = Member::get()->filter('ID', $IDs); |
||
| 147 | break; |
||
| 148 | default: |
||
| 149 | $list = Member::get()->filter('ID', 0); |
||
| 150 | break; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | $this->extend('updateGetAllRecipients', $list, $locales, $recipients); |
||
| 154 | return $list; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * List all invalid recipients |
||
| 159 | * |
||
| 160 | * @return array |
||
| 161 | */ |
||
| 162 | public function listRecipientsWithInvalidEmails() |
||
| 163 | { |
||
| 164 | $list = []; |
||
| 165 | foreach ($this->getAllRecipients() as $r) { |
||
| 166 | $res = Swift_Validate::email($r->Email); |
||
| 167 | if (!$res) { |
||
| 168 | $list[] = $r; |
||
| 169 | } |
||
| 170 | } |
||
| 171 | return $list; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * List of ids |
||
| 176 | * |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | public function getNormalizedRecipientsList() |
||
| 180 | { |
||
| 181 | $list = $this->RecipientsList; |
||
| 182 | |||
| 183 | $perLine = explode("\n", $list); |
||
| 184 | |||
| 185 | $arr = []; |
||
| 186 | foreach ($perLine as $line) { |
||
| 187 | $items = explode(',', $line); |
||
| 188 | foreach ($items as $item) { |
||
| 189 | // Prevent whitespaces from messing up our queries |
||
| 190 | $item = trim($item); |
||
| 191 | |||
| 192 | if (!$item) { |
||
| 193 | continue; |
||
| 194 | } |
||
| 195 | if (is_numeric($item)) { |
||
| 196 | $arr[] = $item; |
||
| 197 | } elseif (strpos($item, '@') !== false) { |
||
| 198 | $arr[] = DB::prepared_query("SELECT ID FROM Member WHERE Email = ?", [$item])->value(); |
||
| 199 | } else { |
||
| 200 | throw new Exception("Unprocessable item $item"); |
||
| 201 | } |
||
| 202 | } |
||
| 203 | } |
||
| 204 | return $arr; |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @return array |
||
| 209 | */ |
||
| 210 | public static function getMembersLocales() |
||
| 211 | { |
||
| 212 | return DB::query("SELECT DISTINCT Locale FROM Member")->column(); |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @return array |
||
| 217 | */ |
||
| 218 | public function listRecipients() |
||
| 219 | { |
||
| 220 | $arr = []; |
||
| 221 | $arr['ALL_MEMBERS'] = _t('Emailing.ALL_MEMBERS', 'All members'); |
||
| 222 | $arr['SELECTED_MEMBERS'] = _t('Emailing.SELECTED_MEMBERS', 'Selected members'); |
||
| 223 | $locales = self::getMembersLocales(); |
||
| 224 | foreach ($locales as $locale) { |
||
| 225 | $arr[$locale . '_MEMBERS'] = _t('Emailing.LOCALE_MEMBERS', '{locale} members', ['locale' => $locale]); |
||
| 226 | } |
||
| 227 | $this->extend("updateListRecipients", $arr, $locales); |
||
| 228 | return $arr; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Provide content for the Preview tab |
||
| 233 | * |
||
| 234 | * @return Tab |
||
| 235 | */ |
||
| 236 | protected function previewTab() |
||
| 259 | } |
||
| 260 | |||
| 261 | public function canView($member = null) |
||
| 262 | { |
||
| 263 | return true; |
||
| 264 | } |
||
| 265 | |||
| 266 | public function canEdit($member = null) |
||
| 269 | } |
||
| 270 | |||
| 271 | public function canCreate($member = null, $context = []) |
||
| 272 | { |
||
| 273 | return Permission::check('CMS_ACCESS', 'any', $member); |
||
| 274 | } |
||
| 275 | |||
| 276 | public function canDelete($member = null) |
||
| 277 | { |
||
| 278 | return Permission::check('CMS_ACCESS', 'any', $member); |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Get rendered body |
||
| 283 | * |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | public function renderTemplate() |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Collect all merge vars |
||
| 298 | * |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | public function collectMergeVars() |
||
| 302 | { |
||
| 303 | $fields = ['Subject', 'Content', 'Callout']; |
||
| 304 | |||
| 305 | $syntax = self::config()->mail_merge_syntax; |
||
| 306 | |||
| 307 | $regex = $syntax; |
||
| 308 | $regex = preg_quote($regex); |
||
| 309 | $regex = str_replace("MERGETAG", "([\w\.]+)", $regex); |
||
| 310 | |||
| 311 | $allMatches = []; |
||
| 312 | foreach ($fields as $field) { |
||
| 313 | $content = $this->$field; |
||
| 314 | $matches = []; |
||
| 315 | preg_match_all('/' . $regex . '/', $content, $matches); |
||
| 316 | if (!empty($matches[1])) { |
||
| 317 | $allMatches = array_merge($allMatches, $matches[1]); |
||
| 318 | } |
||
| 319 | } |
||
| 320 | |||
| 321 | return $allMatches; |
||
| 322 | } |
||
| 323 | |||
| 324 | |||
| 325 | /** |
||
| 326 | * Returns an instance of an Email with the content of the emailing |
||
| 327 | * |
||
| 328 | * @return BetterEmail |
||
| 329 | */ |
||
| 330 | public function getEmail() |
||
| 331 | { |
||
| 332 | $email = Email::create(); |
||
| 333 | if (!$email instanceof BetterEmail) { |
||
| 334 | throw new Exception("Make sure you are injecting the BetterEmail class instead of your base Email class"); |
||
| 335 | } |
||
| 336 | if ($this->Sender) { |
||
| 337 | $senderEmail = EmailUtils::get_email_from_rfc_email($this->Sender); |
||
| 338 | $senderName = EmailUtils::get_displayname_from_rfc_email($this->Sender); |
||
| 339 | $email->setFrom($senderEmail, $senderName); |
||
| 340 | } |
||
| 341 | foreach ($this->getAllRecipients() as $r) { |
||
| 342 | $email->addBCC($r->Email, $r->FirstName . ' ' . $r->Surname); |
||
| 343 | } |
||
| 344 | $email->setSubject($this->Subject); |
||
| 345 | $email->addData('EmailContent', $this->Content); |
||
| 346 | $email->addData('Callout', $this->Callout); |
||
| 347 | $email->addData('IsEmailing', true); |
||
| 348 | return $email; |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Various email providers use various types of mail merge headers |
||
| 353 | * By default, we use mandrill that is expected to work for other platforms through compat layer |
||
| 354 | * |
||
| 355 | * X-Mailgun-Recipient-Variables: {"[email protected]": {"first":"Bob", "id":1}, "[email protected]": {"first":"Alice", "id": 2}} |
||
| 356 | * Template syntax: %recipient.first% |
||
| 357 | * @link https://documentation.mailgun.com/en/latest/user_manual.html#batch-sending |
||
| 358 | * |
||
| 359 | * X-MC-MergeVars [{"rcpt":"[email protected]","vars":[{"name":"merge2","content":"merge2 content"}]}] |
||
| 360 | * Template syntax: *|MERGETAG|* |
||
| 361 | * @link https://mandrill.zendesk.com/hc/en-us/articles/205582117-How-to-Use-SMTP-Headers-to-Customize-Your-Messages |
||
| 362 | * |
||
| 363 | * @link https://developers.sparkpost.com/api/smtp/#header-using-the-x-msys-api-custom-header |
||
| 364 | * |
||
| 365 | * @return string |
||
| 366 | */ |
||
| 367 | public function getMergeVarsHeader() |
||
| 368 | { |
||
| 369 | return self::config()->mail_merge_header; |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Returns an array of emails with members by locale, grouped by a given number of recipients |
||
| 374 | * Some apis prevent sending too many emails at the same time |
||
| 375 | * |
||
| 376 | * @return array |
||
| 377 | */ |
||
| 378 | public function getEmailsByLocales() |
||
| 449 | } |
||
| 450 | } |
||
| 451 |