| Total Complexity | 45 |
| Total Lines | 375 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like LogRequestForm 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 LogRequestForm, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | final class LogRequestForm extends AbstractRequestForm implements RequestFormInterface |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var StandaloneView |
||
| 29 | */ |
||
| 30 | private $view; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var JsonCompatibilityConverter |
||
| 34 | */ |
||
| 35 | private $jsonCompatibilityConverter; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var int |
||
| 39 | */ |
||
| 40 | private $pageId; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var bool |
||
| 44 | */ |
||
| 45 | private $CSVExport = false; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var InfoModuleController |
||
| 49 | */ |
||
| 50 | private $infoModuleController; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var QueryBuilder |
||
| 54 | */ |
||
| 55 | private $queryBuilder; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var CsvWriterInterface |
||
| 59 | */ |
||
| 60 | private $csvWriter; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | private $CSVaccu = []; |
||
| 66 | |||
| 67 | public function __construct(StandaloneView $view, InfoModuleController $infoModuleController, array $extensionSettings) |
||
| 68 | { |
||
| 69 | $this->view = $view; |
||
| 70 | $this->infoModuleController = $infoModuleController; |
||
| 71 | $this->jsonCompatibilityConverter = new JsonCompatibilityConverter(); |
||
| 72 | $this->queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_crawler_queue'); |
||
| 73 | $this->csvWriter = new CrawlerCsvWriter(); |
||
| 74 | $this->extensionSettings = $extensionSettings; |
||
| 75 | } |
||
| 76 | |||
| 77 | public function render($id, string $currentValue, array $menuItems): string |
||
| 78 | { |
||
| 79 | $quiPart = GeneralUtility::_GP('qid_details') ? '&qid_details=' . (int) GeneralUtility::_GP('qid_details') : ''; |
||
| 80 | $setId = (int) GeneralUtility::_GP('setID'); |
||
| 81 | $this->pageId = $id; |
||
| 82 | |||
| 83 | return $this->getDepthDropDownHtml($id, $currentValue, $menuItems) |
||
| 84 | . $this->showLogAction($setId, $quiPart); |
||
| 85 | } |
||
| 86 | |||
| 87 | private function getDepthDropDownHtml($id, string $currentValue, array $menuItems): string |
||
| 88 | { |
||
| 89 | return BackendUtility::getFuncMenu( |
||
| 90 | $id, |
||
| 91 | 'SET[depth]', |
||
| 92 | $currentValue, |
||
| 93 | $menuItems |
||
| 94 | ); |
||
| 95 | } |
||
| 96 | |||
| 97 | /******************************* |
||
| 98 | * |
||
| 99 | * Shows log of indexed URLs |
||
| 100 | * |
||
| 101 | ******************************/ |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @throws \TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException |
||
| 105 | */ |
||
| 106 | private function showLogAction(int $setId, string $quiPath): string |
||
| 107 | { |
||
| 108 | $this->view->setTemplate('ShowLog'); |
||
| 109 | if (empty($this->pageId)) { |
||
| 110 | $this->isErrorDetected = true; |
||
| 111 | MessageUtility::addErrorMessage($this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.noPageSelected')); |
||
| 112 | } else { |
||
| 113 | $this->findCrawler()->setAccessMode('gui'); |
||
|
|
|||
| 114 | $this->findCrawler()->setID = GeneralUtility::md5int(microtime()); |
||
| 115 | |||
| 116 | $csvExport = GeneralUtility::_POST('_csv'); |
||
| 117 | $this->CSVExport = isset($csvExport); |
||
| 118 | |||
| 119 | // Read URL: |
||
| 120 | if (GeneralUtility::_GP('qid_read')) { |
||
| 121 | $this->findCrawler()->readUrl((int) GeneralUtility::_GP('qid_read'), true); |
||
| 122 | } |
||
| 123 | |||
| 124 | // Look for set ID sent - if it is, we will display contents of that set: |
||
| 125 | $showSetId = (int) GeneralUtility::_GP('setID'); |
||
| 126 | |||
| 127 | $queueId = GeneralUtility::_GP('qid_details'); |
||
| 128 | $this->view->assign('queueId', $queueId); |
||
| 129 | $this->view->assign('setId', $showSetId); |
||
| 130 | // Show details: |
||
| 131 | if ($queueId) { |
||
| 132 | // Get entry record: |
||
| 133 | $q_entry = $this->queryBuilder |
||
| 134 | ->from('tx_crawler_queue') |
||
| 135 | ->select('*') |
||
| 136 | ->where( |
||
| 137 | $this->queryBuilder->expr()->eq('qid', $this->queryBuilder->createNamedParameter($queueId)) |
||
| 138 | ) |
||
| 139 | ->execute() |
||
| 140 | ->fetch(); |
||
| 141 | |||
| 142 | // Explode values |
||
| 143 | $q_entry['parameters'] = $this->jsonCompatibilityConverter->convert($q_entry['parameters']); |
||
| 144 | $q_entry['result_data'] = $this->jsonCompatibilityConverter->convert($q_entry['result_data']); |
||
| 145 | $resStatus = ResultHandler::getResStatus($q_entry['result_data']); |
||
| 146 | if (is_array($q_entry['result_data'])) { |
||
| 147 | $q_entry['result_data']['content'] = $this->jsonCompatibilityConverter->convert($q_entry['result_data']['content']); |
||
| 148 | if (! $this->infoModuleController->MOD_SETTINGS['log_resultLog']) { |
||
| 149 | unset($q_entry['result_data']['content']['log']); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | $this->view->assign('queueStatus', $resStatus); |
||
| 154 | $this->view->assign('queueDetails', DebugUtility::viewArray($q_entry)); |
||
| 155 | } else { |
||
| 156 | // Show list |
||
| 157 | // Drawing tree: |
||
| 158 | $tree = GeneralUtility::makeInstance(PageTreeView::class); |
||
| 159 | $perms_clause = $GLOBALS['BE_USER']->getPagePermsClause(1); |
||
| 160 | $tree->init('AND ' . $perms_clause); |
||
| 161 | |||
| 162 | // Set root row: |
||
| 163 | $pageinfo = BackendUtility::readPageAccess( |
||
| 164 | $this->pageId, |
||
| 165 | $perms_clause |
||
| 166 | ); |
||
| 167 | $HTML = $this->getIconFactory()->getIconForRecord('pages', $pageinfo, Icon::SIZE_SMALL)->render(); |
||
| 168 | $tree->tree[] = [ |
||
| 169 | 'row' => $pageinfo, |
||
| 170 | 'HTML' => $HTML, |
||
| 171 | ]; |
||
| 172 | |||
| 173 | // Get branch beneath: |
||
| 174 | if ($this->infoModuleController->MOD_SETTINGS['depth']) { |
||
| 175 | $tree->getTree($this->pageId, $this->infoModuleController->MOD_SETTINGS['depth']); |
||
| 176 | } |
||
| 177 | |||
| 178 | // If Flush button is pressed, flush tables instead of selecting entries: |
||
| 179 | if (GeneralUtility::_POST('_flush')) { |
||
| 180 | $doFlush = true; |
||
| 181 | $doFullFlush = false; |
||
| 182 | } elseif (GeneralUtility::_POST('_flush_all')) { |
||
| 183 | $doFlush = true; |
||
| 184 | $doFullFlush = true; |
||
| 185 | } else { |
||
| 186 | $doFlush = false; |
||
| 187 | $doFullFlush = false; |
||
| 188 | } |
||
| 189 | $itemsPerPage = (int) $this->infoModuleController->MOD_SETTINGS['itemsPerPage']; |
||
| 190 | $queueFilter = new QueueFilter($this->infoModuleController->MOD_SETTINGS['log_display']); |
||
| 191 | // Traverse page tree: |
||
| 192 | $code = ''; |
||
| 193 | $count = 0; |
||
| 194 | foreach ($tree->tree as $data) { |
||
| 195 | // Get result: |
||
| 196 | $logEntriesOfPage = $this->crawlerController->getLogEntriesForPageId( |
||
| 197 | (int) $data['row']['uid'], |
||
| 198 | $queueFilter, |
||
| 199 | $doFlush, |
||
| 200 | $doFullFlush, |
||
| 201 | $itemsPerPage |
||
| 202 | ); |
||
| 203 | |||
| 204 | $code .= $this->drawLog_addRows( |
||
| 205 | $logEntriesOfPage, |
||
| 206 | $data['HTML'] . BackendUtility::getRecordTitle('pages', $data['row'], true) |
||
| 207 | ); |
||
| 208 | if (++$count === 1000) { |
||
| 209 | break; |
||
| 210 | } |
||
| 211 | } |
||
| 212 | $this->view->assign('code', $code); |
||
| 213 | } |
||
| 214 | |||
| 215 | if ($this->CSVExport) { |
||
| 216 | $this->outputCsvFile(); |
||
| 217 | } |
||
| 218 | } |
||
| 219 | $this->view->assign('showResultLog', (bool) $this->infoModuleController->MOD_SETTINGS['log_resultLog']); |
||
| 220 | $this->view->assign('showFeVars', (bool) $this->infoModuleController->MOD_SETTINGS['log_feVars']); |
||
| 221 | $this->view->assign('displayActions', 1); |
||
| 222 | $this->view->assign('displayLogFilterHtml', $this->getDisplayLogFilterHtml($setId)); |
||
| 223 | $this->view->assign('itemPerPageHtml', $this->getItemsPerPageDropDownHtml()); |
||
| 224 | $this->view->assign('showResultLogHtml', $this->getShowResultLogCheckBoxHtml($setId, $quiPath)); |
||
| 225 | $this->view->assign('showFeVarsHtml', $this->getShowFeVarsCheckBoxHtml($setId, $quiPath)); |
||
| 226 | return $this->view->render(); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Outputs the CSV file and sets the correct headers |
||
| 231 | */ |
||
| 232 | private function outputCsvFile(): void |
||
| 233 | { |
||
| 234 | if (! count($this->CSVaccu)) { |
||
| 235 | MessageUtility::addWarningMessage($this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:message.canNotExportEmptyQueueToCsvText')); |
||
| 236 | return; |
||
| 237 | } |
||
| 238 | |||
| 239 | $csvString = $this->csvWriter->arrayToCsv($this->CSVaccu); |
||
| 240 | |||
| 241 | header('Content-Type: application/octet-stream'); |
||
| 242 | header('Content-Disposition: attachment; filename=CrawlerLog.csv'); |
||
| 243 | echo $csvString; |
||
| 244 | |||
| 245 | exit; |
||
| 246 | } |
||
| 247 | |||
| 248 | private function getIconFactory(): IconFactory |
||
| 249 | { |
||
| 250 | return GeneralUtility::makeInstance(IconFactory::class); |
||
| 251 | } |
||
| 252 | |||
| 253 | private function getDisplayLogFilterHtml(int $setId): string |
||
| 262 | ); |
||
| 263 | } |
||
| 264 | |||
| 265 | private function getItemsPerPageDropDownHtml(): string |
||
| 266 | { |
||
| 267 | return $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.itemsPerPage') . ': ' . |
||
| 268 | BackendUtility::getFuncMenu( |
||
| 269 | $this->pageId, |
||
| 270 | 'SET[itemsPerPage]', |
||
| 271 | $this->infoModuleController->MOD_SETTINGS['itemsPerPage'], |
||
| 272 | $this->infoModuleController->MOD_MENU['itemsPerPage'] |
||
| 273 | ); |
||
| 274 | } |
||
| 275 | |||
| 276 | private function getShowResultLogCheckBoxHtml(int $setId, string $quiPart): string |
||
| 277 | { |
||
| 278 | return BackendUtility::getFuncCheck( |
||
| 279 | $this->pageId, |
||
| 280 | 'SET[log_resultLog]', |
||
| 281 | $this->infoModuleController->MOD_SETTINGS['log_resultLog'], |
||
| 282 | 'index.php', |
||
| 283 | '&setID=' . $setId . $quiPart |
||
| 284 | ) . ' ' . $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.showresultlog'); |
||
| 285 | } |
||
| 286 | |||
| 287 | private function getShowFeVarsCheckBoxHtml(int $setId, string $quiPart): string |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Create the rows for display of the page tree |
||
| 300 | * For each page a number of rows are shown displaying GET variable configuration |
||
| 301 | * |
||
| 302 | * @param array $logEntriesOfPage Log items of one page |
||
| 303 | * @param string $titleString Title string |
||
| 304 | * @return string HTML <tr> content (one or more) |
||
| 305 | * @throws \TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException |
||
| 306 | */ |
||
| 307 | private function drawLog_addRows(array $logEntriesOfPage, string $titleString): string |
||
| 400 | } |
||
| 401 | } |
||
| 402 |