Total Complexity | 45 |
Total Lines | 270 |
Duplicated Lines | 0 % |
Changes | 12 | ||
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 | * Whether or not to replace the default SiteTree content field |
||
76 | * |
||
77 | * @var boolean |
||
78 | * @config |
||
79 | */ |
||
80 | private static $replace_content_field = true; |
||
81 | |||
82 | /** |
||
83 | * Get the available element types for this page type, |
||
84 | * |
||
85 | * Uses allowed_elements, stop_element_inheritance, disallowed_elements in |
||
86 | * order to get to correct list. |
||
87 | * |
||
88 | * @return array |
||
89 | */ |
||
90 | public function getElementalTypes() |
||
91 | { |
||
92 | $config = $this->owner->config(); |
||
93 | |||
94 | if (is_array($config->get('allowed_elements'))) { |
||
95 | if ($config->get('stop_element_inheritance')) { |
||
96 | $availableClasses = $config->get('allowed_elements', Config::UNINHERITED); |
||
97 | } else { |
||
98 | $availableClasses = $config->get('allowed_elements'); |
||
99 | } |
||
100 | } else { |
||
101 | $availableClasses = ClassInfo::subclassesFor(BaseElement::class); |
||
102 | } |
||
103 | |||
104 | if ($config->get('stop_element_inheritance')) { |
||
105 | $disallowedElements = (array) $config->get('disallowed_elements', Config::UNINHERITED); |
||
106 | } else { |
||
107 | $disallowedElements = (array) $config->get('disallowed_elements'); |
||
108 | } |
||
109 | $list = []; |
||
110 | |||
111 | foreach ($availableClasses as $availableClass) { |
||
112 | /** @var BaseElement $inst */ |
||
113 | $inst = singleton($availableClass); |
||
114 | |||
115 | if (!in_array($availableClass, $disallowedElements) && $inst->canCreate()) { |
||
116 | if ($inst->hasMethod('canCreateElement') && !$inst->canCreateElement()) { |
||
|
|||
117 | continue; |
||
118 | } |
||
119 | |||
120 | $list[$availableClass] = $inst->getType(); |
||
121 | } |
||
122 | } |
||
123 | |||
124 | if ($config->get('sort_types_alphabetically') !== false) { |
||
125 | asort($list); |
||
126 | } |
||
127 | |||
128 | if (isset($list[BaseElement::class])) { |
||
129 | unset($list[BaseElement::class]); |
||
130 | } |
||
131 | |||
132 | $class = get_class($this->owner); |
||
133 | $this->owner->invokeWithExtensions('updateAvailableTypesForClass', $class, $list); |
||
134 | |||
135 | return $list; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Returns an array of the relation names to ElementAreas. Ignores any |
||
140 | * has_one fields named `Parent` as that would indicate that this is child |
||
141 | * of an existing area |
||
142 | * |
||
143 | * @return array |
||
144 | */ |
||
145 | public function getElementalRelations() |
||
146 | { |
||
147 | $hasOnes = $this->owner->hasOne(); |
||
148 | |||
149 | if (!$hasOnes) { |
||
150 | return false; |
||
151 | } |
||
152 | |||
153 | $elementalAreaRelations = []; |
||
154 | |||
155 | foreach ($hasOnes as $hasOneName => $hasOneClass) { |
||
156 | if ($hasOneName === 'Parent' || $hasOneName === 'ParentID') { |
||
157 | continue; |
||
158 | } |
||
159 | |||
160 | if ($hasOneClass == ElementalArea::class || is_subclass_of($hasOneClass, ElementalArea::class)) { |
||
161 | $elementalAreaRelations[] = $hasOneName; |
||
162 | } |
||
163 | } |
||
164 | |||
165 | return $elementalAreaRelations; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Setup the CMS Fields |
||
170 | * |
||
171 | * @param FieldList |
||
172 | */ |
||
173 | public function updateCMSFields(FieldList $fields) |
||
174 | { |
||
175 | if (!$this->supportsElemental()) { |
||
176 | return; |
||
177 | } |
||
178 | |||
179 | // add an empty holder for content as some module explicitly use insert |
||
180 | // after content. |
||
181 | if (Config::inst()->get(ElementalAreasExtension::class, 'replace_content_field')) { |
||
182 | $fields->replaceField('Content', new LiteralField('Content', '')); |
||
183 | } |
||
184 | $elementalAreaRelations = $this->owner->getElementalRelations(); |
||
185 | |||
186 | foreach ($elementalAreaRelations as $eaRelationship) { |
||
187 | $key = $eaRelationship . 'ID'; |
||
188 | |||
189 | // remove the scaffold dropdown |
||
190 | $fields->removeByName($key); |
||
191 | |||
192 | // remove the field, but don't add anything. |
||
193 | if (!$this->owner->isInDb()) { |
||
194 | continue; |
||
195 | } |
||
196 | |||
197 | // Example: $eaRelationship = 'ElementalArea'; |
||
198 | $area = $this->owner->$eaRelationship(); |
||
199 | |||
200 | $editor = ElementalAreaField::create($eaRelationship, $area, $this->getElementalTypes()); |
||
201 | |||
202 | if ($this->owner instanceof SiteTree && $fields->findOrMakeTab('Root.Main')->fieldByName('Metadata')) { |
||
203 | $fields->addFieldToTab('Root.Main', $editor, 'Metadata'); |
||
204 | } else { |
||
205 | $fields->addFieldToTab('Root.Main', $editor); |
||
206 | } |
||
207 | } |
||
208 | |||
209 | return $fields; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Make sure there is always an ElementalArea for adding Elements |
||
214 | */ |
||
215 | public function onBeforeWrite() |
||
229 | } |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * @return boolean |
||
234 | */ |
||
235 | public function supportsElemental() |
||
236 | { |
||
237 | if ($this->owner->hasMethod('includeElemental')) { |
||
238 | $res = $this->owner->includeElemental(); |
||
239 | |||
240 | if ($res !== null) { |
||
241 | return $res; |
||
242 | } |
||
243 | } |
||
244 | |||
245 | if (is_a($this->owner, RedirectorPage::class) || is_a($this->owner, VirtualPage::class)) { |
||
246 | return false; |
||
247 | } elseif ($ignored = Config::inst()->get(ElementalPageExtension::class, 'ignored_classes')) { |
||
248 | foreach ($ignored as $check) { |
||
249 | if (is_a($this->owner, $check)) { |
||
250 | return false; |
||
251 | } |
||
252 | } |
||
253 | } |
||
254 | |||
255 | return true; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Set all has_one relationships to an ElementalArea to a valid ID if they're unset |
||
260 | * |
||
261 | * @param array $elementalAreaRelations indexed array of relationship names that are to ElementalAreas |
||
262 | * @return DataObject |
||
263 | */ |
||
264 | public function ensureElementalAreasExist($elementalAreaRelations) |
||
265 | { |
||
266 | foreach ($elementalAreaRelations as $eaRelationship) { |
||
267 | $areaID = $eaRelationship . 'ID'; |
||
268 | |||
269 | if (!$this->owner->$areaID) { |
||
270 | $area = ElementalArea::create(); |
||
271 | $area->OwnerClassName = get_class($this->owner); |
||
272 | $area->write(); |
||
273 | $this->owner->$areaID = $area->ID; |
||
274 | } |
||
275 | } |
||
276 | return $this->owner; |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * Extension hook {@see DataObject::requireDefaultRecords} |
||
281 | * |
||
282 | * @return void |
||
283 | */ |
||
284 | public function requireDefaultRecords() |
||
316 | } |
||
317 | } |
||
318 |