Total Complexity | 44 |
Total Lines | 321 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Complex classes like CatalogueExtension 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 CatalogueExtension, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class CatalogueExtension extends DataExtension |
||
27 | { |
||
28 | private static $db = [ |
||
|
|||
29 | "URLSegment" => "Varchar", |
||
30 | "MetaDescription" => "Text", |
||
31 | "ExtraMeta" => "HTMLFragment(['whitelist' => ['meta', 'link']])" |
||
32 | ]; |
||
33 | |||
34 | private static $casting = [ |
||
35 | 'MetaTags' => 'HTMLFragment' |
||
36 | ]; |
||
37 | |||
38 | public function updateRelativeLink(&$link, $action) |
||
39 | { |
||
40 | $parent = $this->owner->Parent(); |
||
41 | |||
42 | if ($parent && $parent->exists()) { |
||
43 | $link = Controller::join_links( |
||
44 | $parent->RelativeLink(), |
||
45 | $this->owner->URLSegment, |
||
46 | $action |
||
47 | ); |
||
48 | } else { |
||
49 | $link = Controller::join_links( |
||
50 | $this->owner->URLSegment, |
||
51 | $action |
||
52 | ); |
||
53 | } |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Returns true if this is the currently active page being used to handle this request. |
||
58 | * |
||
59 | * @return bool |
||
60 | */ |
||
61 | public function isCurrent() |
||
62 | { |
||
63 | $currentPage = Director::get_current_page(); |
||
64 | |||
65 | if ($currentPage instanceof ContentController) { |
||
66 | $currentPage = $currentPage->data(); |
||
67 | } |
||
68 | if ($currentPage instanceof CatalogueCategory || $currentPage instanceof CatalogueProduct) { |
||
69 | return $currentPage === $this->owner || $currentPage->ID === $this->owner->ID; |
||
70 | } |
||
71 | return false; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Check if this page is in the currently active section (e.g. it is either current or one of its children is |
||
76 | * currently being viewed). |
||
77 | * |
||
78 | * @return bool |
||
79 | */ |
||
80 | public function isSection() |
||
81 | { |
||
82 | $is_curr = $this->isCurrent(); |
||
83 | $curr = Director::get_current_page(); |
||
84 | |||
85 | return $is_curr || ( |
||
86 | ($curr instanceof CatalogueCategory || $curr instanceof CatalogueProduct) && in_array($this->owner->ID, $curr->getAncestors()->column()) |
||
87 | ); |
||
88 | } |
||
89 | |||
90 | |||
91 | /** |
||
92 | * Return "link", "current" or section depending on if this page is the current page, or not on the current page but |
||
93 | * in the current section. |
||
94 | * |
||
95 | * @return string |
||
96 | */ |
||
97 | public function LinkingMode() |
||
98 | { |
||
99 | if ($this->isCurrent()) { |
||
100 | return 'current'; |
||
101 | } elseif ($this->isSection()) { |
||
102 | return 'section'; |
||
103 | } else { |
||
104 | return 'link'; |
||
105 | } |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Return "link" or "section" depending on if this is the current section. |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | public function LinkOrSection() |
||
114 | { |
||
115 | return $this->isSection() ? 'section' : 'link'; |
||
116 | } |
||
117 | |||
118 | public function updateCMSFields(FieldList $fields) |
||
119 | { |
||
120 | // Add CMS requirements for URL Segment Field |
||
121 | Requirements::javascript('silverstripe/cms: client/dist/js/bundle.js'); |
||
122 | Requirements::css('silverstripe/cms: client/dist/styles/bundle.css'); |
||
123 | Requirements::add_i18n_javascript('silverstripe/cms: client/lang', false, true); |
||
124 | |||
125 | $fields->removeByName("MetaDescription"); |
||
126 | $fields->removeByName("ExtraMeta"); |
||
127 | |||
128 | $parent = null; |
||
129 | $parent_link = null; |
||
130 | |||
131 | if ($this->owner instanceof CatalogueCategory) { |
||
132 | $parent = $this |
||
133 | ->owner |
||
134 | ->Parent(); |
||
135 | } elseif ($this->owner instanceof CatalogueProduct) { |
||
136 | $parent = $this |
||
137 | ->owner |
||
138 | ->Categories() |
||
139 | ->first(); |
||
140 | } |
||
141 | |||
142 | if ($parent) { |
||
143 | $parent_link = $parent->RelativeLink(); |
||
144 | } |
||
145 | |||
146 | $baseLink = Controller::join_links( |
||
147 | Director::absoluteBaseURL(), |
||
148 | $parent_link |
||
149 | ); |
||
150 | |||
151 | if (substr(rtrim($baseLink), -1) != "/") { |
||
152 | $baseLink = $baseLink . "/"; |
||
153 | } |
||
154 | |||
155 | $fields->addFieldToTab( |
||
156 | "Root.Main", |
||
157 | SiteTreeURLSegmentField::create( |
||
158 | "URLSegment", |
||
159 | $this->owner->fieldLabel('URLSegment') |
||
160 | )->setURLPrefix($baseLink), |
||
161 | 'Content' |
||
162 | ); |
||
163 | |||
164 | // Add meta info fields |
||
165 | $fields->addFieldToTab( |
||
166 | "Root.Main", |
||
167 | ToggleCompositeField::create( |
||
168 | 'Metadata', |
||
169 | _t(__CLASS__.'.MetadataToggle', 'Metadata'), |
||
170 | [ |
||
171 | $metaFieldDesc = TextareaField::create( |
||
172 | "MetaDescription", |
||
173 | $this->owner->fieldLabel('MetaDescription') |
||
174 | ), |
||
175 | $metaFieldExtra = TextareaField::create( |
||
176 | "ExtraMeta", |
||
177 | $this->owner->fieldLabel('ExtraMeta') |
||
178 | ) |
||
179 | ] |
||
180 | )->setHeadingLevel(4) |
||
181 | ); |
||
182 | |||
183 | // Help text for MetaData on page content editor |
||
184 | $metaFieldDesc |
||
185 | ->setRightTitle( |
||
186 | _t( |
||
187 | 'SilverStripe\\CMS\\Model\\SiteTree.METADESCHELP', |
||
188 | "Search engines use this content for displaying search results (although it will not influence their ranking)." |
||
189 | ) |
||
190 | )->addExtraClass('help'); |
||
191 | |||
192 | $metaFieldExtra |
||
193 | ->setRightTitle( |
||
194 | _t( |
||
195 | 'SilverStripe\\CMS\\Model\\SiteTree.METAEXTRAHELP', |
||
196 | "HTML tags for additional meta information. For example <meta name=\"customName\" content=\"your custom content here\" />" |
||
197 | ) |
||
198 | ) |
||
199 | ->addExtraClass('help'); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Find the controller name by our convention of {$ModelClass}Controller |
||
204 | * |
||
205 | * @return string |
||
206 | */ |
||
207 | public function getControllerName() |
||
208 | { |
||
209 | //default controller for SiteTree objects |
||
210 | $controller = CatalogueController::class; |
||
211 | |||
212 | //go through the ancestry for this class looking for |
||
213 | $ancestry = ClassInfo::ancestry($this->owner->ClassName); |
||
214 | |||
215 | // loop over the array going from the deepest descendant (ie: the current class) to SiteTree |
||
216 | while ($class = array_pop($ancestry)) { |
||
217 | //we don't need to go any deeper than the SiteTree class |
||
218 | if ($class == CatalogueProduct::class || $class == CatalogueCategory::class) { |
||
219 | break; |
||
220 | } |
||
221 | |||
222 | // If we have a class of "{$ClassName}Controller" then we found our controller |
||
223 | if (class_exists($candidate = sprintf('%sController', $class))) { |
||
224 | $controller = $candidate; |
||
225 | break; |
||
226 | } elseif (class_exists($candidate = sprintf('%s_Controller', $class))) { |
||
227 | // Support the legacy underscored filename, but raise a deprecation notice |
||
228 | Deprecation::notice( |
||
229 | '5.0', |
||
230 | 'Underscored controller class names are deprecated. Use "MyController" instead of "My_Controller".', |
||
231 | Deprecation::SCOPE_GLOBAL |
||
232 | ); |
||
233 | $controller = $candidate; |
||
234 | break; |
||
235 | } |
||
236 | } |
||
237 | |||
238 | return $controller; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Return the title, description, keywords and language metatags. |
||
243 | * NOTE: Shamelessley taken from SiteTree |
||
244 | * |
||
245 | * @param bool $includeTitle Show default <title>-tag, set to false for custom templating |
||
246 | * |
||
247 | * @return string The XHTML metatags |
||
248 | */ |
||
249 | public function MetaTags($includeTitle = true) |
||
313 | } |
||
314 | |||
315 | public function onBeforeWrite() |
||
316 | { |
||
317 | // Only call on first creation, ir if title is changed |
||
318 | if ($this->owner->isChanged('Title') || !$this->owner->URLSegment) { |
||
334 | } |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * Hides disabled products from googlesitemaps |
||
339 | * Only called if googlesitemaps module is installed |
||
340 | * |
||
341 | * @param [type] $can |
||
342 | * @return bool |
||
343 | */ |
||
344 | public function alterCanIncludeInGoogleSitemap(&$can) |
||
349 |