We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Conditions | 23 |
| Paths | 144 |
| Total Lines | 123 |
| Code Lines | 78 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| 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 |
||
| 113 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 114 | { |
||
| 115 | $dryRun = $input->getOption('dry-run') != false ? true : false; |
||
| 116 | |||
| 117 | $io = new SymfonyStyle($input, $output); |
||
| 118 | $io->title($this->getDescription()); |
||
| 119 | |||
| 120 | $this->initializeRepositories((int) $input->getOption('pid')); |
||
| 121 | |||
| 122 | if ($this->storagePid == 0) { |
||
| 123 | $io->error('ERROR: No valid PID (' . $this->storagePid . ') given.'); |
||
| 124 | return BaseCommand::FAILURE; |
||
| 125 | } |
||
| 126 | |||
| 127 | if ( |
||
| 128 | !empty($input->getOption('solr')) |
||
| 129 | && !is_array($input->getOption('solr')) |
||
| 130 | ) { |
||
| 131 | $allSolrCores = $this->getSolrCores($this->storagePid); |
||
| 132 | $solrCoreUid = $this->getSolrCoreUid($allSolrCores, $input->getOption('solr')); |
||
| 133 | |||
| 134 | // Abort if solrCoreUid is empty or not in the array of allowed solr cores. |
||
| 135 | if (empty($solrCoreUid) || !in_array($solrCoreUid, $allSolrCores)) { |
||
| 136 | $outputSolrCores = []; |
||
| 137 | foreach ($allSolrCores as $indexName => $uid) { |
||
| 138 | $outputSolrCores[] = $uid . ' : ' . $indexName; |
||
| 139 | } |
||
| 140 | if (empty($outputSolrCores)) { |
||
| 141 | $io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. No valid cores found on PID ' . $this->storagePid . ".\n"); |
||
| 142 | return BaseCommand::FAILURE; |
||
| 143 | } else { |
||
| 144 | $io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. ' . "Valid cores are (<uid>:<index_name>):\n" . implode("\n", $outputSolrCores) . "\n"); |
||
| 145 | return BaseCommand::FAILURE; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } else { |
||
| 149 | $io->error('ERROR: Required parameter --solr|-s is missing or array.'); |
||
| 150 | return BaseCommand::FAILURE; |
||
| 151 | } |
||
| 152 | |||
| 153 | if (!empty($input->getOption('owner'))) { |
||
| 154 | if (MathUtility::canBeInterpretedAsInteger($input->getOption('owner'))) { |
||
| 155 | $this->owner = $this->libraryRepository->findByUid(MathUtility::forceIntegerInRange((int) $input->getOption('owner'), 1)); |
||
| 156 | } else { |
||
| 157 | $this->owner = $this->libraryRepository->findOneByIndexName((string) $input->getOption('owner')); |
||
|
|
|||
| 158 | } |
||
| 159 | } else { |
||
| 160 | $this->owner = null; |
||
| 161 | } |
||
| 162 | |||
| 163 | if (!empty($input->getOption('all'))) { |
||
| 164 | if ( |
||
| 165 | !empty($input->getOption('index-limit')) |
||
| 166 | && $input->getOption('index-begin') >= 0 |
||
| 167 | ) { |
||
| 168 | // Get all documents for given limit and start. |
||
| 169 | $documents = $this->documentRepository->findAll() |
||
| 170 | ->getQuery() |
||
| 171 | ->setLimit((int) $input->getOption('index-limit')) |
||
| 172 | ->setOffset((int) $input->getOption('index-begin')) |
||
| 173 | ->execute(); |
||
| 174 | $io->writeln($input->getOption('index-limit') . ' documents starting from ' . $input->getOption('index-begin') . ' will be indexed.'); |
||
| 175 | } else { |
||
| 176 | // Get all documents. |
||
| 177 | $documents = $this->documentRepository->findAll(); |
||
| 178 | } |
||
| 179 | } elseif ( |
||
| 180 | !empty($input->getOption('coll')) |
||
| 181 | && !is_array($input->getOption('coll')) |
||
| 182 | ) { |
||
| 183 | $collections = GeneralUtility::intExplode(',', $input->getOption('coll'), true); |
||
| 184 | // "coll" may be a single integer or a comma-separated list of integers. |
||
| 185 | if (empty(array_filter($collections))) { |
||
| 186 | $io->error('ERROR: Parameter --coll|-c is not a valid comma-separated list of collection UIDs.'); |
||
| 187 | return BaseCommand::FAILURE; |
||
| 188 | } |
||
| 189 | |||
| 190 | if ( |
||
| 191 | !empty($input->getOption('index-limit')) |
||
| 192 | && $input->getOption('index-begin') >= 0 |
||
| 193 | ) { |
||
| 194 | $documents = $this->documentRepository->findAllByCollectionsLimited($collections, (int) $input->getOption('index-limit'), (int) $input->getOption('index-begin')); |
||
| 195 | |||
| 196 | $io->writeln($input->getOption('index-limit') . ' documents starting from ' . $input->getOption('index-begin') . ' will be indexed.'); |
||
| 197 | } else { |
||
| 198 | // Get all documents of given collections. |
||
| 199 | $documents = $this->documentRepository->findAllByCollectionsLimited($collections, 0); |
||
| 200 | } |
||
| 201 | } else { |
||
| 202 | $io->error('ERROR: One of parameters --all|-a or --coll|-c must be given.'); |
||
| 203 | return BaseCommand::FAILURE; |
||
| 204 | } |
||
| 205 | |||
| 206 | foreach ($documents as $id => $document) { |
||
| 207 | $doc = AbstractDocument::getInstance($document->getLocation(), ['storagePid' => $this->storagePid], true); |
||
| 208 | |||
| 209 | if ($doc === null) { |
||
| 210 | $io->warning('WARNING: Document "' . $document->getLocation() . '" could not be loaded. Skip to next document.'); |
||
| 211 | continue; |
||
| 212 | } |
||
| 213 | |||
| 214 | if ($dryRun) { |
||
| 215 | $io->writeln('DRY RUN: Would index ' . ($id + 1) . '/' . count($documents) . ' with UID "' . $document->getUid() . '" ("' . $document->getLocation() . '") on PID ' . $this->storagePid . ' and Solr core ' . $solrCoreUid . '.'); |
||
| 216 | } else { |
||
| 217 | if ($io->isVerbose()) { |
||
| 218 | $io->writeln(date('Y-m-d H:i:s') . ' Indexing ' . ($id + 1) . '/' . count($documents) . ' with UID "' . $document->getUid() . '" ("' . $document->getLocation() . '") on PID ' . $this->storagePid . ' and Solr core ' . $solrCoreUid . '.'); |
||
| 219 | } |
||
| 220 | $document->setCurrentDocument($doc); |
||
| 221 | // save to database |
||
| 222 | $this->saveToDatabase($document, $input->getOption('softCommit')); |
||
| 223 | // add to index |
||
| 224 | Indexer::add($document, $this->documentRepository, $input->getOption('softCommit')); |
||
| 225 | } |
||
| 226 | // Clear document cache to prevent memory exhaustion. |
||
| 227 | AbstractDocument::clearDocumentCache(); |
||
| 228 | } |
||
| 229 | |||
| 230 | // Clear state of persistence manager to prevent memory exhaustion. |
||
| 231 | $this->persistenceManager->clearState(); |
||
| 232 | |||
| 233 | $io->success('All done!'); |
||
| 234 | |||
| 235 | return BaseCommand::SUCCESS; |
||
| 236 | } |
||
| 238 |