Complex classes like ElasticSearchPage_Controller 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 ElasticSearchPage_Controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class ElasticSearchPage_Controller extends Page_Controller { |
||
| 19 | |||
| 20 | private static $allowed_actions = array('SearchForm', 'submit', 'index', 'similar'); |
||
| 21 | |||
| 22 | public function init() { |
||
| 32 | |||
| 33 | |||
| 34 | |||
| 35 | /* |
||
| 36 | Find DataObjects in Elasticsearch similar to the one selected. Note that aggregations are not |
||
| 37 | taken into account, merely the text of the selected document. |
||
| 38 | */ |
||
| 39 | public function similar() { |
||
| 40 | //FIXME double check security, ie if escaping needed |
||
| 41 | $class = $this->request->param('ID'); |
||
| 42 | $instanceID = $this->request->param('OtherID'); |
||
| 43 | |||
| 44 | $es = $this->primeElasticSearcherFromRequest(); |
||
| 45 | $data = $this->initialiseDataArray(); |
||
| 46 | $this->setMoreLikeThisParamsFromRequest($es); |
||
| 47 | $this->addSiteTreeFilterIfRequired($es); |
||
| 48 | $this->getSelectedSearchFields('SimilarSearchable'); |
||
| 49 | |||
| 50 | try { |
||
| 51 | $this->checkForSimulatedServerDown(); |
||
| 52 | if(class_exists($class)) { |
||
| 53 | $instance = \DataObject::get_by_id($class, $instanceID); |
||
| 54 | print_r($this->FieldsToSearch); |
||
| 55 | $paginated = $es->moreLikeThis($instance, $this->FieldsToSearch); |
||
| 56 | $this->Aggregations = $es->getAggregations(); |
||
| 57 | $this->successfulSearch($data, $paginated); |
||
| 58 | $data['SimilarTo'] = $instance; |
||
| 59 | $this->getSimilarTerms($data, $paginated); |
||
| 60 | |||
| 61 | } else { |
||
| 62 | // class does not exist |
||
| 63 | $data['ErrorMessage'] = "Class $class is either not found or not searchable\n"; |
||
| 64 | } |
||
| 65 | } catch (\InvalidArgumentException $e) { |
||
| 66 | $data['ErrorMessage'] = "Class $class is either not found or not searchable\n"; |
||
| 67 | } catch (Elastica\Exception\Connection\HttpException $e) { |
||
| 68 | $data['ErrorMessage'] = 'Unable to connect to search server'; |
||
| 69 | } |
||
| 70 | return $this->renderResults($data); |
||
| 71 | } |
||
| 72 | |||
| 73 | |||
| 74 | private function getSimilarTerms(&$data, &$paginated) { |
||
| 75 | $moreLikeThisTerms = $paginated->getList()->MoreLikeThisTerms; |
||
| 76 | $fieldToTerms = new ArrayList(); |
||
| 77 | foreach(array_keys($moreLikeThisTerms) as $fieldName) { |
||
| 78 | $readableFieldName = str_replace('.standard', '', $fieldName); |
||
| 79 | $fieldTerms = new ArrayList(); |
||
| 80 | foreach($moreLikeThisTerms[$fieldName] as $value) { |
||
| 81 | $do = new DataObject(); |
||
| 82 | $do->Term = $value; |
||
| 83 | $fieldTerms->push($do); |
||
| 84 | } |
||
| 85 | |||
| 86 | $do = new DataObject(); |
||
| 87 | $do->FieldName = $readableFieldName; |
||
| 88 | $do->Terms = $fieldTerms; |
||
| 89 | $fieldToTerms->push($do); |
||
| 90 | } |
||
| 91 | |||
| 92 | $data['SimilarSearchTerms'] = $fieldToTerms; |
||
| 93 | } |
||
| 94 | |||
| 95 | /* |
||
| 96 | Display the search form. If the query parameter exists, search against Elastica |
||
| 97 | and render results accordingly. |
||
| 98 | */ |
||
| 99 | public function index() { |
||
| 100 | $es = $this->primeElasticSearcherFromRequest(); |
||
| 101 | $data = $this->initialiseDataArray(); |
||
| 102 | $this->dealWithAggregation($es); |
||
| 103 | $this->addSiteTreeFilterIfRequired($es); |
||
| 104 | $this->getSelectedSearchFields(); |
||
| 105 | |||
| 106 | $paginated = null; |
||
| 107 | try { |
||
| 108 | $this->checkForSimulatedServerDown(); |
||
| 109 | |||
| 110 | // now actually perform the search using the original query |
||
| 111 | $paginated = $es->search($this->QueryText, $this->FieldsToSearch,$this->TestMode); |
||
| 112 | |||
| 113 | // Deal with a possible suggested query |
||
| 114 | $this->dealWithSuggestedQuery($es, $data, $paginated); |
||
| 115 | |||
| 116 | $this->Aggregations = $es->getAggregations(); |
||
| 117 | $this->successfulSearch($data, $paginated); |
||
| 118 | } catch (Elastica\Exception\Connection\HttpException $e) { |
||
| 119 | $data['ErrorMessage'] = 'Unable to connect to search server'; |
||
| 120 | } |
||
| 121 | |||
| 122 | return $this->renderResults($data); |
||
| 123 | } |
||
| 124 | |||
| 125 | |||
| 126 | private function dealWithSuggestedQuery(&$es, &$data, &$paginated) { |
||
| 138 | |||
| 139 | |||
| 140 | private function successfulSearch(&$data, $paginated) { |
||
| 141 | $data['SearchResults'] = $paginated; |
||
| 147 | |||
| 148 | |||
| 149 | /* |
||
| 150 | Return true if the query is not empty |
||
| 151 | */ |
||
| 152 | public function QueryIsEmpty() { |
||
| 155 | |||
| 156 | |||
| 157 | /** |
||
| 158 | * Process submission of the search form, redirecting to a URL that will render search results |
||
| 159 | * @param array $data form data |
||
| 160 | * @param Form $form form |
||
| 161 | */ |
||
| 162 | public function submit($data, $form) { |
||
| 169 | |||
| 170 | |||
| 171 | /* |
||
| 172 | Obtain an instance of the form |
||
| 173 | */ |
||
| 174 | public function SearchForm() { |
||
| 216 | |||
| 217 | |||
| 218 | /** |
||
| 219 | * @param string $paramName |
||
| 220 | */ |
||
| 221 | private function isParamSet($paramName) { |
||
| 224 | |||
| 225 | |||
| 226 | /** |
||
| 227 | * Set the start page from the request and results per page for a given searcher object |
||
| 228 | */ |
||
| 229 | private function primeElasticSearcherFromRequest() { |
||
| 249 | |||
| 250 | |||
| 251 | /** |
||
| 252 | * Set the admin configured similarity parameters |
||
| 253 | * @param \SilverStripe\Elastica\ElasticSearcher &$elasticSearcher ElasticaSearcher object |
||
| 254 | */ |
||
| 255 | private function setMoreLikeThisParamsFromRequest(&$elasticSearcher) { |
||
| 265 | |||
| 266 | |||
| 267 | private function dealWithAggregation(&$es) { |
||
| 285 | |||
| 286 | |||
| 287 | private function addSiteTreeFilterIfRequired(&$es) { |
||
| 295 | |||
| 296 | |||
| 297 | private function getSelectedSearchFields($selectionField = 'Searchable') { |
||
| 321 | |||
| 322 | |||
| 323 | private function initialiseDataArray() { |
||
| 332 | |||
| 333 | |||
| 334 | private function renderResults($data) { |
||
| 342 | |||
| 343 | |||
| 344 | private function calculateTime() { |
||
| 349 | |||
| 350 | |||
| 351 | private function checkForSimulatedServerDown() { |
||
| 357 | } |
||
| 358 |