We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Conditions | 24 |
| Paths | 236 |
| Total Lines | 120 |
| Code Lines | 78 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 97 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 98 | { |
||
| 99 | $dryRun = $input->getOption('dry-run') != false ? true : false; |
||
| 100 | |||
| 101 | $io = new SymfonyStyle($input, $output); |
||
| 102 | $io->title($this->getDescription()); |
||
| 103 | |||
| 104 | $this->initializeRepositories((int) $input->getOption('pid')); |
||
| 105 | |||
| 106 | if ($this->storagePid == 0) { |
||
| 107 | $io->error('ERROR: No valid PID (' . $this->storagePid . ') given.'); |
||
| 108 | return BaseCommand::FAILURE; |
||
| 109 | } |
||
| 110 | |||
| 111 | if ( |
||
| 112 | !empty($input->getOption('solr')) |
||
| 113 | && !is_array($input->getOption('solr')) |
||
| 114 | ) { |
||
| 115 | $allSolrCores = $this->getSolrCores($this->storagePid); |
||
| 116 | $solrCoreUid = $this->getSolrCoreUid($allSolrCores, $input->getOption('solr')); |
||
| 117 | |||
| 118 | // Abort if solrCoreUid is empty or not in the array of allowed solr cores. |
||
| 119 | if (empty($solrCoreUid) || !in_array($solrCoreUid, $allSolrCores)) { |
||
| 120 | $outputSolrCores = []; |
||
| 121 | foreach ($allSolrCores as $indexName => $uid) { |
||
| 122 | $outputSolrCores[] = $uid . ' : ' . $indexName; |
||
| 123 | } |
||
| 124 | if (empty($outputSolrCores)) { |
||
| 125 | $io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. No valid cores found on PID ' . $this->storagePid . ".\n"); |
||
| 126 | return BaseCommand::FAILURE; |
||
| 127 | } else { |
||
| 128 | $io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. ' . "Valid cores are (<uid>:<index_name>):\n" . implode("\n", $outputSolrCores) . "\n"); |
||
| 129 | return BaseCommand::FAILURE; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | } else { |
||
| 133 | $io->error('ERROR: Required parameter --solr|-s is missing or array.'); |
||
| 134 | return BaseCommand::FAILURE; |
||
| 135 | } |
||
| 136 | |||
| 137 | if ( |
||
| 138 | empty($input->getOption('doc')) |
||
| 139 | || is_array($input->getOption('doc')) |
||
| 140 | || ( |
||
| 141 | !MathUtility::canBeInterpretedAsInteger($input->getOption('doc')) |
||
| 142 | && !GeneralUtility::isValidUrl($input->getOption('doc')) |
||
| 143 | ) |
||
| 144 | ) { |
||
| 145 | $io->error('ERROR: Required parameter --doc|-d is not a valid document UID or URL.'); |
||
| 146 | return BaseCommand::FAILURE; |
||
| 147 | } |
||
| 148 | |||
| 149 | if (!empty($input->getOption('owner'))) { |
||
| 150 | if (MathUtility::canBeInterpretedAsInteger($input->getOption('owner'))) { |
||
| 151 | $this->owner = $this->libraryRepository->findByUid(MathUtility::forceIntegerInRange((int) $input->getOption('owner'), 1)); |
||
| 152 | } else { |
||
| 153 | $this->owner = $this->libraryRepository->findOneByIndexName((string) $input->getOption('owner')); |
||
|
|
|||
| 154 | } |
||
| 155 | } else { |
||
| 156 | $this->owner = null; |
||
| 157 | } |
||
| 158 | |||
| 159 | $document = null; |
||
| 160 | $doc = null; |
||
| 161 | |||
| 162 | // Try to find existing document in database |
||
| 163 | if (MathUtility::canBeInterpretedAsInteger($input->getOption('doc'))) { |
||
| 164 | |||
| 165 | $document = $this->documentRepository->findByUid($input->getOption('doc')); |
||
| 166 | |||
| 167 | if ($document === null) { |
||
| 168 | $io->error('ERROR: Document with UID "' . $input->getOption('doc') . '" could not be found on PID ' . $this->storagePid . ' .'); |
||
| 169 | return BaseCommand::FAILURE; |
||
| 170 | } else { |
||
| 171 | $doc = AbstractDocument::getInstance($document->getLocation(), ['storagePid' => $this->storagePid], true); |
||
| 172 | } |
||
| 173 | |||
| 174 | } else if (GeneralUtility::isValidUrl($input->getOption('doc'))) { |
||
| 175 | $doc = AbstractDocument::getInstance($input->getOption('doc'), ['storagePid' => $this->storagePid], true); |
||
| 176 | |||
| 177 | $document = $this->getDocumentFromUrl($doc, $input->getOption('doc')); |
||
| 178 | } |
||
| 179 | |||
| 180 | if ($doc === null) { |
||
| 181 | $io->error('ERROR: Document "' . $input->getOption('doc') . '" could not be loaded.'); |
||
| 182 | return BaseCommand::FAILURE; |
||
| 183 | } |
||
| 184 | |||
| 185 | $document->setSolrcore($solrCoreUid); |
||
| 186 | |||
| 187 | if ($dryRun) { |
||
| 188 | $io->section('DRY RUN: Would index ' . $document->getUid() . ' ("' . $document->getLocation() . '") on PID ' . $this->storagePid . ' and Solr core ' . $solrCoreUid . '.'); |
||
| 189 | $io->success('All done!'); |
||
| 190 | return BaseCommand::SUCCESS; |
||
| 191 | } else { |
||
| 192 | $document->setCurrentDocument($doc); |
||
| 193 | |||
| 194 | if ($io->isVerbose()) { |
||
| 195 | $io->section('Indexing ' . $document->getUid() . ' ("' . $document->getLocation() . '") on PID ' . $this->storagePid . '.'); |
||
| 196 | } |
||
| 197 | $isSaved = $this->saveToDatabase($document, $input->getOption('softCommit')); |
||
| 198 | |||
| 199 | if ($isSaved) { |
||
| 200 | if ($io->isVerbose()) { |
||
| 201 | $io->section('Indexing ' . $document->getUid() . ' ("' . $document->getLocation() . '") on Solr core ' . $solrCoreUid . '.'); |
||
| 202 | } |
||
| 203 | $isSaved = Indexer::add($document, $this->documentRepository, $input->getOption('softCommit')); |
||
| 204 | } else { |
||
| 205 | $io->error('ERROR: Document with UID "' . $document->getUid() . '" could not be indexed on PID ' . $this->storagePid . ' . There are missing mandatory fields (at least one of those: ' . $this->extConf['general']['requiredMetadataFields'] . ') in this document.'); |
||
| 206 | return BaseCommand::FAILURE; |
||
| 207 | } |
||
| 208 | |||
| 209 | if ($isSaved) { |
||
| 210 | $io->success('All done!'); |
||
| 211 | return BaseCommand::SUCCESS; |
||
| 212 | } |
||
| 213 | |||
| 214 | $io->error('ERROR: Document with UID "' . $document->getUid() . '" could not be indexed on Solr core ' . $solrCoreUid . ' . There are missing mandatory fields (at least one of those: ' . $this->extConf['general']['requiredMetadataFields'] . ') in this document.'); |
||
| 215 | $io->info('INFO: Document with UID "' . $document->getUid() . '" is already in database. If you want to keep the database and index consistent you need to remove it.'); |
||
| 216 | return BaseCommand::FAILURE; |
||
| 217 | } |
||
| 253 |