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 | // The 2 different date types will be stored with different formats |
||
| 43 | 'Date' => 'date', |
||
| 44 | 'SS_Datetime' => 'date', |
||
| 45 | 'Datetime' => 'date', |
||
| 46 | 'DBLocale' => 'string' |
||
| 47 | ); |
||
| 48 | |||
| 49 | |||
| 50 | /** |
||
| 51 | * @var ElasticaService associated elastica search service |
||
| 52 | */ |
||
| 53 | protected $service; |
||
| 54 | |||
| 55 | |||
| 56 | /** |
||
| 57 | * Array of fields that need HTML parsed |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | protected $html_fields = array(); |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Store a mapping of relationship name to result type |
||
| 64 | */ |
||
| 65 | protected $relationship_methods = array(); |
||
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * If importing a large number of items from a fixtures file, or indeed some other source, then |
||
| 70 | * it is quicker to set a flag of value IndexingOff => false. This has the effect of ensuring |
||
| 71 | * no indexing happens, a request is normally made per fixture when loading. One can then run |
||
| 72 | * the reindexing teask to bulk index in one HTTP POST request to Elasticsearch |
||
| 73 | * |
||
| 74 | * @var boolean |
||
| 75 | */ |
||
| 76 | private static $IndexingOff = false; |
||
| 77 | |||
| 78 | |||
| 79 | /** |
||
| 80 | * @see getElasticaResult |
||
| 81 | * @var \Elastica\Result |
||
| 82 | */ |
||
| 83 | protected $elastica_result; |
||
| 84 | |||
| 85 | public function __construct(ElasticaService $service) { |
||
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * Get the elasticsearch type name |
||
| 93 | * |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | public function getElasticaType() { |
||
| 99 | |||
| 100 | |||
| 101 | /** |
||
| 102 | * If the owner is part of a search result |
||
| 103 | * the raw Elastica search result is returned |
||
| 104 | * if set via setElasticaResult |
||
| 105 | * |
||
| 106 | * @return \Elastica\Result |
||
| 107 | */ |
||
| 108 | public function getElasticaResult() { |
||
| 111 | |||
| 112 | |||
| 113 | /** |
||
| 114 | * Set the raw Elastica search result |
||
| 115 | * |
||
| 116 | * @param \Elastica\Result |
||
| 117 | */ |
||
| 118 | public function setElasticaResult(\Elastica\Result $result) { |
||
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * Gets an array of elastic field definitions. |
||
| 125 | * |
||
| 126 | * @return array |
||
| 127 | */ |
||
| 128 | public function getElasticaFields($storeMethodName = false, $recurse = true) { |
||
| 170 | |||
| 171 | |||
| 172 | |||
| 173 | /** |
||
| 174 | * Get the elasticsearch mapping for the current document/type |
||
| 175 | * |
||
| 176 | * @return \Elastica\Type\Mapping |
||
| 177 | */ |
||
| 178 | public function getElasticaMapping() { |
||
| 209 | |||
| 210 | |||
| 211 | /** |
||
| 212 | * Get an elasticsearch document |
||
| 213 | * |
||
| 214 | * @return \Elastica\Document |
||
| 215 | */ |
||
| 216 | public function getElasticaDocument() { |
||
| 252 | |||
| 253 | |||
| 254 | public function getFieldValuesAsArray($recurse = true) { |
||
| 271 | |||
| 272 | |||
| 273 | /** |
||
| 274 | * Returns whether to include the document into the search index. |
||
| 275 | * All documents are added unless they have a field "ShowInSearch" which is set to false |
||
| 276 | * |
||
| 277 | * @return boolean |
||
| 278 | */ |
||
| 279 | public function showRecordInSearch() { |
||
| 282 | |||
| 283 | |||
| 284 | /** |
||
| 285 | * Delete the record from the search index if ShowInSearch is deactivated (non-SiteTree). |
||
| 286 | */ |
||
| 287 | public function onBeforeWrite() { |
||
| 297 | |||
| 298 | |||
| 299 | /** |
||
| 300 | * Delete the record from the search index if ShowInSearch is deactivated (SiteTree). |
||
| 301 | */ |
||
| 302 | public function onBeforePublish() { |
||
| 311 | |||
| 312 | |||
| 313 | /** |
||
| 314 | * Updates the record in the search index (non-SiteTree). |
||
| 315 | */ |
||
| 316 | public function onAfterWrite() { |
||
| 319 | |||
| 320 | |||
| 321 | /** |
||
| 322 | * Updates the record in the search index (SiteTree). |
||
| 323 | */ |
||
| 324 | public function onAfterPublish() { |
||
| 327 | |||
| 328 | |||
| 329 | /** |
||
| 330 | * Updates the record in the search index. |
||
| 331 | */ |
||
| 332 | protected function doIndexDocument() { |
||
| 337 | |||
| 338 | |||
| 339 | /** |
||
| 340 | * Removes the record from the search index (non-SiteTree). |
||
| 341 | */ |
||
| 342 | public function onAfterDelete() { |
||
| 345 | |||
| 346 | |||
| 347 | /** |
||
| 348 | * Removes the record from the search index (non-SiteTree). |
||
| 349 | */ |
||
| 350 | public function onAfterUnpublish() { |
||
| 353 | |||
| 354 | |||
| 355 | /** |
||
| 356 | * Removes the record from the search index if the "ShowInSearch" attribute is set to true. |
||
| 357 | */ |
||
| 358 | protected function doDeleteDocumentIfInSearch() { |
||
| 363 | |||
| 364 | |||
| 365 | /** |
||
| 366 | * Removes the record from the search index. |
||
| 367 | */ |
||
| 368 | protected function doDeleteDocument() { |
||
| 379 | |||
| 380 | |||
| 381 | /** |
||
| 382 | * Return all of the searchable fields defined in $this->owner::$searchable_fields and all the parent classes. |
||
| 383 | * |
||
| 384 | * @param $recuse Whether or not to traverse relationships. First time round yes, subsequently no |
||
| 385 | * @return array searchable fields |
||
| 386 | */ |
||
| 387 | public function getAllSearchableFields($recurse = true) { |
||
| 441 | |||
| 442 | |||
| 443 | |||
| 444 | |||
| 445 | public function requireDefaultRecords() { |
||
| 467 | |||
| 468 | |||
| 469 | /* |
||
| 470 | Allow the option of overriding the default template with one of <ClassName>ElasticSearchResult |
||
| 471 | */ |
||
| 472 | public function RenderResult($linkToContainer = '') { |
||
| 477 | |||
| 478 | |||
| 479 | public function getTermVectors() { |
||
| 482 | |||
| 483 | |||
| 484 | public function updateCMSFields(\FieldList $fields) { |
||
| 549 | |||
| 550 | |||
| 551 | } |
||
| 552 |