Total Complexity | 54 |
Total Lines | 573 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like EmailRecipient 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 EmailRecipient, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
48 | class EmailRecipient extends DataObject |
||
49 | { |
||
50 | private static $db = [ |
||
51 | 'EmailAddress' => 'Varchar(200)', |
||
52 | 'EmailSubject' => 'Varchar(200)', |
||
53 | 'EmailFrom' => 'Varchar(200)', |
||
54 | 'EmailReplyTo' => 'Varchar(200)', |
||
55 | 'EmailBody' => 'Text', |
||
56 | 'EmailBodyHtml' => 'HTMLText', |
||
57 | 'EmailTemplate' => 'Varchar', |
||
58 | 'SendPlain' => 'Boolean', |
||
59 | 'HideFormData' => 'Boolean', |
||
60 | 'CustomRulesCondition' => 'Enum("And,Or")' |
||
61 | ]; |
||
62 | |||
63 | private static $has_one = [ |
||
64 | 'Form' => DataObject::class, |
||
65 | 'SendEmailFromField' => EditableFormField::class, |
||
66 | 'SendEmailToField' => EditableFormField::class, |
||
67 | 'SendEmailSubjectField' => EditableFormField::class |
||
68 | ]; |
||
69 | |||
70 | private static $has_many = [ |
||
71 | 'CustomRules' => EmailRecipientCondition::class, |
||
72 | ]; |
||
73 | |||
74 | private static $owns = [ |
||
75 | 'CustomRules', |
||
76 | ]; |
||
77 | |||
78 | private static $cascade_deetes = [ |
||
79 | 'CustomRules', |
||
80 | ]; |
||
81 | |||
82 | private static $summary_fields = [ |
||
83 | 'EmailAddress', |
||
84 | 'EmailSubject', |
||
85 | 'EmailFrom' |
||
86 | ]; |
||
87 | |||
88 | private static $table_name = 'UserDefinedForm_EmailRecipient'; |
||
89 | |||
90 | /** |
||
91 | * Setting this to true will allow you to select "risky" fields as |
||
92 | * email recipient, such as free-text entry fields. |
||
93 | * |
||
94 | * It's advisable to leave this off. |
||
95 | * |
||
96 | * @config |
||
97 | * @var bool |
||
98 | */ |
||
99 | private static $allow_unbound_recipient_fields = false; |
||
100 | |||
101 | public function requireDefaultRecords() |
||
102 | { |
||
103 | parent::requireDefaultRecords(); |
||
104 | |||
105 | // make sure to migrate the class across (prior to v5.x) |
||
106 | DB::query("UPDATE UserDefinedForm_EmailRecipient SET FormClass = 'Page' WHERE FormClass IS NULL"); |
||
107 | } |
||
108 | |||
109 | public function summaryFields() |
||
110 | { |
||
111 | $fields = parent::summaryFields(); |
||
112 | if (isset($fields['EmailAddress'])) { |
||
113 | /** @skipUpgrade */ |
||
114 | $fields['EmailAddress'] = _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.EMAILADDRESS', 'Email'); |
||
115 | } |
||
116 | if (isset($fields['EmailSubject'])) { |
||
117 | $fields['EmailSubject'] = _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.EMAILSUBJECT', 'Subject'); |
||
118 | } |
||
119 | if (isset($fields['EmailFrom'])) { |
||
120 | $fields['EmailFrom'] = _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.EMAILFROM', 'From'); |
||
121 | } |
||
122 | return $fields; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Get instance of UserDefinedForm when editing in getCMSFields |
||
127 | * |
||
128 | * @return UserDefinedForm |
||
129 | */ |
||
130 | protected function getFormParent() |
||
131 | { |
||
132 | // LeftAndMain::sessionNamespace is protected. @todo replace this with a non-deprecated equivalent. |
||
133 | $sessionNamespace = $this->config()->get('session_namespace') ?: CMSMain::class; |
||
134 | |||
135 | $formID = $this->FormID |
||
|
|||
136 | ? $this->FormID |
||
137 | : Controller::curr()->getRequest()->getSession()->get($sessionNamespace . '.currentPage'); |
||
138 | return UserDefinedForm::get()->byID($formID); |
||
139 | } |
||
140 | |||
141 | public function getTitle() |
||
142 | { |
||
143 | if ($this->EmailAddress) { |
||
144 | return $this->EmailAddress; |
||
145 | } |
||
146 | if ($this->EmailSubject) { |
||
147 | return $this->EmailSubject; |
||
148 | } |
||
149 | return parent::getTitle(); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Generate a gridfield config for editing filter rules |
||
154 | * |
||
155 | * @return GridFieldConfig |
||
156 | */ |
||
157 | protected function getRulesConfig() |
||
158 | { |
||
159 | $formFields = $this->getFormParent()->Fields(); |
||
160 | |||
161 | $config = GridFieldConfig::create() |
||
162 | ->addComponents( |
||
163 | new GridFieldButtonRow('before'), |
||
164 | new GridFieldToolbarHeader(), |
||
165 | new GridFieldAddNewInlineButton(), |
||
166 | new GridFieldDeleteAction(), |
||
167 | $columns = new GridFieldEditableColumns() |
||
168 | ); |
||
169 | |||
170 | $columns->setDisplayFields(array( |
||
171 | 'ConditionFieldID' => function ($record, $column, $grid) use ($formFields) { |
||
172 | return DropdownField::create($column, false, $formFields->map('ID', 'Title')); |
||
173 | }, |
||
174 | 'ConditionOption' => function ($record, $column, $grid) { |
||
175 | $options = EmailRecipientCondition::config()->condition_options; |
||
176 | return DropdownField::create($column, false, $options); |
||
177 | }, |
||
178 | 'ConditionValue' => function ($record, $column, $grid) { |
||
179 | return TextField::create($column); |
||
180 | } |
||
181 | )); |
||
182 | |||
183 | return $config; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * @return FieldList |
||
188 | */ |
||
189 | public function getCMSFields() |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | * Return whether a user can create an object of this type |
||
411 | * |
||
412 | * @param Member $member |
||
413 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
414 | * @return bool |
||
415 | */ |
||
416 | public function canCreate($member = null, $context = []) |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * Helper method to check the parent for this object |
||
430 | * |
||
431 | * @param array $args List of arguments passed to canCreate |
||
432 | * @return SiteTree Parent page instance |
||
433 | */ |
||
434 | protected function getCanCreateContext($args) |
||
447 | } |
||
448 | |||
449 | public function canView($member = null) |
||
450 | { |
||
451 | if ($form = $this->getFormParent()) { |
||
452 | return $form->canView($member); |
||
453 | } |
||
454 | return parent::canView($member); |
||
455 | } |
||
456 | |||
457 | public function canEdit($member = null) |
||
458 | { |
||
459 | if ($form = $this->getFormParent()) { |
||
460 | return $form->canEdit($member); |
||
461 | } |
||
462 | |||
463 | return parent::canEdit($member); |
||
464 | } |
||
465 | |||
466 | /** |
||
467 | * @param Member |
||
468 | * |
||
469 | * @return boolean |
||
470 | */ |
||
471 | public function canDelete($member = null) |
||
472 | { |
||
473 | return $this->canEdit($member); |
||
474 | } |
||
475 | |||
476 | /** |
||
477 | * Determine if this recipient may receive notifications for this submission |
||
478 | * |
||
479 | * @param array $data |
||
480 | * @param Form $form |
||
481 | * @return bool |
||
482 | */ |
||
483 | public function canSend($data, $form) |
||
484 | { |
||
485 | // Skip if no rules configured |
||
486 | $customRules = $this->CustomRules(); |
||
487 | if (!$customRules->count()) { |
||
488 | return true; |
||
489 | } |
||
490 | |||
491 | // Check all rules |
||
492 | $isAnd = $this->CustomRulesCondition === 'And'; |
||
493 | foreach ($customRules as $customRule) { |
||
494 | /** @var EmailRecipientCondition $customRule */ |
||
495 | $matches = $customRule->matches($data); |
||
496 | if ($isAnd && !$matches) { |
||
497 | return false; |
||
498 | } |
||
499 | if (!$isAnd && $matches) { |
||
500 | return true; |
||
501 | } |
||
502 | } |
||
503 | |||
504 | // Once all rules are checked |
||
505 | return $isAnd; |
||
506 | } |
||
507 | |||
508 | /** |
||
509 | * Make sure the email template saved against the recipient exists on the file system. |
||
510 | * |
||
511 | * @param string |
||
512 | * |
||
513 | * @return boolean |
||
514 | */ |
||
515 | public function emailTemplateExists($template = '') |
||
516 | { |
||
517 | $t = ($template ? $template : $this->EmailTemplate); |
||
518 | |||
519 | return array_key_exists($t, (array) $this->getEmailTemplateDropdownValues()); |
||
520 | } |
||
521 | |||
522 | /** |
||
523 | * Get the email body for the current email format |
||
524 | * |
||
525 | * @return string |
||
526 | */ |
||
527 | public function getEmailBodyContent() |
||
533 | } |
||
534 | |||
535 | /** |
||
536 | * Gets a list of email templates suitable for populating the email template dropdown. |
||
537 | * |
||
538 | * @return array |
||
539 | */ |
||
540 | public function getEmailTemplateDropdownValues() |
||
584 | } |
||
585 | |||
586 | /** |
||
587 | * Validate that valid email addresses are being used |
||
588 | * |
||
589 | * @return ValidationResult |
||
590 | */ |
||
591 | public function validate() |
||
623 |