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