| Total Complexity | 47 |
| Total Lines | 368 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| 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 |
||
| 37 | class Emailing extends DataObject |
||
| 38 | { |
||
| 39 | private static $table_name = 'Emailing'; |
||
| 40 | |||
| 41 | private static $db = array( |
||
| 42 | 'Subject' => 'Varchar(255)', |
||
| 43 | 'Recipients' => 'Varchar(255)', |
||
| 44 | 'RecipientsList' => 'Text', |
||
| 45 | 'Sender' => 'Varchar(255)', |
||
| 46 | 'LastSent' => 'Datetime', |
||
| 47 | // Content |
||
| 48 | 'Content' => 'HTMLText', |
||
| 49 | 'Callout' => 'HTMLText', |
||
| 50 | ); |
||
| 51 | private static $summary_fields = array( |
||
| 52 | 'Subject', 'LastSent' |
||
| 53 | ); |
||
| 54 | private static $searchable_fields = array( |
||
| 55 | 'Subject', |
||
| 56 | ); |
||
| 57 | private static $translate = array( |
||
| 58 | 'Subject', 'Content', 'Callout' |
||
| 59 | ); |
||
| 60 | |||
| 61 | public function getTitle() |
||
| 64 | } |
||
| 65 | |||
| 66 | public function getCMSActions() |
||
| 80 | } |
||
| 81 | |||
| 82 | public function getCMSFields() |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return DataList |
||
| 110 | */ |
||
| 111 | public function getAllRecipients() |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * List of ids |
||
| 144 | * |
||
| 145 | * @return array |
||
| 146 | */ |
||
| 147 | public function getNormalizedRecipientsList() |
||
| 148 | { |
||
| 149 | $list = $this->RecipientsList; |
||
| 150 | |||
| 151 | $perLine = explode("\n", $list); |
||
| 152 | |||
| 153 | $arr = []; |
||
| 154 | foreach ($perLine as $line) { |
||
| 155 | $items = explode(',', $line); |
||
| 156 | foreach ($items as $item) { |
||
| 157 | // Prevent whitespaces from messing up our queries |
||
| 158 | $item = trim($item); |
||
| 159 | |||
| 160 | if (!$item) { |
||
| 161 | continue; |
||
| 162 | } |
||
| 163 | if (is_numeric($item)) { |
||
| 164 | $arr[] = $item; |
||
| 165 | } elseif (strpos($item, '@') !== false) { |
||
| 166 | $arr[] = DB::prepared_query("SELECT ID FROM Member WHERE Email = ?", [$item])->value(); |
||
| 167 | } else { |
||
| 168 | throw new Exception("Unprocessable item $item"); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | } |
||
| 172 | return $arr; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | public static function getMembersLocales() |
||
| 179 | { |
||
| 180 | return DB::query("SELECT DISTINCT Locale FROM Member")->column(); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return array |
||
| 185 | */ |
||
| 186 | public function listRecipients() |
||
| 187 | { |
||
| 188 | $arr = []; |
||
| 189 | $arr['ALL_MEMBERS'] = _t('Emailing.ALL_MEMBERS', 'All members'); |
||
| 190 | $arr['SELECTED_MEMBERS'] = _t('Emailing.SELECTED_MEMBERS', 'Selected members'); |
||
| 191 | $locales = self::getMembersLocales(); |
||
| 192 | foreach ($locales as $locale) { |
||
| 193 | $arr[$locale . '_MEMBERS'] = _t('Emailing.LOCALE_MEMBERS', '{locale} members', ['locale' => $locale]); |
||
| 194 | } |
||
| 195 | $this->extend("updateListRecipients", $arr, $locales); |
||
| 196 | return $arr; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Provide content for the Preview tab |
||
| 201 | * |
||
| 202 | * @return Tab |
||
| 203 | */ |
||
| 204 | protected function previewTab() |
||
| 227 | } |
||
| 228 | |||
| 229 | public function canView($member = null) |
||
| 230 | { |
||
| 231 | return true; |
||
| 232 | } |
||
| 233 | |||
| 234 | public function canEdit($member = null) |
||
| 237 | } |
||
| 238 | |||
| 239 | public function canCreate($member = null, $context = []) |
||
| 240 | { |
||
| 241 | return Permission::check('CMS_ACCESS', 'any', $member); |
||
| 242 | } |
||
| 243 | |||
| 244 | public function canDelete($member = null) |
||
| 245 | { |
||
| 246 | return Permission::check('CMS_ACCESS', 'any', $member); |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Get rendered body |
||
| 251 | * |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | public function renderTemplate() |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Collect all merge vars |
||
| 266 | * |
||
| 267 | * @return array |
||
| 268 | */ |
||
| 269 | public function collectMergeVars() |
||
| 270 | { |
||
| 271 | $fields = ['Subject', 'Content', 'Callout']; |
||
| 272 | |||
| 273 | $syntax = self::config()->mail_merge_syntax; |
||
| 274 | |||
| 275 | $regex = $syntax; |
||
| 276 | $regex = preg_quote($regex); |
||
| 277 | $regex = str_replace("MERGETAG", "([\w\.]+)", $regex); |
||
| 278 | |||
| 279 | $allMatches = []; |
||
| 280 | foreach ($fields as $field) { |
||
| 281 | $content = $this->$field; |
||
| 282 | $matches = []; |
||
| 283 | preg_match_all('/' . $regex . '/', $content, $matches); |
||
| 284 | if (!empty($matches[1])) { |
||
| 285 | $allMatches = array_merge($allMatches, $matches[1]); |
||
| 286 | } |
||
| 287 | } |
||
| 288 | |||
| 289 | return $allMatches; |
||
| 290 | } |
||
| 291 | |||
| 292 | |||
| 293 | /** |
||
| 294 | * Returns an instance of an Email with the content of the emailing |
||
| 295 | * |
||
| 296 | * @return BetterEmail |
||
| 297 | */ |
||
| 298 | public function getEmail() |
||
| 299 | { |
||
| 300 | $email = Email::create(); |
||
| 301 | if (!$email instanceof BetterEmail) { |
||
| 302 | throw new Exception("Make sure you are injecting the BetterEmail class instead of your base Email class"); |
||
| 303 | } |
||
| 304 | if ($this->Sender) { |
||
| 305 | $email->setFrom($this->Sender); |
||
| 306 | } |
||
| 307 | foreach ($this->getAllRecipients() as $r) { |
||
| 308 | $email->addBCC($r->Email, $r->FirstName . ' ' . $r->Surname); |
||
| 309 | } |
||
| 310 | $email->setSubject($this->Subject); |
||
| 311 | $email->addData('EmailContent', $this->Content); |
||
| 312 | $email->addData('Callout', $this->Callout); |
||
| 313 | return $email; |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Various email providers use various types of mail merge headers |
||
| 318 | * By default, we use mandrill that is expected to work for other platforms through compat layer |
||
| 319 | * |
||
| 320 | * X-Mailgun-Recipient-Variables: {"[email protected]": {"first":"Bob", "id":1}, "[email protected]": {"first":"Alice", "id": 2}} |
||
| 321 | * Template syntax: %recipient.first% |
||
| 322 | * @link https://documentation.mailgun.com/en/latest/user_manual.html#batch-sending |
||
| 323 | * |
||
| 324 | * X-MC-MergeVars [{"rcpt":"[email protected]","vars":[{"name":"merge2","content":"merge2 content"}]}] |
||
| 325 | * Template syntax: *|MERGETAG|* |
||
| 326 | * @link https://mandrill.zendesk.com/hc/en-us/articles/205582117-How-to-Use-SMTP-Headers-to-Customize-Your-Messages |
||
| 327 | * |
||
| 328 | * @link https://developers.sparkpost.com/api/smtp/#header-using-the-x-msys-api-custom-header |
||
| 329 | * |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | public function getMergeVarsHeader() |
||
| 333 | { |
||
| 334 | return self::config()->mail_merge_header; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Returns an array of emails with members by locale, grouped by a given number of recipients |
||
| 339 | * Some apis prevent sending too many emails at the same time |
||
| 340 | * |
||
| 341 | * @return array |
||
| 342 | */ |
||
| 343 | public function getEmailsByLocales() |
||
| 405 | } |
||
| 406 | } |
||
| 407 |