We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Conditions | 25 |
| Paths | 440 |
| Total Lines | 121 |
| Code Lines | 71 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 89 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 90 | { |
||
| 91 | $dryRun = $input->getOption('dry-run') != false ? true : false; |
||
| 92 | |||
| 93 | $io = new SymfonyStyle($input, $output); |
||
| 94 | $io->title($this->getDescription()); |
||
| 95 | |||
| 96 | $this->initializeRepositories($input->getOption('pid')); |
||
| 97 | |||
| 98 | if ($this->storagePid == 0) { |
||
| 99 | $io->error('ERROR: No valid PID (' . $this->storagePid . ') given.'); |
||
| 100 | exit(1); |
||
| 101 | } |
||
| 102 | |||
| 103 | |||
| 104 | if ( |
||
| 105 | !empty($input->getOption('solr')) |
||
| 106 | && !is_array($input->getOption('solr')) |
||
| 107 | ) { |
||
| 108 | $allSolrCores = $this->getSolrCores($this->storagePid); |
||
| 109 | $solrCoreUid = $this->getSolrCoreUid($allSolrCores, $input->getOption('solr')); |
||
| 110 | |||
| 111 | // Abort if solrCoreUid is empty or not in the array of allowed solr cores. |
||
| 112 | if (empty($solrCoreUid) || !in_array($solrCoreUid, $allSolrCores)) { |
||
| 113 | $output_solrCores = []; |
||
| 114 | foreach ($allSolrCores as $index_name => $uid) { |
||
| 115 | $output_solrCores[] = $uid . ' : ' . $index_name; |
||
| 116 | } |
||
| 117 | if (empty($output_solrCores)) { |
||
| 118 | $io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. No valid cores found on PID ' . $this->storagePid . ".\n"); |
||
| 119 | exit(1); |
||
| 120 | } else { |
||
| 121 | $io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. ' . "Valid cores are (<uid>:<index_name>):\n" . implode("\n", $output_solrCores) . "\n"); |
||
| 122 | exit(1); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } else { |
||
| 126 | $io->error('ERROR: Required parameter --solr|-s is missing or array.'); |
||
| 127 | exit(1); |
||
| 128 | } |
||
| 129 | |||
| 130 | if ( |
||
| 131 | empty($input->getOption('doc')) |
||
| 132 | || is_array($input->getOption('doc')) |
||
| 133 | || ( |
||
| 134 | !MathUtility::canBeInterpretedAsInteger($input->getOption('doc')) |
||
| 135 | && !GeneralUtility::isValidUrl($input->getOption('doc')) |
||
| 136 | ) |
||
| 137 | ) { |
||
| 138 | $io->error('ERROR: Required parameter --doc|-d is not a valid document UID or URL.'); |
||
| 139 | exit(1); |
||
| 140 | } |
||
| 141 | |||
| 142 | if (!empty($input->getOption('owner'))) { |
||
| 143 | if (MathUtility::canBeInterpretedAsInteger($input->getOption('owner'))) { |
||
| 144 | $this->owner = $this->libraryRepository->findByUid(MathUtility::forceIntegerInRange((int) $input->getOption('owner'), 1)); |
||
| 145 | } else { |
||
| 146 | $this->owner = $this->libraryRepository->findOneByIndexName((string) $input->getOption('owner')); |
||
|
|
|||
| 147 | } |
||
| 148 | } else { |
||
| 149 | $this->owner = null; |
||
| 150 | } |
||
| 151 | |||
| 152 | // Try to find existing document in database |
||
| 153 | if (MathUtility::canBeInterpretedAsInteger($input->getOption('doc'))) { |
||
| 154 | |||
| 155 | $document = $this->documentRepository->findByUid($input->getOption('doc')); |
||
| 156 | |||
| 157 | if ($document === null) { |
||
| 158 | $io->error('ERROR: Document with UID "' . $input->getOption('doc') . '" could not be found on PID ' . $this->storagePid . ' .'); |
||
| 159 | exit(1); |
||
| 160 | } else { |
||
| 161 | $doc = Doc::getInstance($document->getLocation(), ['storagePid' => $this->storagePid], true); |
||
| 162 | } |
||
| 163 | |||
| 164 | } else if (GeneralUtility::isValidUrl($input->getOption('doc'))) { |
||
| 165 | $doc = Doc::getInstance($input->getOption('doc'), ['storagePid' => $this->storagePid], true); |
||
| 166 | |||
| 167 | if ($doc->recordId) { |
||
| 168 | $document = $this->documentRepository->findOneByRecordId($doc->recordId); |
||
| 169 | } |
||
| 170 | |||
| 171 | if ($document === null) { |
||
| 172 | // create new Document object |
||
| 173 | $document = $this->objectManager->get(Document::class); |
||
| 174 | } |
||
| 175 | |||
| 176 | // now there must exist a document object |
||
| 177 | if ($document) { |
||
| 178 | $document->setLocation($input->getOption('doc')); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | if ($doc === null) { |
||
| 183 | $io->error('ERROR: Document "' . $input->getOption('doc') . '" could not be loaded.'); |
||
| 184 | exit(1); |
||
| 185 | } |
||
| 186 | |||
| 187 | $document->setDoc($doc); |
||
| 188 | |||
| 189 | if ($this->owner !== null) { |
||
| 190 | $document->setOwner($this->owner); |
||
| 191 | } |
||
| 192 | |||
| 193 | $document->setSolrcore($solrCoreUid); |
||
| 194 | |||
| 195 | if ($dryRun) { |
||
| 196 | $io->section('DRY RUN: Would index ' . $document->getUid() . ' ("' . $document->getLocation() . '") on PID ' . $this->storagePid . ' and Solr core ' . $solrCoreUid . '.'); |
||
| 197 | } else { |
||
| 198 | if ($io->isVerbose()) { |
||
| 199 | $io->section('Indexing ' . $document->getUid() . ' ("' . $document->getLocation() . '") on PID ' . $this->storagePid . ' and Solr core ' . $solrCoreUid . '.'); |
||
| 200 | } |
||
| 201 | // save to database |
||
| 202 | $this->saveToDatabase($document); |
||
| 203 | // add to index |
||
| 204 | Indexer::add($document, $solrCoreUid); |
||
| 205 | } |
||
| 206 | |||
| 207 | $io->success('All done!'); |
||
| 208 | |||
| 209 | return 0; |
||
| 210 | } |
||
| 213 |