Complex classes like Searchable 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Searchable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Searchable extends \DataExtension { |
||
13 | |||
14 | /** |
||
15 | * Counter used to display progress of indexing |
||
16 | * @var integer |
||
17 | */ |
||
18 | public static $index_ctr = 0; |
||
19 | |||
20 | /** |
||
21 | * Everytime progressInterval divides $index_ctr exactly display progress |
||
22 | * @var integer |
||
23 | */ |
||
24 | private static $progressInterval = 0; |
||
25 | |||
26 | public static $mappings = array( |
||
27 | 'Boolean' => 'boolean', |
||
28 | 'Decimal' => 'double', |
||
29 | 'Currency' => 'double', |
||
30 | 'Double' => 'double', |
||
31 | 'Enum' => 'string', |
||
32 | 'Float' => 'float', |
||
33 | 'HTMLText' => 'string', |
||
34 | 'HTMLVarchar' => 'string', |
||
35 | 'Int' => 'integer', |
||
36 | 'Text' => 'string', |
||
37 | 'VarChar' => 'string', |
||
38 | 'Varchar' => 'string', |
||
39 | 'Year' => 'integer', |
||
40 | 'Percentage' => 'double', |
||
41 | 'Time' => 'date', |
||
42 | |||
43 | // The 2 different date types will be stored with different formats |
||
44 | 'Date' => 'date', |
||
45 | 'SS_Datetime' => 'date', |
||
46 | 'Datetime' => 'date', |
||
47 | 'DBLocale' => 'string' |
||
48 | ); |
||
49 | |||
50 | |||
51 | /** |
||
52 | * @var ElasticaService associated elastica search service |
||
53 | */ |
||
54 | protected $service; |
||
55 | |||
56 | |||
57 | /** |
||
58 | * Array of fields that need HTML parsed |
||
59 | * @var array |
||
60 | */ |
||
61 | protected $html_fields = array(); |
||
62 | |||
63 | /** |
||
64 | * Store a mapping of relationship name to result type |
||
65 | */ |
||
66 | protected $relationship_methods = array(); |
||
67 | |||
68 | |||
69 | /** |
||
70 | * If importing a large number of items from a fixtures file, or indeed some other source, then |
||
71 | * it is quicker to set a flag of value IndexingOff => false. This has the effect of ensuring |
||
72 | * no indexing happens, a request is normally made per fixture when loading. One can then run |
||
73 | * the reindexing teask to bulk index in one HTTP POST request to Elasticsearch |
||
74 | * |
||
75 | * @var boolean |
||
76 | */ |
||
77 | private static $IndexingOff = false; |
||
78 | |||
79 | |||
80 | /** |
||
81 | * @see getElasticaResult |
||
82 | * @var \Elastica\Result |
||
83 | */ |
||
84 | protected $elastica_result; |
||
85 | |||
86 | public function __construct(ElasticaService $service) { |
||
90 | |||
91 | |||
92 | /** |
||
93 | * Get the elasticsearch type name |
||
94 | * |
||
95 | * @return string |
||
96 | */ |
||
97 | public function getElasticaType() { |
||
100 | |||
101 | |||
102 | /** |
||
103 | * If the owner is part of a search result |
||
104 | * the raw Elastica search result is returned |
||
105 | * if set via setElasticaResult |
||
106 | * |
||
107 | * @return \Elastica\Result |
||
108 | */ |
||
109 | public function getElasticaResult() { |
||
112 | |||
113 | |||
114 | /** |
||
115 | * Set the raw Elastica search result |
||
116 | * |
||
117 | * @param \Elastica\Result |
||
118 | */ |
||
119 | public function setElasticaResult(\Elastica\Result $result) { |
||
122 | |||
123 | |||
124 | /** |
||
125 | * Gets an array of elastic field definitions. |
||
126 | * |
||
127 | * @return array |
||
128 | */ |
||
129 | public function getElasticaFields($storeMethodName = false, $recurse = true) { |
||
130 | $db = $this->owner->db(); |
||
131 | $fields = $this->getAllSearchableFields(); |
||
132 | $result = array(); |
||
133 | |||
134 | foreach($fields as $name => $params) { |
||
135 | $spec = array(); |
||
136 | $name = str_replace('()', '', $name); |
||
137 | |||
138 | if(array_key_exists($name, $db)) { |
||
139 | $class = $db[$name]; |
||
140 | $this->assignSpecForStandardFieldType($name, $class, $spec); |
||
141 | } else { |
||
142 | // field name is not in the db, it could be a method |
||
143 | $has_lists = $this->getListRelationshipMethods(); |
||
144 | $has_ones = $this->owner->has_one(); |
||
145 | |||
146 | // check has_many and many_many relations |
||
147 | if(isset($has_lists[$name])) { |
||
148 | // the classes returned by the list method |
||
149 | $resultType = $has_lists[$name]; |
||
150 | $this->assignSpecForRelationship($name, $resultType, $spec, $storeMethodName, $recurse); |
||
151 | } else if(isset($has_ones[$name])) { |
||
152 | $resultType = $has_ones[$name]; |
||
153 | $this->assignSpecForRelationship($name, $resultType, $spec, $storeMethodName, $recurse); |
||
154 | } |
||
155 | // otherwise fall back to string - Enum is one such category |
||
156 | else { |
||
157 | $spec["type"] = "string"; |
||
158 | } |
||
159 | } |
||
160 | |||
161 | $this->addIndexedFields($name, $spec); |
||
162 | |||
163 | $result[$name] = $spec; |
||
164 | } |
||
165 | |||
166 | if($this->owner->hasMethod('updateElasticHTMLFields')) { |
||
167 | $this->html_fields = $this->owner->updateElasticHTMLFields($this->html_fields); |
||
168 | } |
||
169 | |||
170 | return $result; |
||
171 | } |
||
172 | |||
173 | |||
174 | |||
175 | private function addIndexedFields($name, &$spec) { |
||
176 | // in the case of a relationship type will not be set |
||
177 | if(isset($spec['type'])) { |
||
178 | if($spec['type'] == 'string') { |
||
179 | $unstemmed = array(); |
||
180 | $unstemmed['type'] = "string"; |
||
181 | $unstemmed['analyzer'] = "unstemmed"; |
||
182 | $unstemmed['term_vector'] = "yes"; |
||
183 | $extraFields = array('standard' => $unstemmed); |
||
184 | |||
185 | $shingles = array(); |
||
186 | $shingles['type'] = "string"; |
||
187 | $shingles['analyzer'] = "shingles"; |
||
188 | $shingles['term_vector'] = "yes"; |
||
189 | $extraFields['shingles'] = $shingles; |
||
190 | |||
191 | //Add autocomplete field if so required |
||
192 | $autocomplete = \Config::inst()->get($this->owner->ClassName, 'searchable_autocomplete'); |
||
193 | |||
194 | if(isset($autocomplete) && in_array($name, $autocomplete)) { |
||
195 | $autocompleteField = array(); |
||
196 | $autocompleteField['type'] = "string"; |
||
197 | $autocompleteField['index_analyzer'] = "autocomplete_index_analyzer"; |
||
198 | $autocompleteField['search_analyzer'] = "autocomplete_search_analyzer"; |
||
199 | $autocompleteField['term_vector'] = "yes"; |
||
200 | $extraFields['autocomplete'] = $autocompleteField; |
||
201 | } |
||
202 | |||
203 | $spec['fields'] = $extraFields; |
||
204 | // FIXME - make index/locale specific, get from settings |
||
205 | $spec['analyzer'] = 'stemmed'; |
||
206 | $spec['term_vector'] = "yes"; |
||
207 | } |
||
208 | } |
||
209 | } |
||
210 | |||
211 | |||
212 | /** |
||
213 | * @param string &$name |
||
214 | * @param boolean $storeMethodName |
||
215 | * @param boolean $recurse |
||
216 | */ |
||
217 | private function assignSpecForRelationship(&$name, $resultType, &$spec, $storeMethodName, $recurse) { |
||
218 | $resultTypeInstance = \Injector::inst()->create($resultType); |
||
219 | $resultTypeMapping = array(); |
||
220 | // get the fields for the result type, but do not recurse |
||
221 | if($recurse) { |
||
222 | $resultTypeMapping = $resultTypeInstance->getElasticaFields($storeMethodName, false); |
||
223 | } |
||
224 | $resultTypeMapping['ID'] = array('type' => 'integer'); |
||
225 | if($storeMethodName) { |
||
226 | $resultTypeMapping['__method'] = $name; |
||
227 | } |
||
228 | $spec = array('properties' => $resultTypeMapping); |
||
229 | // we now change the name to the result type, not the method name |
||
230 | $name = $resultType; |
||
231 | } |
||
232 | |||
233 | |||
234 | /** |
||
235 | * @param string $name |
||
236 | */ |
||
237 | private function assignSpecForStandardFieldType($name, $class, &$spec) { |
||
238 | if(($pos = strpos($class, '('))) { |
||
239 | // Valid in the case of Varchar(255) |
||
240 | $class = substr($class, 0, $pos); |
||
241 | } |
||
242 | |||
243 | if(array_key_exists($class, self::$mappings)) { |
||
244 | $spec['type'] = self::$mappings[$class]; |
||
245 | if($spec['type'] === 'date') { |
||
246 | $spec['format'] = $this->getFormatForDate($class); |
||
247 | } |
||
248 | |||
249 | if($class === 'HTMLText' || $class === 'HTMLVarchar') { |
||
250 | array_push($this->html_fields, $name); |
||
251 | } |
||
252 | } |
||
253 | } |
||
254 | |||
255 | |||
256 | private function getFormatForDate($class) { |
||
257 | $format = 'y-M-d'; // default |
||
258 | switch ($class) { |
||
259 | case 'Date': |
||
260 | $format = 'y-M-d'; |
||
261 | break; |
||
262 | case 'SS_Datetime': |
||
263 | $format = 'y-M-d H:m:s'; |
||
264 | break; |
||
265 | case 'Datetime': |
||
266 | $format = 'y-M-d H:m:s'; |
||
267 | break; |
||
268 | case 'Time': |
||
269 | $format = 'H:m:s'; |
||
270 | break; |
||
271 | } |
||
272 | |||
273 | return $format; |
||
274 | } |
||
275 | |||
276 | |||
277 | /** |
||
278 | * Get the elasticsearch mapping for the current document/type |
||
279 | * |
||
280 | * @return \Elastica\Type\Mapping |
||
281 | */ |
||
282 | public function getElasticaMapping() { |
||
313 | |||
314 | |||
315 | /** |
||
316 | * Get an elasticsearch document |
||
317 | * |
||
318 | * @return \Elastica\Document |
||
319 | */ |
||
320 | public function getElasticaDocument() { |
||
357 | |||
358 | |||
359 | public function getFieldValuesAsArray($recurse = true) { |
||
421 | |||
422 | |||
423 | /** |
||
424 | * Returns whether to include the document into the search index. |
||
425 | * All documents are added unless they have a field "ShowInSearch" which is set to false |
||
426 | * |
||
427 | * @return boolean |
||
428 | */ |
||
429 | public function showRecordInSearch() { |
||
432 | |||
433 | |||
434 | /** |
||
435 | * Delete the record from the search index if ShowInSearch is deactivated (non-SiteTree). |
||
436 | */ |
||
437 | public function onBeforeWrite() { |
||
445 | |||
446 | |||
447 | /** |
||
448 | * Delete the record from the search index if ShowInSearch is deactivated (SiteTree). |
||
449 | */ |
||
450 | public function onBeforePublish() { |
||
461 | |||
462 | |||
463 | /** |
||
464 | * Updates the record in the search index (non-SiteTree). |
||
465 | */ |
||
466 | public function onAfterWrite() { |
||
469 | |||
470 | |||
471 | /** |
||
472 | * Updates the record in the search index (SiteTree). |
||
473 | */ |
||
474 | public function onAfterPublish() { |
||
477 | |||
478 | |||
479 | /** |
||
480 | * Updates the record in the search index. |
||
481 | */ |
||
482 | protected function doIndexDocument() { |
||
489 | |||
490 | |||
491 | /** |
||
492 | * Removes the record from the search index (non-SiteTree). |
||
493 | */ |
||
494 | public function onAfterDelete() { |
||
497 | |||
498 | |||
499 | /** |
||
500 | * Removes the record from the search index (non-SiteTree). |
||
501 | */ |
||
502 | public function onAfterUnpublish() { |
||
505 | |||
506 | |||
507 | /** |
||
508 | * Removes the record from the search index if the "ShowInSearch" attribute is set to true. |
||
509 | */ |
||
510 | protected function doDeleteDocumentIfInSearch() { |
||
515 | |||
516 | |||
517 | /** |
||
518 | * Removes the record from the search index. |
||
519 | */ |
||
520 | protected function doDeleteDocument() { |
||
532 | |||
533 | |||
534 | /** |
||
535 | * Return all of the searchable fields defined in $this->owner::$searchable_fields and all the parent classes. |
||
536 | * |
||
537 | * @param $recuse Whether or not to traverse relationships. First time round yes, subsequently no |
||
538 | * @return array searchable fields |
||
539 | */ |
||
540 | public function getAllSearchableFields($recurse = true) { |
||
594 | |||
595 | |||
596 | /* |
||
597 | Evaluate each field, e.g. 'Title', 'Member.Name' |
||
598 | */ |
||
599 | private function fieldsToElasticaConfig($fields) { |
||
616 | |||
617 | |||
618 | public function requireDefaultRecords() { |
||
669 | |||
670 | |||
671 | private function getListRelationshipMethods() { |
||
683 | |||
684 | |||
685 | private function isInSiteTree($classname) { |
||
699 | |||
700 | |||
701 | /* |
||
702 | Allow the option of overriding the default template with one of <ClassName>ElasticSearchResult |
||
703 | */ |
||
704 | public function RenderResult($linkToContainer = '') { |
||
709 | |||
710 | |||
711 | |||
712 | public function getTermVectors() { |
||
715 | |||
716 | |||
717 | public function updateCMSFields(\FieldList $fields) { |
||
782 | |||
783 | |||
784 | } |
||
785 |