We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 60 | 
| Total Lines | 414 | 
| Duplicated Lines | 0 % | 
| Changes | 23 | ||
| 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() | ||
| 115 | } | ||
| 116 | |||
| 117 | /** | ||
| 118 | * Adds the current document's UID or parent ID to the search form | ||
| 119 | * | ||
| 120 | * @access protected | ||
| 121 | * | ||
| 122 | * @return string HTML input fields with current document's UID | ||
| 123 | */ | ||
| 124 | protected function addCurrentDocument() | ||
| 125 |     { | ||
| 126 | // Load current list object. | ||
| 127 | $list = GeneralUtility::makeInstance(DocumentList::class); | ||
| 128 | // Load current document. | ||
| 129 | if ( | ||
| 130 | !empty($this->requestData['id']) | ||
| 131 | && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->requestData['id']) | ||
| 132 |         ) { | ||
| 133 | $this->loadDocument($this->requestData); | ||
| 134 | // Get document's UID | ||
| 135 |             if ($this->document) { | ||
| 136 |                 $this->view->assign('DOCUMENT_ID', $this->document->getUid()); | ||
| 137 | } | ||
| 138 |         } elseif (!empty($list->metadata['options']['params']['filterquery'])) { | ||
| 139 | // Get document's UID from search metadata. | ||
| 140 |             // 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" | ||
| 141 |             // or "collection_faceting:("Some Collection Title")" | ||
| 142 |             foreach ($list->metadata['options']['params']['filterquery'] as $facet) { | ||
| 143 |                 if (($lastUidPos = strrpos($facet['query'], 'uid:')) !== false) { | ||
| 144 |                     $facetKeyVal = explode(':', substr($facet['query'], $lastUidPos)); | ||
| 145 |                     if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($facetKeyVal[1])) { | ||
| 146 | $documentId = (int) $facetKeyVal[1]; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | } | ||
| 150 |             if (!empty($documentId)) { | ||
| 151 |                 $this->view->assign('DOCUMENT_ID', $documentId); | ||
| 152 | } | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | |||
| 157 | /** | ||
| 158 | * Adds the current collection's UID to the search form | ||
| 159 | * | ||
| 160 | * @access protected | ||
| 161 | * | ||
| 162 | * @return string HTML input fields with current document's UID and parent ID | ||
| 163 | */ | ||
| 164 | protected function addCurrentCollection() | ||
| 165 |     { | ||
| 166 | // Load current collection. | ||
| 167 | $list = GeneralUtility::makeInstance(DocumentList::class); | ||
| 168 | if ( | ||
| 169 | !empty($list->metadata['options']['source']) | ||
| 170 | && $list->metadata['options']['source'] == 'collection' | ||
| 171 |         ) { | ||
| 172 |             $this->view->assign('COLLECTION_ID', $list->metadata['options']['select']); | ||
| 173 | // Get collection's UID. | ||
| 174 |         } elseif (!empty($list->metadata['options']['params']['filterquery'])) { | ||
| 175 | // Get collection's UID from search metadata. | ||
| 176 |             foreach ($list->metadata['options']['params']['filterquery'] as $facet) { | ||
| 177 |                 $facetKeyVal = explode(':', $facet['query'], 2); | ||
| 178 | if ( | ||
| 179 | $facetKeyVal[0] == 'collection_faceting' | ||
| 180 | && !strpos($facetKeyVal[1], '" OR "') | ||
| 181 |                 ) { | ||
| 182 |                     $collectionId = Helper::getUidFromIndexName(trim($facetKeyVal[1], '(")'), 'tx_dlf_collections'); | ||
| 183 | } | ||
| 184 | } | ||
| 185 |             $this->view->assign('COLLECTION_ID', $collectionId); | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | /** | ||
| 190 | * Adds the facets menu to the search form | ||
| 191 | * | ||
| 192 | * @access protected | ||
| 193 | * | ||
| 194 | * @return string HTML output of facets menu | ||
| 195 | */ | ||
| 196 | protected function addFacetsMenu() | ||
| 197 |     { | ||
| 198 | // Check for typoscript configuration to prevent fatal error. | ||
| 199 |         if (empty($this->settings['facetsConf'])) { | ||
| 200 |             $this->logger->warning('Incomplete plugin configuration'); | ||
| 201 | return ''; | ||
| 202 | } | ||
| 203 | // Quit without doing anything if no facets are selected. | ||
| 204 |         if (empty($this->settings['facets']) && empty($this->settings['facetCollections'])) { | ||
| 205 | return ''; | ||
| 206 | } | ||
| 207 | |||
| 208 | // Get facets from plugin configuration. | ||
| 209 | $facets = []; | ||
| 210 |         foreach (GeneralUtility::trimExplode(',', $this->settings['facets'], true) as $facet) { | ||
| 211 | $facets[$facet . '_faceting'] = Helper::translate($facet, 'tx_dlf_metadata', $this->settings['storagePid']); | ||
| 212 | } | ||
| 213 | |||
| 214 |         $this->view->assign('facetsMenu', $this->makeFacetsMenuArray($facets)); | ||
| 215 | } | ||
| 216 | |||
| 217 | /** | ||
| 218 | * This builds a menu array for HMENU | ||
| 219 | * | ||
| 220 | * @access public | ||
| 221 | * | ||
| 222 | * @param string $content: The PlugIn content | ||
| 223 | * @param array $conf: The PlugIn configuration | ||
| 224 | * | ||
| 225 | * @return array HMENU array | ||
| 226 | */ | ||
| 227 | public function makeFacetsMenuArray($facets) | ||
| 228 |     { | ||
| 229 | $menuArray = []; | ||
| 230 | // Set default value for facet search. | ||
| 231 | $search = [ | ||
| 232 | 'query' => '*', | ||
| 233 | 'params' => [ | ||
| 234 | 'component' => [ | ||
| 235 | 'facetset' => [ | ||
| 236 | 'facet' => [] | ||
| 237 | ] | ||
| 238 | ] | ||
| 239 | ] | ||
| 240 | ]; | ||
| 241 | // Extract query and filter from last search. | ||
| 242 | $list = GeneralUtility::makeInstance(DocumentList::class); | ||
| 243 |         if (!empty($list->metadata['options']['source'])) { | ||
| 244 |             if ($list->metadata['options']['source'] == 'search') { | ||
| 245 | $search['query'] = $list->metadata['options']['select']; | ||
| 246 | } | ||
| 247 | $search['params'] = $list->metadata['options']['params']; | ||
| 248 | } | ||
| 249 | // Get applicable facets. | ||
| 250 | $solr = Solr::getInstance($this->settings['solrcore']); | ||
| 251 |         if (!$solr->ready) { | ||
| 252 |             $this->logger->error('Apache Solr not available'); | ||
| 253 | return []; | ||
| 254 | } | ||
| 255 | // Set needed parameters for facet search. | ||
| 256 |         if (empty($search['params']['filterquery'])) { | ||
| 257 | $search['params']['filterquery'] = []; | ||
| 258 | } | ||
| 259 | |||
| 260 |         foreach (array_keys($facets) as $field) { | ||
| 261 | $search['params']['component']['facetset']['facet'][] = [ | ||
| 262 | 'type' => 'field', | ||
| 263 | 'key' => $field, | ||
| 264 | 'field' => $field, | ||
| 265 | 'limit' => $this->settings['limitFacets'], | ||
| 266 | 'sort' => isset($this->settings['sortingFacets']) ? $this->settings['sortingFacets'] : 'count' | ||
| 267 | ]; | ||
| 268 | } | ||
| 269 | |||
| 270 | // Set additional query parameters. | ||
| 271 | $search['params']['start'] = 0; | ||
| 272 | $search['params']['rows'] = 0; | ||
| 273 | // Set query. | ||
| 274 | $search['params']['query'] = $search['query']; | ||
| 275 | // Perform search. | ||
| 276 | $selectQuery = $solr->service->createSelect($search['params']); | ||
| 277 | $results = $solr->service->select($selectQuery); | ||
| 278 | $facet = $results->getFacetSet(); | ||
| 279 | |||
| 280 | $facetCollectionArray = []; | ||
| 281 | |||
| 282 | // replace everything expect numbers and comma | ||
| 283 |         $facetCollections = preg_replace('/[^0-9,]/', '', $this->settings['facetCollections']); | ||
| 284 | |||
| 285 |         if (!empty($facetCollections)) { | ||
| 286 | $collections = $this->collectionRepository->findCollectionsBySettings(['collections' => $facetCollections]); | ||
| 287 | |||
| 288 | /** @var Collection $collection */ | ||
| 289 |             foreach ($collections as $collection) { | ||
| 290 | $facetCollectionArray[] = $collection->getIndexName(); | ||
| 291 | } | ||
| 292 | } | ||
| 293 | |||
| 294 | // Process results. | ||
| 295 |         if ($facet) { | ||
| 296 |             foreach ($facet as $field => $values) { | ||
| 297 | $entryArray = []; | ||
| 298 | $entryArray['title'] = htmlspecialchars($facets[$field]); | ||
| 299 | $entryArray['count'] = 0; | ||
| 300 | $entryArray['_OVERRIDE_HREF'] = ''; | ||
| 301 | $entryArray['doNotLinkIt'] = 1; | ||
| 302 | $entryArray['ITEM_STATE'] = 'NO'; | ||
| 303 | // Count number of facet values. | ||
| 304 | $i = 0; | ||
| 305 |                 foreach ($values as $value => $count) { | ||
| 306 |                     if ($count > 0) { | ||
| 307 | // check if facet collection configuration exists | ||
| 308 |                         if (!empty($this->settings['facetCollections'])) { | ||
| 309 |                             if ($field == "collection_faceting" && !in_array($value, $facetCollectionArray)) { | ||
| 310 | continue; | ||
| 311 | } | ||
| 312 | } | ||
| 313 | $entryArray['count']++; | ||
| 314 |                         if ($entryArray['ITEM_STATE'] == 'NO') { | ||
| 315 | $entryArray['ITEM_STATE'] = 'IFSUB'; | ||
| 316 | } | ||
| 317 | $entryArray['_SUB_MENU'][] = $this->getFacetsMenuEntry($field, $value, $count, $search, $entryArray['ITEM_STATE']); | ||
| 318 |                         if (++$i == $this->settings['limit']) { | ||
| 319 | break; | ||
| 320 | } | ||
| 321 |                     } else { | ||
| 322 | break; | ||
| 323 | } | ||
| 324 | } | ||
| 325 | $menuArray[] = $entryArray; | ||
| 326 | } | ||
| 327 | } | ||
| 328 | return $menuArray; | ||
| 329 | } | ||
| 330 | |||
| 331 | /** | ||
| 332 | * Creates an array for a HMENU entry of a facet value. | ||
| 333 | * | ||
| 334 | * @access protected | ||
| 335 | * | ||
| 336 | * @param string $field: The facet's index_name | ||
| 337 | * @param string $value: The facet's value | ||
| 338 | * @param int $count: Number of hits for this facet | ||
| 339 | * @param array $search: The parameters of the current search query | ||
| 340 | * @param string &$state: The state of the parent item | ||
| 341 | * | ||
| 342 | * @return array The array for the facet's menu entry | ||
| 343 | */ | ||
| 344 | protected function getFacetsMenuEntry($field, $value, $count, $search, &$state) | ||
| 400 | } | ||
| 401 | |||
| 402 | /** | ||
| 403 | * Returns the extended search form and adds the JS files necessary for extended search. | ||
| 404 | * | ||
| 405 | * @access protected | ||
| 406 | * | ||
| 407 | * @return string The extended search form or an empty string | ||
| 408 | */ | ||
| 409 | protected function addExtendedSearch() | ||
| 438 | } | ||
| 439 | } | ||
| 440 |