| Total Complexity | 101 | 
| Total Lines | 512 | 
| Duplicated Lines | 0 % | 
| Changes | 5 | ||
| Bugs | 0 | Features | 0 | 
Complex classes like SolrSearch 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 SolrSearch, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 23 | class SolrSearch implements \Countable, \Iterator, \ArrayAccess, QueryResultInterface | ||
| 24 | { | ||
| 25 | protected $result; | ||
| 26 | protected $position = 0; | ||
| 27 | |||
| 28 | /** | ||
| 29 | * | ||
| 30 | * @param DocumentRepository $documentRepository | ||
| 31 | * @param QueryResult|Collection $collection | ||
| 32 | * @param array $settings | ||
| 33 | * @param array $searchParams | ||
| 34 | * @param QueryResult $listedMetadata | ||
| 35 | */ | ||
| 36 | public function __construct($documentRepository, $collection, $settings, $searchParams, $listedMetadata = null) | ||
| 37 |     { | ||
| 38 | $this->documentRepository = $documentRepository; | ||
|  | |||
| 39 | $this->collection = $collection; | ||
| 40 | $this->settings = $settings; | ||
| 41 | $this->searchParams = $searchParams; | ||
| 42 | $this->listedMetadata = $listedMetadata; | ||
| 43 | } | ||
| 44 | |||
| 45 | public function getNumLoadedDocuments() | ||
| 46 |     { | ||
| 47 | return count($this->result['documents']); | ||
| 48 | } | ||
| 49 | |||
| 50 | public function count() | ||
| 51 |     { | ||
| 52 |         if ($this->result === null) { | ||
| 53 | return 0; | ||
| 54 | } | ||
| 55 | |||
| 56 | return $this->result['numberOfToplevels']; | ||
| 57 | } | ||
| 58 | |||
| 59 | public function current() | ||
| 60 |     { | ||
| 61 | return $this[$this->position]; | ||
| 62 | } | ||
| 63 | |||
| 64 | public function key() | ||
| 65 |     { | ||
| 66 | return $this->position; | ||
| 67 | } | ||
| 68 | |||
| 69 | public function next() | ||
| 70 |     { | ||
| 71 | $this->position++; | ||
| 72 | } | ||
| 73 | |||
| 74 | public function rewind() | ||
| 75 |     { | ||
| 76 | $this->position = 0; | ||
| 77 | } | ||
| 78 | |||
| 79 | public function valid() | ||
| 80 |     { | ||
| 81 | return isset($this[$this->position]); | ||
| 82 | } | ||
| 83 | |||
| 84 | public function offsetExists($offset) | ||
| 85 |     { | ||
| 86 | $idx = $this->result['document_keys'][$offset]; | ||
| 87 | return isset($this->result['documents'][$idx]); | ||
| 88 | } | ||
| 89 | |||
| 90 | public function offsetGet($offset) | ||
| 91 |     { | ||
| 92 | $idx = $this->result['document_keys'][$offset]; | ||
| 93 | $document = $this->result['documents'][$idx] ?? null; | ||
| 94 | |||
| 95 |         if ($document !== null) { | ||
| 96 | // It may happen that a Solr group only includes non-toplevel results, | ||
| 97 | // in which case metadata of toplevel entry isn't yet filled. | ||
| 98 |             if (empty($document['metadata'])) { | ||
| 99 | $document['metadata'] = $this->fetchToplevelMetadataFromSolr([ | ||
| 100 | 'query' => 'uid:' . $document['uid'], | ||
| 101 | 'start' => 0, | ||
| 102 | 'rows' => 1, | ||
| 103 | 'sort' => ['score' => 'desc'], | ||
| 104 | ])[$document['uid']] ?? []; | ||
| 105 | } | ||
| 106 | |||
| 107 | // get title of parent/grandparent/... if empty | ||
| 108 |             if (empty($document['title']) && $document['partOf'] > 0) { | ||
| 109 | $superiorTitle = Doc::getTitle($document['partOf'], true); | ||
| 110 |                 if (!empty($superiorTitle)) { | ||
| 111 | $document['title'] = '[' . $superiorTitle . ']'; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | return $document; | ||
| 117 | } | ||
| 118 | |||
| 119 | public function offsetSet($offset, $value) | ||
| 120 |     { | ||
| 121 |         throw new \Exception("SolrSearch: Modifying result list is not supported"); | ||
| 122 | } | ||
| 123 | |||
| 124 | public function offsetUnset($offset) | ||
| 125 |     { | ||
| 126 |         throw new \Exception("SolrSearch: Modifying result list is not supported"); | ||
| 127 | } | ||
| 128 | |||
| 129 | public function getSolrResults() | ||
| 130 |     { | ||
| 131 | return $this->result['solrResults']; | ||
| 132 | } | ||
| 133 | |||
| 134 | public function getByUid($uid) | ||
| 135 |     { | ||
| 136 | return $this->result['documents'][$uid]; | ||
| 137 | } | ||
| 138 | |||
| 139 | public function getQuery() | ||
| 140 |     { | ||
| 141 | return new SolrSearchQuery($this); | ||
| 142 | } | ||
| 143 | |||
| 144 | public function getFirst() | ||
| 145 |     { | ||
| 146 | return $this[0]; | ||
| 147 | } | ||
| 148 | |||
| 149 | public function toArray() | ||
| 150 |     { | ||
| 151 | return array_values($this->result['documents']); | ||
| 152 | } | ||
| 153 | |||
| 154 | /** | ||
| 155 | * Get total number of hits. | ||
| 156 | * | ||
| 157 | * This can be accessed in Fluid template using `.numFound`. | ||
| 158 | */ | ||
| 159 | public function getNumFound() | ||
| 162 | } | ||
| 163 | |||
| 164 | public function prepare() | ||
| 165 |     { | ||
| 166 | // Prepare query parameters. | ||
| 167 | $params = []; | ||
| 168 | $matches = []; | ||
| 169 | $fields = Solr::getFields(); | ||
| 170 | |||
| 171 | // Set search query. | ||
| 172 | if ( | ||
| 173 | (!empty($this->searchParams['fulltext'])) | ||
| 174 |             || preg_match('/' . $fields['fulltext'] . ':\((.*)\)/', trim($this->searchParams['query']), $matches) | ||
| 175 |         ) { | ||
| 176 | // If the query already is a fulltext query e.g using the facets | ||
| 177 | $this->searchParams['query'] = empty($matches[1]) ? $this->searchParams['query'] : $matches[1]; | ||
| 178 | // Search in fulltext field if applicable. Query must not be empty! | ||
| 179 |             if (!empty($this->searchParams['query'])) { | ||
| 180 |                 $query = $fields['fulltext'] . ':(' . Solr::escapeQuery(trim($this->searchParams['query'])) . ')'; | ||
| 181 | } | ||
| 182 | $params['fulltext'] = true; | ||
| 183 |         } else { | ||
| 184 | // Retain given search field if valid. | ||
| 185 |             if (!empty($this->searchParams['query'])) { | ||
| 186 | $query = Solr::escapeQueryKeepField(trim($this->searchParams['query']), $this->settings['storagePid']); | ||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | // Add extended search query. | ||
| 191 | if ( | ||
| 192 | !empty($this->searchParams['extQuery']) | ||
| 193 | && is_array($this->searchParams['extQuery']) | ||
| 194 |         ) { | ||
| 195 | $allowedOperators = ['AND', 'OR', 'NOT']; | ||
| 196 | $numberOfExtQueries = count($this->searchParams['extQuery']); | ||
| 197 |             for ($i = 0; $i < $numberOfExtQueries; $i++) { | ||
| 198 |                 if (!empty($this->searchParams['extQuery'][$i])) { | ||
| 199 | if ( | ||
| 200 | in_array($this->searchParams['extOperator'][$i], $allowedOperators) | ||
| 201 |                     ) { | ||
| 202 |                         if (!empty($query)) { | ||
| 203 | $query .= ' ' . $this->searchParams['extOperator'][$i] . ' '; | ||
| 204 | } | ||
| 205 |                         $query .= Indexer::getIndexFieldName($this->searchParams['extField'][$i], $this->settings['storagePid']) . ':(' . Solr::escapeQuery($this->searchParams['extQuery'][$i]) . ')'; | ||
| 206 | } | ||
| 207 | } | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | // Add filter query for faceting. | ||
| 212 |         if (isset($this->searchParams['fq']) && is_array($this->searchParams['fq'])) { | ||
| 213 |             foreach ($this->searchParams['fq'] as $filterQuery) { | ||
| 214 | $params['filterquery'][]['query'] = $filterQuery; | ||
| 215 | } | ||
| 216 | } | ||
| 217 | |||
| 218 | // Add filter query for in-document searching. | ||
| 219 | if ( | ||
| 220 | !empty($this->searchParams['documentId']) | ||
| 221 | && MathUtility::canBeInterpretedAsInteger($this->searchParams['documentId']) | ||
| 222 |         ) { | ||
| 223 | // Search in document and all subordinates (valid for up to three levels of hierarchy). | ||
| 224 |             $params['filterquery'][]['query'] = '_query_:"{!join from=' | ||
| 225 | . $fields['uid'] . ' to=' . $fields['partof'] . '}' | ||
| 226 |                 . $fields['uid'] . ':{!join from=' . $fields['uid'] . ' to=' . $fields['partof'] . '}' | ||
| 227 |                 . $fields['uid'] . ':' . $this->searchParams['documentId'] . '"' . ' OR {!join from=' | ||
| 228 | . $fields['uid'] . ' to=' . $fields['partof'] . '}' | ||
| 229 | . $fields['uid'] . ':' . $this->searchParams['documentId'] . ' OR ' | ||
| 230 | . $fields['uid'] . ':' . $this->searchParams['documentId']; | ||
| 231 | } | ||
| 232 | |||
| 233 | // if a collection is given, we prepare the collection query string | ||
| 234 |         if ($this->collection) { | ||
| 235 |             if ($this->collection instanceof Collection) { | ||
| 236 | $collectionsQueryString = '"' . $this->collection->getIndexName() . '"'; | ||
| 237 |             } else { | ||
| 238 | $collectionsQueryString = ''; | ||
| 239 |                 foreach ($this->collection as $index => $collectionEntry) { | ||
| 240 | $collectionsQueryString .= ($index > 0 ? ' OR ' : '') . '"' . $collectionEntry->getIndexName() . '"'; | ||
| 241 | } | ||
| 242 | } | ||
| 243 | |||
| 244 |             if (empty($query)) { | ||
| 245 | $params['filterquery'][]['query'] = 'toplevel:true'; | ||
| 246 | $params['filterquery'][]['query'] = 'partof:0'; | ||
| 247 | } | ||
| 248 |             $params['filterquery'][]['query'] = 'collection_faceting:(' . $collectionsQueryString . ')'; | ||
| 249 | } | ||
| 250 | |||
| 251 | // Set some query parameters. | ||
| 252 | $params['query'] = !empty($query) ? $query : '*'; | ||
| 253 | |||
| 254 | // order the results as given or by title as default | ||
| 255 |         if (!empty($this->searchParams['orderBy'])) { | ||
| 256 | $querySort = [ | ||
| 257 | $this->searchParams['orderBy'] => $this->searchParams['order'], | ||
| 258 | ]; | ||
| 259 |         } else { | ||
| 260 | $querySort = [ | ||
| 261 | 'year_sorting' => 'asc', | ||
| 262 | 'title_sorting' => 'asc', | ||
| 263 | ]; | ||
| 264 | } | ||
| 265 | |||
| 266 | $params['sort'] = $querySort; | ||
| 267 | $params['listMetadataRecords'] = []; | ||
| 268 | |||
| 269 | // Restrict the fields to the required ones. | ||
| 270 | $params['fields'] = 'uid,id,page,title,thumbnail,partof,toplevel,type'; | ||
| 271 | |||
| 272 |         if ($this->listedMetadata) { | ||
| 273 |             foreach ($this->listedMetadata as $metadata) { | ||
| 274 |                 if ($metadata->getIndexStored() || $metadata->getIndexIndexed()) { | ||
| 275 | $listMetadataRecord = $metadata->getIndexName() . '_' . ($metadata->getIndexTokenized() ? 't' : 'u') . ($metadata->getIndexStored() ? 's' : 'u') . ($metadata->getIndexIndexed() ? 'i' : 'u'); | ||
| 276 | $params['fields'] .= ',' . $listMetadataRecord; | ||
| 277 | $params['listMetadataRecords'][$metadata->getIndexName()] = $listMetadataRecord; | ||
| 278 | } | ||
| 279 | } | ||
| 280 | } | ||
| 281 | |||
| 282 | $this->params = $params; | ||
| 283 | |||
| 284 | // Send off query to get total number of search results in advance | ||
| 285 | $this->submit(0, 1, false); | ||
| 286 | } | ||
| 287 | |||
| 288 | public function submit($start, $rows, $processResults = true) | ||
| 378 | } | ||
| 379 | |||
| 380 | /** | ||
| 381 | * Find all listed metadata using specified query params. | ||
| 382 | * | ||
| 383 | * @param int $queryParams | ||
| 384 | * @return array | ||
| 385 | */ | ||
| 386 | protected function fetchToplevelMetadataFromSolr($queryParams) | ||
| 387 |     { | ||
| 388 | // Prepare query parameters. | ||
| 389 | $params = $queryParams; | ||
| 390 | $metadataArray = []; | ||
| 391 | |||
| 392 | // Set some query parameters. | ||
| 393 | $params['listMetadataRecords'] = []; | ||
| 394 | |||
| 395 | // Restrict the fields to the required ones. | ||
| 396 | $params['fields'] = 'uid,toplevel'; | ||
| 397 | |||
| 398 |         if ($this->listedMetadata) { | ||
| 399 |             foreach ($this->listedMetadata as $metadata) { | ||
| 400 |                 if ($metadata->getIndexStored() || $metadata->getIndexIndexed()) { | ||
| 401 | $listMetadataRecord = $metadata->getIndexName() . '_' . ($metadata->getIndexTokenized() ? 't' : 'u') . ($metadata->getIndexStored() ? 's' : 'u') . ($metadata->getIndexIndexed() ? 'i' : 'u'); | ||
| 402 | $params['fields'] .= ',' . $listMetadataRecord; | ||
| 403 | $params['listMetadataRecords'][$metadata->getIndexName()] = $listMetadataRecord; | ||
| 404 | } | ||
| 405 | } | ||
| 406 | } | ||
| 407 | // Set filter query to just get toplevel documents. | ||
| 408 | $params['filterquery'][] = ['query' => 'toplevel:true']; | ||
| 409 | |||
| 410 | // Perform search. | ||
| 411 | $result = $this->searchSolr($params, true); | ||
| 412 | |||
| 413 |         foreach ($result['documents'] as $doc) { | ||
| 414 | $metadataArray[$doc['uid']] = $doc['metadata']; | ||
| 415 | } | ||
| 416 | |||
| 417 | return $metadataArray; | ||
| 418 | } | ||
| 419 | |||
| 420 | /** | ||
| 421 | * Processes a search request | ||
| 422 | * | ||
| 423 | * @access public | ||
| 424 | * | ||
| 425 | * @param array $parameters: Additional search parameters | ||
| 426 | * @param boolean $enableCache: Enable caching of Solr requests | ||
| 427 | * | ||
| 428 | * @return array The Apache Solr Documents that were fetched | ||
| 429 | */ | ||
| 430 | protected function searchSolr($parameters = [], $enableCache = true) | ||
| 535 | } | ||
| 536 | } | ||
| 537 |