| Total Complexity | 45 |
| Total Lines | 298 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like EditableCustomRule 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 EditableCustomRule, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class EditableCustomRule extends DataObject |
||
| 26 | { |
||
| 27 | private static $condition_options = [ |
||
|
|
|||
| 28 | 'IsBlank' => 'Is blank', |
||
| 29 | 'IsNotBlank' => 'Is not blank', |
||
| 30 | 'HasValue' => 'Equals', |
||
| 31 | 'ValueNot' => 'Doesn\'t equal', |
||
| 32 | 'ValueLessThan' => 'Less than', |
||
| 33 | 'ValueLessThanEqual' => 'Less than or equal', |
||
| 34 | 'ValueGreaterThan' => 'Greater than', |
||
| 35 | 'ValueGreaterThanEqual' => 'Greater than or equal' |
||
| 36 | ]; |
||
| 37 | |||
| 38 | private static $db = [ |
||
| 39 | 'Display' => 'Enum("Show,Hide")', |
||
| 40 | 'ConditionOption' => 'Enum("IsBlank,IsNotBlank,HasValue,ValueNot,ValueLessThan,ValueLessThanEqual,ValueGreaterThan,ValueGreaterThanEqual")', |
||
| 41 | 'FieldValue' => 'Varchar(255)' |
||
| 42 | ]; |
||
| 43 | |||
| 44 | private static $has_one = [ |
||
| 45 | 'Parent' => EditableFormField::class, |
||
| 46 | 'ConditionField' => EditableFormField::class |
||
| 47 | ]; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Built in extensions required |
||
| 51 | * |
||
| 52 | * @config |
||
| 53 | * @var array |
||
| 54 | 1 | */ |
|
| 55 | private static $extensions = [ |
||
| 56 | 1 | Versioned::class . "('Stage', 'Live')" |
|
| 57 | 1 | ]; |
|
| 58 | |||
| 59 | private static $table_name = 'EditableCustomRule'; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param Member $member |
||
| 63 | * @return bool |
||
| 64 | */ |
||
| 65 | public function canDelete($member = null) |
||
| 66 | { |
||
| 67 | return $this->canEdit($member); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param Member $member |
||
| 72 | * @return bool |
||
| 73 | */ |
||
| 74 | public function canEdit($member = null) |
||
| 75 | { |
||
| 76 | return $this->Parent()->canEdit($member); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param Member $member |
||
| 81 | * @return bool |
||
| 82 | */ |
||
| 83 | public function canView($member = null) |
||
| 84 | { |
||
| 85 | return $this->Parent()->canView($member); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Return whether a user can create an object of this type |
||
| 90 | * |
||
| 91 | * @param Member $member |
||
| 92 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
| 93 | * @return bool |
||
| 94 | */ |
||
| 95 | public function canCreate($member = null, $context = []) |
||
| 96 | { |
||
| 97 | // Check parent page |
||
| 98 | $parent = $this->getCanCreateContext(func_get_args()); |
||
| 99 | if ($parent) { |
||
| 100 | return $parent->canEdit($member); |
||
| 101 | } |
||
| 102 | |||
| 103 | // Fall back to secure admin permissions |
||
| 104 | return parent::canCreate($member); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Helper method to check the parent for this object |
||
| 109 | * |
||
| 110 | * @param array $args List of arguments passed to canCreate |
||
| 111 | * @return DataObject Some parent dataobject to inherit permissions from |
||
| 112 | */ |
||
| 113 | protected function getCanCreateContext($args) |
||
| 114 | { |
||
| 115 | // Inspect second parameter to canCreate for a 'Parent' context |
||
| 116 | if (isset($args[1]['Parent'])) { |
||
| 117 | return $args[1]['Parent']; |
||
| 118 | } |
||
| 119 | // Hack in currently edited page if context is missing |
||
| 120 | if (Controller::has_curr() && Controller::curr() instanceof CMSMain) { |
||
| 121 | return Controller::curr()->currentPage(); |
||
| 122 | } |
||
| 123 | |||
| 124 | // No page being edited |
||
| 125 | return null; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param Member $member |
||
| 130 | * @return bool |
||
| 131 | */ |
||
| 132 | public function canPublish($member = null) |
||
| 133 | { |
||
| 134 | return $this->canEdit($member); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param Member $member |
||
| 139 | * @return bool |
||
| 140 | */ |
||
| 141 | public function canUnpublish($member = null) |
||
| 142 | { |
||
| 143 | return $this->canDelete($member); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Substitutes configured rule logic with it's JS equivalents and returns them as array elements |
||
| 148 | * |
||
| 149 | * @return array |
||
| 150 | * @throws LogicException If the provided condition option was not able to be handled |
||
| 151 | */ |
||
| 152 | public function buildExpression() |
||
| 153 | { |
||
| 154 | /** @var EditableFormField $formFieldWatch */ |
||
| 155 | $formFieldWatch = $this->ConditionField(); |
||
| 156 | //Encapsulated the action to the object |
||
| 157 | $action = $formFieldWatch->getJsEventHandler(); |
||
| 158 | |||
| 159 | 2 | // is this field a special option field |
|
| 160 | $checkboxField = $formFieldWatch->isCheckBoxField(); |
||
| 161 | $radioField = $formFieldWatch->isRadioField(); |
||
| 162 | 2 | $target = sprintf('$("%s")', $formFieldWatch->getSelectorFieldOnly()); |
|
| 163 | $fieldValue = Convert::raw2js($this->FieldValue); |
||
| 164 | 2 | ||
| 165 | $conditionOptions = [ |
||
| 166 | 'ValueLessThan' => '<', |
||
| 167 | 2 | 'ValueLessThanEqual' => '<=', |
|
| 168 | 2 | 'ValueGreaterThan' => '>', |
|
| 169 | 2 | 'ValueGreaterThanEqual' => '>=' |
|
| 170 | 2 | ]; |
|
| 171 | |||
| 172 | // and what should we evaluate |
||
| 173 | 2 | switch ($this->ConditionOption) { |
|
| 174 | 2 | case 'IsNotBlank': |
|
| 175 | 2 | case 'IsBlank': |
|
| 176 | $expression = ($checkboxField || $radioField) ? "!{$target}.is(\":checked\")" : "{$target}.val() == ''"; |
||
| 177 | 2 | if ((string) $this->ConditionOption === 'IsNotBlank') { |
|
| 178 | //Negate |
||
| 179 | 2 | $expression = "!({$expression})"; |
|
| 180 | 2 | } |
|
| 181 | 2 | break; |
|
| 182 | case 'HasValue': |
||
| 183 | case 'ValueNot': |
||
| 184 | if ($checkboxField) { |
||
| 185 | if ($formFieldWatch->isCheckBoxGroupField()) { |
||
| 186 | $expression = sprintf( |
||
| 187 | "$.inArray('%s', %s.filter(':checked').map(function(){ return $(this).val();}).get()) > -1", |
||
| 188 | 2 | $fieldValue, |
|
| 189 | 2 | $target |
|
| 190 | 2 | ); |
|
| 191 | } else { |
||
| 192 | $expression = "{$target}.prop('checked')"; |
||
| 193 | } |
||
| 194 | } elseif ($radioField) { |
||
| 195 | // We cannot simply get the value of the radio group, we need to find the checked option first. |
||
| 196 | $expression = sprintf( |
||
| 197 | '%s.closest(".field, .control-group").find("input:checked").val() == "%s"', |
||
| 198 | $target, |
||
| 199 | $fieldValue |
||
| 200 | 2 | ); |
|
| 201 | } else { |
||
| 202 | $expression = sprintf('%s.val() == "%s"', $target, $fieldValue); |
||
| 203 | } |
||
| 204 | |||
| 205 | if ((string) $this->ConditionOption === 'ValueNot') { |
||
| 206 | //Negate |
||
| 207 | $expression = "!({$expression})"; |
||
| 208 | 2 | } |
|
| 209 | break; |
||
| 210 | case 'ValueLessThan': |
||
| 211 | 2 | case 'ValueLessThanEqual': |
|
| 212 | case 'ValueGreaterThan': |
||
| 213 | case 'ValueGreaterThanEqual': |
||
| 214 | $expression = sprintf( |
||
| 215 | 2 | '%s.val() %s parseFloat("%s")', |
|
| 216 | 1 | $target, |
|
| 217 | 1 | $conditionOptions[$this->ConditionOption], |
|
| 218 | 1 | $fieldValue |
|
| 219 | 1 | ); |
|
| 220 | 1 | break; |
|
| 221 | 1 | default: |
|
| 222 | 1 | throw new LogicException("Unhandled rule {$this->ConditionOption}"); |
|
| 223 | 1 | break; |
|
| 224 | } |
||
| 225 | 1 | ||
| 226 | 1 | $result = [ |
|
| 227 | 'operation' => $expression, |
||
| 228 | 'event' => $action, |
||
| 229 | ]; |
||
| 230 | 2 | ||
| 231 | return $result; |
||
| 232 | } |
||
| 233 | 2 | ||
| 234 | 2 | ||
| 235 | 2 | /** |
|
| 236 | * Determines whether the rule is satisfied, based on provided form data. |
||
| 237 | 2 | * Used for php validation of required conditional fields |
|
| 238 | * |
||
| 239 | * @param array $data Submitted form data |
||
| 240 | * @return boolean |
||
| 241 | * @throws LogicException Invalid ConditionOption is set for this rule. |
||
| 242 | */ |
||
| 243 | public function validateAgainstFormData(array $data) |
||
| 288 | } |
||
| 289 | |||
| 290 | |||
| 291 | /** |
||
| 292 | * Returns the opposite visibility function for the value of the initial visibility field, e.g. show/hide. This |
||
| 293 | * will toggle the "hide" class either way, which is handled by CSS. |
||
| 294 | * |
||
| 295 | * @param string $initialState |
||
| 296 | * @param boolean $invert |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | public function toggleDisplayText($initialState, $invert = false) |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Returns an event name to be dispatched when the field is changed. Matches up with the visibility classes |
||
| 310 | * added or removed in `toggleDisplayText()`. |
||
| 311 | * |
||
| 312 | * @param string $initialState |
||
| 313 | * @param bool $invert |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | public function toggleDisplayEvent($initialState, $invert = false) |
||
| 323 | } |
||
| 324 | } |
||
| 325 |