We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 50 |
| Total Lines | 372 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 0 |
Complex classes like BaseCommand often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BaseCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class BaseCommand extends Command |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * @access protected |
||
| 48 | * @var CollectionRepository |
||
| 49 | */ |
||
| 50 | protected CollectionRepository $collectionRepository; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @access protected |
||
| 54 | * @var DocumentRepository |
||
| 55 | */ |
||
| 56 | protected DocumentRepository $documentRepository; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @access protected |
||
| 60 | * @var LibraryRepository |
||
| 61 | */ |
||
| 62 | protected LibraryRepository $libraryRepository; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @access protected |
||
| 66 | * @var StructureRepository |
||
| 67 | */ |
||
| 68 | protected StructureRepository $structureRepository; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @access protected |
||
| 72 | * @var int |
||
| 73 | */ |
||
| 74 | protected int $storagePid; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @access protected |
||
| 78 | * @var Library|null |
||
| 79 | */ |
||
| 80 | protected ?Library $owner; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @access protected |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | protected array $extConf; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @access protected |
||
| 90 | * @var ConfigurationManager |
||
| 91 | */ |
||
| 92 | protected ConfigurationManager $configurationManager; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @access protected |
||
| 96 | * @var PersistenceManager |
||
| 97 | */ |
||
| 98 | protected PersistenceManager $persistenceManager; |
||
| 99 | |||
| 100 | public function __construct( |
||
| 101 | CollectionRepository $collectionRepository, |
||
| 102 | DocumentRepository $documentRepository, |
||
| 103 | LibraryRepository $libraryRepository, |
||
| 104 | StructureRepository $structureRepository, |
||
| 105 | ConfigurationManager $configurationManager, |
||
| 106 | PersistenceManager $persistenceManager |
||
| 107 | ) { |
||
| 108 | parent::__construct(); |
||
| 109 | |||
| 110 | $this->collectionRepository = $collectionRepository; |
||
| 111 | $this->documentRepository = $documentRepository; |
||
| 112 | $this->libraryRepository = $libraryRepository; |
||
| 113 | $this->structureRepository = $structureRepository; |
||
| 114 | $this->configurationManager = $configurationManager; |
||
| 115 | $this->persistenceManager = $persistenceManager; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Initialize the extbase repository based on the given storagePid. |
||
| 120 | * |
||
| 121 | * TYPO3 10+: Find a better solution e.g. based on Symfony Dependency Injection. |
||
| 122 | * |
||
| 123 | * @access protected |
||
| 124 | * |
||
| 125 | * @param int $storagePid The storage pid |
||
| 126 | * |
||
| 127 | * @return void |
||
| 128 | */ |
||
| 129 | protected function initializeRepositories(int $storagePid): void |
||
| 130 | { |
||
| 131 | $frameworkConfiguration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK); |
||
| 132 | $frameworkConfiguration['persistence']['storagePid'] = MathUtility::forceIntegerInRange($storagePid, 0); |
||
| 133 | $this->configurationManager->setConfiguration($frameworkConfiguration); |
||
| 134 | $this->extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('dlf'); |
||
| 135 | $this->storagePid = MathUtility::forceIntegerInRange($storagePid, 0); |
||
| 136 | $this->persistenceManager = GeneralUtility::makeInstance(PersistenceManager::class); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Return matching uid of Solr core depending on the input value. |
||
| 141 | * |
||
| 142 | * @access protected |
||
| 143 | * |
||
| 144 | * @param array $solrCores array of the valid Solr cores |
||
| 145 | * @param string|bool|null $inputSolrId possible uid or name of Solr core |
||
| 146 | * |
||
| 147 | * @return int matching uid of Solr core |
||
| 148 | */ |
||
| 149 | protected function getSolrCoreUid(array $solrCores, $inputSolrId): ?int |
||
| 150 | { |
||
| 151 | if (MathUtility::canBeInterpretedAsInteger($inputSolrId)) { |
||
| 152 | $solrCoreUid = MathUtility::forceIntegerInRange((int) $inputSolrId, 0); |
||
| 153 | } else { |
||
| 154 | $solrCoreUid = $solrCores[$inputSolrId]; |
||
| 155 | } |
||
| 156 | return $solrCoreUid; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Fetches all Solr cores on given page. |
||
| 161 | * |
||
| 162 | * @access protected |
||
| 163 | * |
||
| 164 | * @param int $pageId The UID of the Solr core or 0 to disable indexing |
||
| 165 | * |
||
| 166 | * @return array Array of valid Solr cores |
||
| 167 | */ |
||
| 168 | protected function getSolrCores(int $pageId): array |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Update or insert document to database |
||
| 194 | * |
||
| 195 | * @access protected |
||
| 196 | * |
||
| 197 | * @param Document $document The document instance |
||
| 198 | * @param bool $softCommit If true, documents are just added to the index by a soft commit |
||
| 199 | * |
||
| 200 | * @return bool true on success, false otherwise |
||
| 201 | */ |
||
| 202 | protected function saveToDatabase(Document $document, bool $softCommit = false): bool |
||
| 203 | { |
||
| 204 | $doc = $document->getCurrentDocument(); |
||
| 205 | if ($doc === null) { |
||
| 206 | return false; |
||
| 207 | } |
||
| 208 | |||
| 209 | $doc->cPid = $this->storagePid; |
||
| 210 | |||
| 211 | $metadata = $doc->getToplevelMetadata($this->storagePid); |
||
| 212 | $validator = new DocumentValidator($metadata, explode(',', $this->extConf['general']['requiredMetadataFields'])); |
||
| 213 | |||
| 214 | if ($validator->hasAllMandatoryMetadataFields()) { |
||
| 215 | // set title data |
||
| 216 | $document->setTitle($metadata['title'][0] ? : ''); |
||
| 217 | $document->setTitleSorting($metadata['title_sorting'][0] ? : ''); |
||
| 218 | $document->setPlace(implode('; ', $metadata['place'])); |
||
| 219 | $document->setYear(implode('; ', $metadata['year'])); |
||
| 220 | $document->setAuthor($this->getAuthors($metadata['author'])); |
||
| 221 | $document->setThumbnail($doc->thumbnail ? : ''); |
||
| 222 | $document->setMetsLabel($metadata['mets_label'][0] ? : ''); |
||
| 223 | $document->setMetsOrderlabel($metadata['mets_orderlabel'][0] ? : ''); |
||
| 224 | |||
| 225 | $structure = $this->structureRepository->findOneByIndexName($metadata['type'][0]); |
||
|
|
|||
| 226 | $document->setStructure($structure); |
||
| 227 | |||
| 228 | if (is_array($metadata['collection'])) { |
||
| 229 | $this->addCollections($document, $metadata['collection']); |
||
| 230 | } |
||
| 231 | |||
| 232 | // set identifiers |
||
| 233 | $document->setProdId($metadata['prod_id'][0] ? : ''); |
||
| 234 | $document->setOpacId($metadata['opac_id'][0] ? : ''); |
||
| 235 | $document->setUnionId($metadata['union_id'][0] ? : ''); |
||
| 236 | |||
| 237 | $document->setRecordId($metadata['record_id'][0]); |
||
| 238 | $document->setUrn($metadata['urn'][0] ? : ''); |
||
| 239 | $document->setPurl($metadata['purl'][0] ? : ''); |
||
| 240 | $document->setDocumentFormat($metadata['document_format'][0] ? : ''); |
||
| 241 | |||
| 242 | // set access |
||
| 243 | $document->setLicense($metadata['license'][0] ? : ''); |
||
| 244 | $document->setTerms($metadata['terms'][0] ? : ''); |
||
| 245 | $document->setRestrictions($metadata['restrictions'][0] ? : ''); |
||
| 246 | $document->setOutOfPrint($metadata['out_of_print'][0] ? : ''); |
||
| 247 | $document->setRightsInfo($metadata['rights_info'][0] ? : ''); |
||
| 248 | $document->setStatus(0); |
||
| 249 | |||
| 250 | $this->setOwner($metadata['owner'][0]); |
||
| 251 | $document->setOwner($this->owner); |
||
| 252 | |||
| 253 | // set volume data |
||
| 254 | $document->setVolume($metadata['volume'][0] ? : ''); |
||
| 255 | $document->setVolumeSorting($metadata['volume_sorting'][0] ? : $metadata['mets_order'][0] ? : ''); |
||
| 256 | |||
| 257 | // Get UID of parent document. |
||
| 258 | if ($document->getDocumentFormat() === 'METS') { |
||
| 259 | $document->setPartof($this->getParentDocumentUidForSaving($document, $softCommit)); |
||
| 260 | } |
||
| 261 | |||
| 262 | if ($document->getUid() === null) { |
||
| 263 | // new document |
||
| 264 | $this->documentRepository->add($document); |
||
| 265 | } else { |
||
| 266 | // update of existing document |
||
| 267 | $this->documentRepository->update($document); |
||
| 268 | } |
||
| 269 | |||
| 270 | $this->persistenceManager->persistAll(); |
||
| 271 | |||
| 272 | return true; |
||
| 273 | } |
||
| 274 | |||
| 275 | return false; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Get the ID of the parent document if the current document has one. |
||
| 280 | * Currently only applies to METS documents. |
||
| 281 | * |
||
| 282 | * @access protected |
||
| 283 | * |
||
| 284 | * @param Document $document for which parent UID should be taken |
||
| 285 | * @param bool $softCommit If true, documents are just added to the index by a soft commit |
||
| 286 | * |
||
| 287 | * @return int The parent document's id. |
||
| 288 | */ |
||
| 289 | protected function getParentDocumentUidForSaving(Document $document, bool $softCommit = false): int |
||
| 290 | { |
||
| 291 | $doc = $document->getCurrentDocument(); |
||
| 292 | |||
| 293 | if ($doc !== null && !empty($doc->parentHref)) { |
||
| 294 | // find document object by record_id of parent |
||
| 295 | $parent = AbstractDocument::getInstance($doc->parentHref, ['storagePid' => $this->storagePid]); |
||
| 296 | |||
| 297 | if ($parent->recordId) { |
||
| 298 | $parentDocument = $this->documentRepository->findOneByRecordId($parent->recordId); |
||
| 299 | |||
| 300 | if ($parentDocument === null) { |
||
| 301 | // create new Document object |
||
| 302 | $parentDocument = GeneralUtility::makeInstance(Document::class); |
||
| 303 | } |
||
| 304 | |||
| 305 | $parentDocument->setOwner($this->owner); |
||
| 306 | $parentDocument->setCurrentDocument($parent); |
||
| 307 | $parentDocument->setLocation($doc->parentHref); |
||
| 308 | $parentDocument->setSolrcore($document->getSolrcore()); |
||
| 309 | |||
| 310 | $success = $this->saveToDatabase($parentDocument, $softCommit); |
||
| 311 | |||
| 312 | if ($success === true) { |
||
| 313 | // add to index |
||
| 314 | Indexer::add($parentDocument, $this->documentRepository, $softCommit); |
||
| 315 | return $parentDocument->getUid(); |
||
| 316 | } |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | return 0; |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Add collections. |
||
| 325 | * |
||
| 326 | * @access private |
||
| 327 | * |
||
| 328 | * @param Document &$document |
||
| 329 | * @param array $collections |
||
| 330 | * |
||
| 331 | * @return void |
||
| 332 | */ |
||
| 333 | private function addCollections(Document &$document, array $collections): void |
||
| 334 | { |
||
| 335 | foreach ($collections as $collection) { |
||
| 336 | $documentCollection = $this->collectionRepository->findOneByIndexName($collection); |
||
| 337 | if (!$documentCollection) { |
||
| 338 | // create new Collection object |
||
| 339 | $documentCollection = GeneralUtility::makeInstance(Collection::class); |
||
| 340 | $documentCollection->setIndexName($collection); |
||
| 341 | $documentCollection->setLabel($collection); |
||
| 342 | $documentCollection->setOaiName((!empty($this->extConf['general']['publishNewCollections']) ? Helper::getCleanString($collection) : '')); |
||
| 343 | $documentCollection->setIndexSearch(''); |
||
| 344 | $documentCollection->setDescription(''); |
||
| 345 | // add to CollectionRepository |
||
| 346 | $this->collectionRepository->add($documentCollection); |
||
| 347 | // persist collection to prevent duplicates |
||
| 348 | $this->persistenceManager->persistAll(); |
||
| 349 | } |
||
| 350 | // add to document |
||
| 351 | $document->addCollection($documentCollection); |
||
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Get authors considering that database field can't accept |
||
| 357 | * more than 255 characters. |
||
| 358 | * |
||
| 359 | * @access private |
||
| 360 | * |
||
| 361 | * @param array $metadataAuthor |
||
| 362 | * |
||
| 363 | * @return string |
||
| 364 | */ |
||
| 365 | private function getAuthors(array $metadataAuthor): string |
||
| 366 | { |
||
| 367 | // Remove appended "valueURI" from authors' names for storing in database. |
||
| 368 | foreach ($metadataAuthor as $i => $author) { |
||
| 369 | $splitName = explode(pack('C', 31), $author); |
||
| 370 | $metadataAuthor[$i] = $splitName[0]; |
||
| 371 | } |
||
| 372 | |||
| 373 | $authors = ''; |
||
| 374 | $delimiter = '; '; |
||
| 375 | $ellipsis = 'et al.'; |
||
| 376 | |||
| 377 | $count = count($metadataAuthor); |
||
| 378 | |||
| 379 | for ($i = 0; $i < $count; $i++) { |
||
| 380 | // Build the next part to add |
||
| 381 | $nextPart = ($i === 0 ? '' : $delimiter) . $metadataAuthor[$i]; |
||
| 382 | // Check if adding this part and ellipsis in future would exceed the character limit |
||
| 383 | if (strlen($authors . $nextPart . $delimiter . $ellipsis) > 255) { |
||
| 384 | // Add ellipsis and stop adding more authors |
||
| 385 | $authors .= $delimiter . $ellipsis; |
||
| 386 | break; |
||
| 387 | } |
||
| 388 | // Add the part to the main string |
||
| 389 | $authors .= $nextPart; |
||
| 390 | } |
||
| 391 | |||
| 392 | return $authors; |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * If owner is not set set but found by metadata, take it or take default library, if nothing found in database then create new owner. |
||
| 397 | * |
||
| 398 | * @access private |
||
| 399 | * |
||
| 400 | * @param ?string $owner |
||
| 401 | * |
||
| 402 | * @return void |
||
| 403 | */ |
||
| 404 | private function setOwner($owner): void |
||
| 416 | } |
||
| 417 | } |
||
| 418 | } |
||
| 419 | } |
||
| 420 |