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