| Conditions | 14 |
| Paths | 34 |
| Total Lines | 97 |
| Code Lines | 65 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 115 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 116 | { |
||
| 117 | /** @var JsonCompatibilityConverter $jsonCompatibilityConverter */ |
||
| 118 | $jsonCompatibilityConverter = GeneralUtility::makeInstance(JsonCompatibilityConverter::class); |
||
| 119 | $mode = $input->getOption('mode') ?? 'queue'; |
||
| 120 | |||
| 121 | $extensionSettings = GeneralUtility::makeInstance(ExtensionConfigurationProvider::class)->getExtensionConfiguration(); |
||
| 122 | |||
| 123 | $objectManager = GeneralUtility::makeInstance(ObjectManager::class); |
||
| 124 | |||
| 125 | /** @var CrawlerController $crawlerController */ |
||
| 126 | $crawlerController = $objectManager->get(CrawlerController::class); |
||
| 127 | /** @var QueueRepository $queueRepository */ |
||
| 128 | $queueRepository = $objectManager->get(QueueRepository::class); |
||
| 129 | |||
| 130 | if ($mode === 'exec') { |
||
| 131 | $crawlerController->registerQueueEntriesInternallyOnly = true; |
||
| 132 | } |
||
| 133 | |||
| 134 | $pageId = MathUtility::forceIntegerInRange((int) $input->getArgument('page'), 0); |
||
| 135 | if ($pageId === 0) { |
||
| 136 | $message = "Page ${pageId} is not a valid page, please check you root page id and try again."; |
||
| 137 | MessageUtility::addErrorMessage($message); |
||
| 138 | $output->writeln("<info>${message}</info>"); |
||
| 139 | return 1; |
||
| 140 | } |
||
| 141 | |||
| 142 | $configurationKeys = $this->getConfigurationKeys((string) $input->getArgument('conf')); |
||
| 143 | |||
| 144 | if ($mode === 'queue' || $mode === 'exec') { |
||
| 145 | $reason = new Reason(); |
||
| 146 | $reason->setReason(Reason::REASON_CLI_SUBMIT); |
||
| 147 | $reason->setDetailText('The cli script of the crawler added to the queue'); |
||
| 148 | |||
| 149 | $signalPayload = ['reason' => $reason]; |
||
| 150 | SignalSlotUtility::emitSignal( |
||
|
|
|||
| 151 | self::class, |
||
| 152 | SignalSlotUtility::SIGNAL_INVOKE_QUEUE_CHANGE, |
||
| 153 | $signalPayload |
||
| 154 | ); |
||
| 155 | } |
||
| 156 | |||
| 157 | if ($extensionSettings['cleanUpOldQueueEntries']) { |
||
| 158 | $queueRepository->cleanUpOldQueueEntries(); |
||
| 159 | } |
||
| 160 | |||
| 161 | $crawlerController->setID = GeneralUtility::md5int(microtime()); |
||
| 162 | $queueRows = $crawlerController->getPageTreeAndUrls( |
||
| 163 | $pageId, |
||
| 164 | MathUtility::forceIntegerInRange((int) $input->getOption('depth'), 0, 99), |
||
| 165 | $crawlerController->getCurrentTime(), |
||
| 166 | MathUtility::forceIntegerInRange((int) $input->getOption('number') ?: 30, 1, 1000), |
||
| 167 | $mode === 'queue' || $mode === 'exec', |
||
| 168 | $mode === 'url', |
||
| 169 | [], |
||
| 170 | $configurationKeys |
||
| 171 | ); |
||
| 172 | |||
| 173 | if ($mode === 'url') { |
||
| 174 | $output->writeln('<info>' . implode(PHP_EOL, $crawlerController->downloadUrls) . PHP_EOL . '</info>'); |
||
| 175 | } elseif ($mode === 'exec') { |
||
| 176 | $progressBar = new ProgressBar($output); |
||
| 177 | $output->writeln('<info>Executing ' . count($crawlerController->urlList) . ' requests right away:</info>'); |
||
| 178 | $this->outputUrls($queueRows, $output); |
||
| 179 | $output->writeln('<info>Processing</info>' . PHP_EOL); |
||
| 180 | |||
| 181 | foreach ($progressBar->iterate($crawlerController->queueEntries) as $queueRec) { |
||
| 182 | $p = $jsonCompatibilityConverter->convert($queueRec['parameters']); |
||
| 183 | |||
| 184 | $progressBar->clear(); |
||
| 185 | $output->writeln('<info>' . $p['url'] . ' (' . implode(',', $p['procInstructions']) . ') => ' . '</info>' . PHP_EOL); |
||
| 186 | $progressBar->display(); |
||
| 187 | |||
| 188 | $result = $crawlerController->readUrlFromArray($queueRec); |
||
| 189 | |||
| 190 | $resultContent = $result['content'] ?? ''; |
||
| 191 | $requestResult = $jsonCompatibilityConverter->convert($resultContent); |
||
| 192 | |||
| 193 | $progressBar->clear(); |
||
| 194 | if (is_array($requestResult)) { |
||
| 195 | $resLog = is_array($requestResult['log']) ? PHP_EOL . chr(9) . chr(9) . implode(PHP_EOL . chr(9) . chr(9), $requestResult['log']) : ''; |
||
| 196 | $output->writeln('<info>OK: ' . $resLog . '</info>' . PHP_EOL); |
||
| 197 | } else { |
||
| 198 | $output->writeln('<error>Error checking Crawler Result: ' . substr(preg_replace('/\s+/', ' ', strip_tags($resultContent)), 0, 30000) . '...' . PHP_EOL . '</error>' . PHP_EOL); |
||
| 199 | } |
||
| 200 | $progressBar->display(); |
||
| 201 | } |
||
| 202 | $output->writeln(''); |
||
| 203 | } elseif ($mode === 'queue') { |
||
| 204 | $output->writeln('<info>Putting ' . count($crawlerController->urlList) . ' entries in queue:</info>' . PHP_EOL); |
||
| 205 | $this->outputUrls($queueRows, $output); |
||
| 206 | } else { |
||
| 207 | $output->writeln('<info>' . count($crawlerController->urlList) . ' entries found for processing. (Use "mode" to decide action):</info>' . PHP_EOL); |
||
| 208 | $this->outputUrls($queueRows, $output); |
||
| 209 | } |
||
| 210 | |||
| 211 | return 0; |
||
| 212 | } |
||
| 237 |