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