| Total Complexity | 42 |
| Total Lines | 237 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ElementalAreasExtension 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 ElementalAreasExtension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class ElementalAreasExtension extends DataExtension |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * @config |
||
| 48 | * |
||
| 49 | * @var array $ignored_classes Classes to ignore adding elements too. |
||
| 50 | */ |
||
| 51 | private static $ignored_classes = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @config |
||
| 55 | * |
||
| 56 | * On saving the element area, should Elemental reset the main website |
||
| 57 | * `$Content` field. |
||
| 58 | * |
||
| 59 | * @var boolean |
||
| 60 | */ |
||
| 61 | private static $clear_contentfield = false; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Get the available element types for this page type, |
||
| 65 | * |
||
| 66 | * Uses allowed_elements, stop_element_inheritance, disallowed_elements in |
||
| 67 | * order to get to correct list. |
||
| 68 | * |
||
| 69 | * @return array |
||
| 70 | */ |
||
| 71 | public function getElementalTypes() |
||
| 72 | { |
||
| 73 | $config = $this->owner->config(); |
||
| 74 | |||
| 75 | if (is_array($config->get('allowed_elements'))) { |
||
| 76 | if ($config->get('stop_element_inheritance')) { |
||
| 77 | $availableClasses = $config->get('allowed_elements', Config::UNINHERITED); |
||
| 78 | } else { |
||
| 79 | $availableClasses = $config->get('allowed_elements'); |
||
| 80 | } |
||
| 81 | } else { |
||
| 82 | $availableClasses = ClassInfo::subclassesFor(BaseElement::class); |
||
| 83 | } |
||
| 84 | |||
| 85 | if ($config->get('stop_element_inheritance')) { |
||
| 86 | $disallowedElements = (array) $config->get('disallowed_elements', Config::UNINHERITED); |
||
| 87 | } else { |
||
| 88 | $disallowedElements = (array) $config->get('disallowed_elements'); |
||
| 89 | } |
||
| 90 | $list = array(); |
||
| 91 | |||
| 92 | foreach ($availableClasses as $availableClass) { |
||
| 93 | /** @var BaseElement $inst */ |
||
| 94 | $inst = singleton($availableClass); |
||
| 95 | |||
| 96 | if (!in_array($availableClass, $disallowedElements) && $inst->canCreate()) { |
||
| 97 | if ($inst->hasMethod('canCreateElement') && !$inst->canCreateElement()) { |
||
|
|
|||
| 98 | continue; |
||
| 99 | } |
||
| 100 | |||
| 101 | $list[$availableClass] = $inst->getType(); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | if ($config->get('sort_types_alphabetically') !== false) { |
||
| 106 | asort($list); |
||
| 107 | } |
||
| 108 | |||
| 109 | if (isset($list[BaseElement::class])) { |
||
| 110 | unset($list[BaseElement::class]); |
||
| 111 | } |
||
| 112 | |||
| 113 | $this->owner->invokeWithExtensions('updateAvailableTypesForClass', $class, $list); |
||
| 114 | |||
| 115 | return $list; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Returns an array of the relation names to ElementAreas. Ignores any |
||
| 120 | * has_one fields named `Parent` as that would indicate that this is child |
||
| 121 | * of an existing area |
||
| 122 | * |
||
| 123 | * @return array |
||
| 124 | */ |
||
| 125 | public function getElementalRelations() |
||
| 126 | { |
||
| 127 | $hasOnes = $this->owner->hasOne(); |
||
| 128 | |||
| 129 | if (!$hasOnes) { |
||
| 130 | return false; |
||
| 131 | } |
||
| 132 | |||
| 133 | $elementalAreaRelations = []; |
||
| 134 | |||
| 135 | foreach ($hasOnes as $hasOneName => $hasOneClass) { |
||
| 136 | if ($hasOneName === 'Parent' || $hasOneName === 'ParentID') { |
||
| 137 | continue; |
||
| 138 | } |
||
| 139 | |||
| 140 | if ($hasOneClass == ElementalArea::class || is_subclass_of($hasOneClass, ElementalArea::class)) { |
||
| 141 | $elementalAreaRelations[] = $hasOneName; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | return $elementalAreaRelations; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Setup the CMS Fields |
||
| 150 | * |
||
| 151 | * @param FieldList |
||
| 152 | */ |
||
| 153 | public function updateCMSFields(FieldList $fields) |
||
| 154 | { |
||
| 155 | if (!$this->supportsElemental()) { |
||
| 156 | return; |
||
| 157 | } |
||
| 158 | |||
| 159 | // add an empty holder for content as some module explicitly use insert |
||
| 160 | // after content. |
||
| 161 | $fields->replaceField('Content', new LiteralField('Content', '')); |
||
| 162 | $elementalAreaRelations = $this->owner->getElementalRelations(); |
||
| 163 | |||
| 164 | foreach ($elementalAreaRelations as $eaRelationship) { |
||
| 165 | $key = $eaRelationship . 'ID'; |
||
| 166 | |||
| 167 | // remove the scaffold dropdown |
||
| 168 | $fields->removeByName($key); |
||
| 169 | |||
| 170 | // remove the field, but don't add anything. |
||
| 171 | if (!$this->owner->isInDb()) { |
||
| 172 | continue; |
||
| 173 | } |
||
| 174 | |||
| 175 | // Example: $eaRelationship = 'ElementalArea'; |
||
| 176 | $area = $this->owner->$eaRelationship(); |
||
| 177 | |||
| 178 | $editor = ElementalAreaField::create($eaRelationship, $area, $this->getElementalTypes()); |
||
| 179 | |||
| 180 | if ($this->owner instanceof SiteTree && $fields->findOrMakeTab('Root.Main')->fieldByName('Metadata')) { |
||
| 181 | $fields->addFieldToTab('Root.Main', $editor, 'Metadata'); |
||
| 182 | } else { |
||
| 183 | $fields->addFieldToTab('Root.Main', $editor); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | return $fields; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Make sure there is always an ElementalArea for adding Elements |
||
| 192 | */ |
||
| 193 | public function onBeforeWrite() |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @return boolean |
||
| 212 | */ |
||
| 213 | public function supportsElemental() |
||
| 214 | { |
||
| 215 | if ($this->owner->hasMethod('includeElemental')) { |
||
| 216 | $res = $this->owner->includeElemental(); |
||
| 217 | |||
| 218 | if ($res !== null) { |
||
| 219 | return $res; |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | if (is_a($this->owner, RedirectorPage::class) || is_a($this->owner, VirtualPage::class)) { |
||
| 224 | return false; |
||
| 225 | } elseif ($ignored = Config::inst()->get(ElementalPageExtension::class, 'ignored_classes')) { |
||
| 226 | foreach ($ignored as $check) { |
||
| 227 | if (is_a($this->owner, $check)) { |
||
| 228 | return false; |
||
| 229 | } |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | return true; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Set all has_one relationships to an ElementalArea to a valid ID if they're unset |
||
| 238 | * |
||
| 239 | * @param array $elementalAreaRelations indexed array of relationship names that are to ElementalAreas |
||
| 240 | * @return DataObject |
||
| 241 | */ |
||
| 242 | public function ensureElementalAreasExist($elementalAreaRelations) |
||
| 243 | { |
||
| 244 | foreach ($elementalAreaRelations as $eaRelationship) { |
||
| 245 | $areaID = $eaRelationship . 'ID'; |
||
| 246 | |||
| 247 | if (!$this->owner->$areaID) { |
||
| 248 | $area = ElementalArea::create(); |
||
| 249 | $area->OwnerClassName = get_class($this->owner); |
||
| 250 | $area->write(); |
||
| 251 | $this->owner->$areaID = $area->ID; |
||
| 252 | } |
||
| 253 | } |
||
| 254 | return $this->owner; |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Extension hook {@see DataObject::requireDefaultRecords} |
||
| 259 | * |
||
| 260 | * @return void |
||
| 261 | */ |
||
| 262 | public function requireDefaultRecords() |
||
| 281 | } |
||
| 282 | } |
||
| 283 | } |
||
| 284 |