We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Conditions | 33 |
Paths | > 20000 |
Total Lines | 161 |
Code Lines | 104 |
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 |
||
110 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
111 | { |
||
112 | $dryRun = $input->getOption('dry-run') != false ? true : false; |
||
113 | |||
114 | $io = new SymfonyStyle($input, $output); |
||
115 | $io->title($this->getDescription()); |
||
116 | |||
117 | $this->initializeRepositories((int) $input->getOption('pid')); |
||
118 | |||
119 | if ($this->storagePid == 0) { |
||
120 | $io->error('ERROR: No valid PID (' . $this->storagePid . ') given.'); |
||
121 | return BaseCommand::FAILURE; |
||
122 | } |
||
123 | |||
124 | if ( |
||
125 | !empty($input->getOption('solr')) |
||
126 | && !is_array($input->getOption('solr')) |
||
127 | ) { |
||
128 | $allSolrCores = $this->getSolrCores($this->storagePid); |
||
129 | if (MathUtility::canBeInterpretedAsInteger($input->getOption('solr'))) { |
||
130 | $solrCoreUid = MathUtility::forceIntegerInRange((int) $input->getOption('solr'), 0); |
||
131 | } else { |
||
132 | $solrCoreUid = $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 (MathUtility::canBeInterpretedAsInteger($input->getOption('lib'))) { |
||
154 | $this->owner = $this->libraryRepository->findByUid(MathUtility::forceIntegerInRange((int) $input->getOption('lib'), 1)); |
||
155 | } |
||
156 | |||
157 | if ($this->owner) { |
||
158 | $baseUrl = $this->owner->getOaiBase(); |
||
159 | } else { |
||
160 | $io->error('ERROR: Required parameter --lib|-l is not a valid UID.'); |
||
161 | return BaseCommand::FAILURE; |
||
162 | } |
||
163 | if (!GeneralUtility::isValidUrl($baseUrl)) { |
||
164 | $io->error('ERROR: No valid OAI Base URL set for library with given UID ("' . $input->getOption('lib') . '").'); |
||
165 | return BaseCommand::FAILURE; |
||
166 | } else { |
||
167 | try { |
||
168 | $oai = Endpoint::build($baseUrl); |
||
169 | } catch (BaseoaipmhException $e) { |
||
170 | $this->handleOaiError($e, $io); |
||
171 | } |
||
172 | } |
||
173 | |||
174 | if ( |
||
175 | !is_array($input->getOption('from')) |
||
176 | && preg_match('/^\d{4}-\d{2}-\d{2}$/', $input->getOption('from')) |
||
177 | ) { |
||
178 | $from = new \DateTime($input->getOption('from')); |
||
179 | } else { |
||
180 | $from = null; |
||
181 | } |
||
182 | |||
183 | if ( |
||
184 | !is_array($input->getOption('until')) |
||
185 | && preg_match('/^\d{4}-\d{2}-\d{2}$/', $input->getOption('until')) |
||
186 | ) { |
||
187 | $until = new \DateTime($input->getOption('until')); |
||
188 | } else { |
||
189 | $until = null; |
||
190 | } |
||
191 | |||
192 | $set = null; |
||
193 | if ( |
||
194 | !is_array($input->getOption('set')) |
||
195 | && !empty($input->getOption('set')) |
||
196 | && !empty($oai) |
||
197 | ) { |
||
198 | $setsAvailable = $oai->listSets(); |
||
199 | foreach ($setsAvailable as $setAvailable) { |
||
200 | if ((string) $setAvailable->setSpec === $input->getOption('set')) { |
||
201 | $set = $input->getOption('set'); |
||
202 | break; |
||
203 | } |
||
204 | } |
||
205 | if (empty($set)) { |
||
206 | $io->error('ERROR: OAI interface does not provide a set with given setSpec ("' . $input->getOption('set') . '").'); |
||
207 | return BaseCommand::FAILURE; |
||
208 | } |
||
209 | } |
||
210 | |||
211 | $identifiers = []; |
||
212 | // Get OAI record identifiers to process. |
||
213 | try { |
||
214 | if (!empty($oai)) { |
||
215 | $identifiers = $oai->listIdentifiers('mets', $from, $until, $set); |
||
216 | } else { |
||
217 | $io->error('ERROR: OAI interface does not exist.'); |
||
218 | } |
||
219 | } catch (BaseoaipmhException $exception) { |
||
220 | $this->handleOaiError($exception, $io); |
||
221 | } |
||
222 | |||
223 | // Process all identifiers. |
||
224 | $baseLocation = $baseUrl . (parse_url($baseUrl, PHP_URL_QUERY) ? '&' : '?'); |
||
225 | foreach ($identifiers as $identifier) { |
||
226 | // Build OAI GetRecord URL... |
||
227 | $params = [ |
||
228 | 'verb' => 'GetRecord', |
||
229 | 'metadataPrefix' => 'mets', |
||
230 | 'identifier' => (string) $identifier->identifier |
||
231 | ]; |
||
232 | $docLocation = $baseLocation . http_build_query($params); |
||
233 | // ...index the document... |
||
234 | $document = null; |
||
235 | $doc = AbstractDocument::getInstance($docLocation, ['storagePid' => $this->storagePid], true); |
||
236 | |||
237 | if ($doc === null) { |
||
238 | $io->warning('WARNING: Document "' . $docLocation . '" could not be loaded. Skip to next document.'); |
||
239 | continue; |
||
240 | } |
||
241 | |||
242 | if ($doc->recordId) { |
||
243 | $document = $this->documentRepository->findOneByRecordId($doc->recordId); |
||
|
|||
244 | } |
||
245 | |||
246 | if ($document === null) { |
||
247 | // create new Document object |
||
248 | $document = GeneralUtility::makeInstance(Document::class); |
||
249 | } |
||
250 | |||
251 | $document->setLocation($docLocation); |
||
252 | $document->setSolrcore($solrCoreUid); |
||
253 | |||
254 | if ($dryRun) { |
||
255 | $io->writeln('DRY RUN: Would index ' . $document->getUid() . ' ("' . $document->getLocation() . '") on PID ' . $this->storagePid . ' and Solr core ' . $solrCoreUid . '.'); |
||
256 | } else { |
||
257 | if ($io->isVerbose()) { |
||
258 | $io->writeln(date('Y-m-d H:i:s') . ' Indexing ' . $document->getUid() . ' ("' . $document->getLocation() . '") on PID ' . $this->storagePid . ' and Solr core ' . $solrCoreUid . '.'); |
||
259 | } |
||
260 | $document->setCurrentDocument($doc); |
||
261 | // save to database |
||
262 | $this->saveToDatabase($document, $input->getOption('softCommit')); |
||
263 | // add to index |
||
264 | Indexer::add($document, $this->documentRepository, $input->getOption('softCommit')); |
||
265 | } |
||
266 | } |
||
267 | |||
268 | $io->success('All done!'); |
||
269 | |||
270 | return BaseCommand::SUCCESS; |
||
271 | } |
||
288 |