We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 66 |
| Total Lines | 427 |
| Duplicated Lines | 0 % |
| Changes | 24 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SearchController 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 SearchController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class SearchController extends AbstractController |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var CollectionRepository |
||
| 28 | */ |
||
| 29 | protected $collectionRepository; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @param CollectionRepository $collectionRepository |
||
| 33 | */ |
||
| 34 | public function injectCollectionRepository(CollectionRepository $collectionRepository) |
||
| 35 | { |
||
| 36 | $this->collectionRepository = $collectionRepository; |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var MetadataRepository |
||
| 41 | */ |
||
| 42 | protected $metadataRepository; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param MetadataRepository $metadataRepository |
||
| 46 | */ |
||
| 47 | public function injectMetadataRepository(MetadataRepository $metadataRepository) |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Search Action |
||
| 54 | * |
||
| 55 | * @return void |
||
| 56 | */ |
||
| 57 | public function searchAction() |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Main action |
||
| 68 | * |
||
| 69 | * @return void |
||
| 70 | */ |
||
| 71 | public function mainAction() |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Adds the current document's UID or parent ID to the search form |
||
| 132 | * |
||
| 133 | * @access protected |
||
| 134 | * |
||
| 135 | * @return string HTML input fields with current document's UID |
||
| 136 | */ |
||
| 137 | protected function addCurrentDocument() |
||
| 138 | { |
||
| 139 | // Load current list object. |
||
| 140 | $list = GeneralUtility::makeInstance(DocumentList::class); |
||
| 141 | // Load current document. |
||
| 142 | if ( |
||
| 143 | !empty($this->requestData['id']) |
||
| 144 | && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->requestData['id']) |
||
| 145 | ) { |
||
| 146 | $this->loadDocument($this->requestData); |
||
| 147 | // Get document's UID |
||
| 148 | if ($this->document) { |
||
| 149 | $this->view->assign('DOCUMENT_ID', $this->document->getUid()); |
||
| 150 | } |
||
| 151 | } elseif (!empty($list->metadata['options']['params']['filterquery'])) { |
||
| 152 | // Get document's UID from search metadata. |
||
| 153 | // The string may be e.g. "{!join from=uid to=partof}uid:{!join from=uid to=partof}uid:2" OR {!join from=uid to=partof}uid:2 OR uid:2" |
||
| 154 | // or "collection_faceting:("Some Collection Title")" |
||
| 155 | foreach ($list->metadata['options']['params']['filterquery'] as $facet) { |
||
| 156 | if (($lastUidPos = strrpos($facet['query'], 'uid:')) !== false) { |
||
| 157 | $facetKeyVal = explode(':', substr($facet['query'], $lastUidPos)); |
||
| 158 | if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($facetKeyVal[1])) { |
||
| 159 | $documentId = (int) $facetKeyVal[1]; |
||
| 160 | } |
||
| 161 | } |
||
| 162 | } |
||
| 163 | if (!empty($documentId)) { |
||
| 164 | $this->view->assign('DOCUMENT_ID', $documentId); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | |||
| 170 | /** |
||
| 171 | * Adds the current collection's UID to the search form |
||
| 172 | * |
||
| 173 | * @access protected |
||
| 174 | * |
||
| 175 | * @return string HTML input fields with current document's UID and parent ID |
||
| 176 | */ |
||
| 177 | protected function addCurrentCollection() |
||
| 178 | { |
||
| 179 | // Load current collection. |
||
| 180 | $list = GeneralUtility::makeInstance(DocumentList::class); |
||
| 181 | if ( |
||
| 182 | !empty($list->metadata['options']['source']) |
||
| 183 | && $list->metadata['options']['source'] == 'collection' |
||
| 184 | ) { |
||
| 185 | $this->view->assign('COLLECTION_ID', $list->metadata['options']['select']); |
||
| 186 | // Get collection's UID. |
||
| 187 | } elseif (!empty($list->metadata['options']['params']['filterquery'])) { |
||
| 188 | // Get collection's UID from search metadata. |
||
| 189 | foreach ($list->metadata['options']['params']['filterquery'] as $facet) { |
||
| 190 | $facetKeyVal = explode(':', $facet['query'], 2); |
||
| 191 | if ( |
||
| 192 | $facetKeyVal[0] == 'collection_faceting' |
||
| 193 | && !strpos($facetKeyVal[1], '" OR "') |
||
| 194 | ) { |
||
| 195 | $collectionId = Helper::getUidFromIndexName(trim($facetKeyVal[1], '(")'), 'tx_dlf_collections'); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | $this->view->assign('COLLECTION_ID', $collectionId); |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Adds the facets menu to the search form |
||
| 204 | * |
||
| 205 | * @access protected |
||
| 206 | * |
||
| 207 | * @return string HTML output of facets menu |
||
| 208 | */ |
||
| 209 | protected function addFacetsMenu() |
||
| 210 | { |
||
| 211 | // Check for typoscript configuration to prevent fatal error. |
||
| 212 | if (empty($this->settings['facetsConf'])) { |
||
| 213 | $this->logger->warning('Incomplete plugin configuration'); |
||
| 214 | return ''; |
||
| 215 | } |
||
| 216 | // Quit without doing anything if no facets are selected. |
||
| 217 | if (empty($this->settings['facets']) && empty($this->settings['facetCollections'])) { |
||
| 218 | return ''; |
||
| 219 | } |
||
| 220 | |||
| 221 | // Get facets from plugin configuration. |
||
| 222 | $facets = []; |
||
| 223 | foreach (GeneralUtility::trimExplode(',', $this->settings['facets'], true) as $facet) { |
||
| 224 | $facets[$facet . '_faceting'] = Helper::translate($facet, 'tx_dlf_metadata', $this->settings['storagePid']); |
||
| 225 | } |
||
| 226 | |||
| 227 | $this->view->assign('facetsMenu', $this->makeFacetsMenuArray($facets)); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * This builds a menu array for HMENU |
||
| 232 | * |
||
| 233 | * @access public |
||
| 234 | * |
||
| 235 | * @param string $content: The PlugIn content |
||
| 236 | * @param array $conf: The PlugIn configuration |
||
| 237 | * |
||
| 238 | * @return array HMENU array |
||
| 239 | */ |
||
| 240 | public function makeFacetsMenuArray($facets) |
||
| 241 | { |
||
| 242 | $menuArray = []; |
||
| 243 | // Set default value for facet search. |
||
| 244 | $search = [ |
||
| 245 | 'query' => '*', |
||
| 246 | 'params' => [ |
||
| 247 | 'component' => [ |
||
| 248 | 'facetset' => [ |
||
| 249 | 'facet' => [] |
||
| 250 | ] |
||
| 251 | ] |
||
| 252 | ] |
||
| 253 | ]; |
||
| 254 | // Extract query and filter from last search. |
||
| 255 | $list = GeneralUtility::makeInstance(DocumentList::class); |
||
| 256 | if (!empty($list->metadata['options']['source'])) { |
||
| 257 | if ($list->metadata['options']['source'] == 'search') { |
||
| 258 | $search['query'] = $list->metadata['options']['select']; |
||
| 259 | } |
||
| 260 | $search['params'] = $list->metadata['options']['params']; |
||
| 261 | } |
||
| 262 | // Get applicable facets. |
||
| 263 | $solr = Solr::getInstance($this->settings['solrcore']); |
||
| 264 | if (!$solr->ready) { |
||
| 265 | $this->logger->error('Apache Solr not available'); |
||
| 266 | return []; |
||
| 267 | } |
||
| 268 | // Set needed parameters for facet search. |
||
| 269 | if (empty($search['params']['filterquery'])) { |
||
| 270 | $search['params']['filterquery'] = []; |
||
| 271 | } |
||
| 272 | |||
| 273 | foreach (array_keys($facets) as $field) { |
||
| 274 | $search['params']['component']['facetset']['facet'][] = [ |
||
| 275 | 'type' => 'field', |
||
| 276 | 'key' => $field, |
||
| 277 | 'field' => $field, |
||
| 278 | 'limit' => $this->settings['limitFacets'], |
||
| 279 | 'sort' => isset($this->settings['sortingFacets']) ? $this->settings['sortingFacets'] : 'count' |
||
| 280 | ]; |
||
| 281 | } |
||
| 282 | |||
| 283 | // Set additional query parameters. |
||
| 284 | $search['params']['start'] = 0; |
||
| 285 | $search['params']['rows'] = 0; |
||
| 286 | // Set query. |
||
| 287 | $search['params']['query'] = $search['query']; |
||
| 288 | // Perform search. |
||
| 289 | $selectQuery = $solr->service->createSelect($search['params']); |
||
| 290 | $results = $solr->service->select($selectQuery); |
||
| 291 | $facet = $results->getFacetSet(); |
||
| 292 | |||
| 293 | $facetCollectionArray = []; |
||
| 294 | |||
| 295 | // replace everything expect numbers and comma |
||
| 296 | $facetCollections = preg_replace('/[^0-9,]/', '', $this->settings['facetCollections']); |
||
| 297 | |||
| 298 | if (!empty($facetCollections)) { |
||
| 299 | $collections = $this->collectionRepository->findCollectionsBySettings(['collections' => $facetCollections]); |
||
| 300 | |||
| 301 | /** @var Collection $collection */ |
||
| 302 | foreach ($collections as $collection) { |
||
| 303 | $facetCollectionArray[] = $collection->getIndexName(); |
||
| 304 | } |
||
| 305 | } |
||
| 306 | |||
| 307 | // Process results. |
||
| 308 | if ($facet) { |
||
| 309 | foreach ($facet as $field => $values) { |
||
| 310 | $entryArray = []; |
||
| 311 | $entryArray['title'] = htmlspecialchars($facets[$field]); |
||
| 312 | $entryArray['count'] = 0; |
||
| 313 | $entryArray['_OVERRIDE_HREF'] = ''; |
||
| 314 | $entryArray['doNotLinkIt'] = 1; |
||
| 315 | $entryArray['ITEM_STATE'] = 'NO'; |
||
| 316 | // Count number of facet values. |
||
| 317 | $i = 0; |
||
| 318 | foreach ($values as $value => $count) { |
||
| 319 | if ($count > 0) { |
||
| 320 | // check if facet collection configuration exists |
||
| 321 | if (!empty($this->settings['facetCollections'])) { |
||
| 322 | if ($field == "collection_faceting" && !in_array($value, $facetCollectionArray)) { |
||
| 323 | continue; |
||
| 324 | } |
||
| 325 | } |
||
| 326 | $entryArray['count']++; |
||
| 327 | if ($entryArray['ITEM_STATE'] == 'NO') { |
||
| 328 | $entryArray['ITEM_STATE'] = 'IFSUB'; |
||
| 329 | } |
||
| 330 | $entryArray['_SUB_MENU'][] = $this->getFacetsMenuEntry($field, $value, $count, $search, $entryArray['ITEM_STATE']); |
||
| 331 | if (++$i == $this->settings['limit']) { |
||
| 332 | break; |
||
| 333 | } |
||
| 334 | } else { |
||
| 335 | break; |
||
| 336 | } |
||
| 337 | } |
||
| 338 | $menuArray[] = $entryArray; |
||
| 339 | } |
||
| 340 | } |
||
| 341 | return $menuArray; |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Creates an array for a HMENU entry of a facet value. |
||
| 346 | * |
||
| 347 | * @access protected |
||
| 348 | * |
||
| 349 | * @param string $field: The facet's index_name |
||
| 350 | * @param string $value: The facet's value |
||
| 351 | * @param int $count: Number of hits for this facet |
||
| 352 | * @param array $search: The parameters of the current search query |
||
| 353 | * @param string &$state: The state of the parent item |
||
| 354 | * |
||
| 355 | * @return array The array for the facet's menu entry |
||
| 356 | */ |
||
| 357 | protected function getFacetsMenuEntry($field, $value, $count, $search, &$state) |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Returns the extended search form and adds the JS files necessary for extended search. |
||
| 417 | * |
||
| 418 | * @access protected |
||
| 419 | * |
||
| 420 | * @return string The extended search form or an empty string |
||
| 421 | */ |
||
| 422 | protected function addExtendedSearch() |
||
| 451 | } |
||
| 452 | } |
||
| 453 |