We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 51 |
| Total Lines | 365 |
| 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 |
||
| 43 | class BaseCommand extends Command |
||
| 44 | { |
||
| 45 | /** |
||
| 46 | * @access protected |
||
| 47 | * @var CollectionRepository |
||
| 48 | */ |
||
| 49 | protected CollectionRepository $collectionRepository; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @access protected |
||
| 53 | * @var DocumentRepository |
||
| 54 | */ |
||
| 55 | protected DocumentRepository $documentRepository; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @access protected |
||
| 59 | * @var LibraryRepository |
||
| 60 | */ |
||
| 61 | protected LibraryRepository $libraryRepository; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @access protected |
||
| 65 | * @var StructureRepository |
||
| 66 | */ |
||
| 67 | protected StructureRepository $structureRepository; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @access protected |
||
| 71 | * @var int |
||
| 72 | */ |
||
| 73 | protected int $storagePid; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @access protected |
||
| 77 | * @var Library|null |
||
| 78 | */ |
||
| 79 | protected ?Library $owner; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @access protected |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | protected array $extConf; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var ConfigurationManager |
||
| 89 | */ |
||
| 90 | protected ConfigurationManager $configurationManager; |
||
| 91 | |||
| 92 | public function __construct( |
||
| 93 | CollectionRepository $collectionRepository, |
||
| 94 | DocumentRepository $documentRepository, |
||
| 95 | LibraryRepository $libraryRepository, |
||
| 96 | StructureRepository $structureRepository, |
||
| 97 | ConfigurationManager $configurationManager |
||
| 98 | ) { |
||
| 99 | parent::__construct(); |
||
| 100 | |||
| 101 | $this->collectionRepository = $collectionRepository; |
||
| 102 | $this->documentRepository = $documentRepository; |
||
| 103 | $this->libraryRepository = $libraryRepository; |
||
| 104 | $this->structureRepository = $structureRepository; |
||
| 105 | $this->configurationManager = $configurationManager; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Initialize the extbase repository based on the given storagePid. |
||
| 110 | * |
||
| 111 | * TYPO3 10+: Find a better solution e.g. based on Symfony Dependency Injection. |
||
| 112 | * |
||
| 113 | * @access protected |
||
| 114 | * |
||
| 115 | * @param int $storagePid The storage pid |
||
| 116 | * |
||
| 117 | * @return bool |
||
| 118 | */ |
||
| 119 | protected function initializeRepositories(int $storagePid): bool |
||
| 120 | { |
||
| 121 | if (MathUtility::canBeInterpretedAsInteger($storagePid)) { |
||
| 122 | $frameworkConfiguration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK); |
||
| 123 | |||
| 124 | $frameworkConfiguration['persistence']['storagePid'] = MathUtility::forceIntegerInRange((int) $storagePid, 0); |
||
| 125 | $this->configurationManager->setConfiguration($frameworkConfiguration); |
||
| 126 | |||
| 127 | // Get extension configuration. |
||
| 128 | $this->extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('dlf'); |
||
| 129 | } else { |
||
| 130 | return false; |
||
| 131 | } |
||
| 132 | $this->storagePid = MathUtility::forceIntegerInRange((int) $storagePid, 0); |
||
| 133 | |||
| 134 | return true; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Return matching uid of Solr core depending on the input value. |
||
| 139 | * |
||
| 140 | * @access protected |
||
| 141 | * |
||
| 142 | * @param array $solrCores array of the valid Solr cores |
||
| 143 | * @param string|bool|null $inputSolrId possible uid or name of Solr core |
||
| 144 | * |
||
| 145 | * @return int matching uid of Solr core |
||
| 146 | */ |
||
| 147 | protected function getSolrCoreUid(array $solrCores, $inputSolrId): int |
||
| 148 | { |
||
| 149 | if (MathUtility::canBeInterpretedAsInteger($inputSolrId)) { |
||
| 150 | $solrCoreUid = MathUtility::forceIntegerInRange((int) $inputSolrId, 0); |
||
| 151 | } else { |
||
| 152 | $solrCoreUid = $solrCores[$inputSolrId]; |
||
| 153 | } |
||
| 154 | return $solrCoreUid; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Fetches all Solr cores on given page. |
||
| 159 | * |
||
| 160 | * @access protected |
||
| 161 | * |
||
| 162 | * @param int $pageId The UID of the Solr core or 0 to disable indexing |
||
| 163 | * |
||
| 164 | * @return array Array of valid Solr cores |
||
| 165 | */ |
||
| 166 | protected function getSolrCores(int $pageId): array |
||
| 167 | { |
||
| 168 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 169 | ->getQueryBuilderForTable('tx_dlf_solrcores'); |
||
| 170 | |||
| 171 | $solrCores = []; |
||
| 172 | $result = $queryBuilder |
||
| 173 | ->select('uid', 'index_name') |
||
| 174 | ->from('tx_dlf_solrcores') |
||
| 175 | ->where( |
||
| 176 | $queryBuilder->expr()->eq( |
||
| 177 | 'pid', |
||
| 178 | $queryBuilder->createNamedParameter((int) $pageId, Connection::PARAM_INT) |
||
| 179 | ) |
||
| 180 | ) |
||
| 181 | ->execute(); |
||
| 182 | |||
| 183 | while ($record = $result->fetchAssociative()) { |
||
| 184 | $solrCores[$record['index_name']] = $record['uid']; |
||
| 185 | } |
||
| 186 | |||
| 187 | return $solrCores; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Update or insert document to database |
||
| 192 | * |
||
| 193 | * @access protected |
||
| 194 | * |
||
| 195 | * @param Document $document The document instance |
||
| 196 | * |
||
| 197 | * @return bool true on success, false otherwise |
||
| 198 | */ |
||
| 199 | protected function saveToDatabase(Document $document): bool |
||
| 200 | { |
||
| 201 | $doc = $document->getCurrentDocument(); |
||
| 202 | if ($doc === null) { |
||
| 203 | return false; |
||
| 204 | } |
||
| 205 | $persistenceManager = GeneralUtility::makeInstance(PersistenceManager::class); |
||
| 206 | $doc->cPid = $this->storagePid; |
||
| 207 | |||
| 208 | $metadata = $doc->getToplevelMetadata($this->storagePid); |
||
| 209 | |||
| 210 | // set title data |
||
| 211 | $document->setTitle($metadata['title'][0] ? : ''); |
||
| 212 | $document->setTitleSorting($metadata['title_sorting'][0] ? : ''); |
||
| 213 | $document->setPlace(implode('; ', $metadata['place'])); |
||
| 214 | $document->setYear(implode('; ', $metadata['year'])); |
||
| 215 | |||
| 216 | // Remove appended "valueURI" from authors' names for storing in database. |
||
| 217 | foreach ($metadata['author'] as $i => $author) { |
||
| 218 | $splitName = explode(chr(31), $author); |
||
| 219 | $metadata['author'][$i] = $splitName[0]; |
||
| 220 | } |
||
| 221 | $document->setAuthor($this->getAuthors($metadata['author'])); |
||
| 222 | $document->setThumbnail($doc->thumbnail ? : ''); |
||
| 223 | $document->setMetsLabel($metadata['mets_label'][0] ? : ''); |
||
| 224 | $document->setMetsOrderlabel($metadata['mets_orderlabel'][0] ? : ''); |
||
| 225 | |||
| 226 | $structure = $this->structureRepository->findOneByIndexName($metadata['type'][0]); |
||
|
|
|||
| 227 | $document->setStructure($structure); |
||
| 228 | |||
| 229 | if (is_array($metadata['collection'])) { |
||
| 230 | $this->addCollections($document, $metadata['collection'], $persistenceManager); |
||
| 231 | } |
||
| 232 | |||
| 233 | // set identifiers |
||
| 234 | $document->setProdId($metadata['prod_id'][0] ? : ''); |
||
| 235 | $document->setOpacId($metadata['opac_id'][0] ? : ''); |
||
| 236 | $document->setUnionId($metadata['union_id'][0] ? : ''); |
||
| 237 | |||
| 238 | $document->setRecordId($metadata['record_id'][0] ? : ''); // (?) $doc->recordId |
||
| 239 | $document->setUrn($metadata['urn'][0] ? : ''); |
||
| 240 | $document->setPurl($metadata['purl'][0] ? : ''); |
||
| 241 | $document->setDocumentFormat($metadata['document_format'][0] ? : ''); |
||
| 242 | |||
| 243 | // set access |
||
| 244 | $document->setLicense($metadata['license'][0] ? : ''); |
||
| 245 | $document->setTerms($metadata['terms'][0] ? : ''); |
||
| 246 | $document->setRestrictions($metadata['restrictions'][0] ? : ''); |
||
| 247 | $document->setOutOfPrint($metadata['out_of_print'][0] ? : ''); |
||
| 248 | $document->setRightsInfo($metadata['rights_info'][0] ? : ''); |
||
| 249 | $document->setStatus(0); |
||
| 250 | |||
| 251 | $this->setOwner($metadata['owner'][0]); |
||
| 252 | $document->setOwner($this->owner); |
||
| 253 | |||
| 254 | // set volume data |
||
| 255 | $document->setVolume($metadata['volume'][0] ? : ''); |
||
| 256 | $document->setVolumeSorting($metadata['volume_sorting'][0] ? : $metadata['mets_order'][0] ? : ''); |
||
| 257 | |||
| 258 | // Get UID of parent document. |
||
| 259 | if ($document->getDocumentFormat() === 'METS') { |
||
| 260 | $document->setPartof($this->getParentDocumentUidForSaving($document)); |
||
| 261 | } |
||
| 262 | |||
| 263 | if ($document->getUid() === null) { |
||
| 264 | // new document |
||
| 265 | $this->documentRepository->add($document); |
||
| 266 | } else { |
||
| 267 | // update of existing document |
||
| 268 | $this->documentRepository->update($document); |
||
| 269 | } |
||
| 270 | |||
| 271 | $persistenceManager->persistAll(); |
||
| 272 | |||
| 273 | return true; |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Get the ID of the parent document if the current document has one. |
||
| 278 | * Currently only applies to METS documents. |
||
| 279 | * |
||
| 280 | * @access protected |
||
| 281 | * |
||
| 282 | * @param Document $document for which parent UID should be taken |
||
| 283 | * |
||
| 284 | * @return int The parent document's id. |
||
| 285 | */ |
||
| 286 | protected function getParentDocumentUidForSaving(Document $document): int |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Add collections. |
||
| 322 | * |
||
| 323 | * @access private |
||
| 324 | * |
||
| 325 | * @param Document &$document |
||
| 326 | * @param array $collections |
||
| 327 | * @param PersistenceManager $persistenceManager |
||
| 328 | * |
||
| 329 | * @return void |
||
| 330 | */ |
||
| 331 | private function addCollections(Document &$document, array $collections, PersistenceManager $persistenceManager): void |
||
| 350 | } |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Get authors considering that database field can't accept |
||
| 355 | * more than 255 characters. |
||
| 356 | * |
||
| 357 | * @access private |
||
| 358 | * |
||
| 359 | * @param array $metadataAuthor |
||
| 360 | * |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | private function getAuthors(array $metadataAuthor): string |
||
| 364 | { |
||
| 365 | $authors = ''; |
||
| 366 | $delimiter = '; '; |
||
| 367 | $ellipsis = 'et al.'; |
||
| 368 | |||
| 369 | $count = count($metadataAuthor); |
||
| 370 | |||
| 371 | for ($i = 0; $i < $count; $i++) { |
||
| 372 | // Build the next part to add |
||
| 373 | $nextPart = ($i === 0 ? '' : $delimiter) . $metadataAuthor[$i]; |
||
| 374 | // Check if adding this part and ellipsis in future would exceed the character limit |
||
| 375 | if (strlen($authors . $nextPart . $delimiter . $ellipsis) > 255) { |
||
| 376 | // Add ellipsis and stop adding more authors |
||
| 377 | $authors .= $delimiter . $ellipsis; |
||
| 378 | break; |
||
| 379 | } |
||
| 380 | // Add the part to the main string |
||
| 381 | $authors .= $nextPart; |
||
| 382 | } |
||
| 383 | |||
| 384 | return $authors; |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * 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. |
||
| 389 | * |
||
| 390 | * @access private |
||
| 391 | * |
||
| 392 | * @param ?string $owner |
||
| 393 | * |
||
| 394 | * @return void |
||
| 395 | */ |
||
| 396 | private function setOwner($owner): void |
||
| 408 | } |
||
| 409 | } |
||
| 412 |