We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 84 |
| Total Lines | 729 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
Complex classes like DocumentRepository 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 DocumentRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class DocumentRepository extends \TYPO3\CMS\Extbase\Persistence\Repository |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Find one document by given parameters |
||
| 29 | * |
||
| 30 | * GET parameters may be: |
||
| 31 | * |
||
| 32 | * - 'id': the uid of the document |
||
| 33 | * - 'location': the URL of the location of the XML file |
||
| 34 | * - 'recordId': the record_id of the document |
||
| 35 | * |
||
| 36 | * @param array $parameters |
||
| 37 | * |
||
| 38 | * @return \Kitodo\Dlf\Domain\Model\Document|null |
||
| 39 | */ |
||
| 40 | public function findOneByParameters($parameters) |
||
| 41 | { |
||
| 42 | $doc = null; |
||
| 43 | $document = null; |
||
| 44 | |||
| 45 | if (isset($parameters['id']) && MathUtility::canBeInterpretedAsInteger($parameters['id'])) { |
||
| 46 | |||
| 47 | $document = $this->findOneByIdAndSettings($parameters['id']); |
||
| 48 | |||
| 49 | } else if (isset($parameters['recordId'])) { |
||
| 50 | |||
| 51 | $document = $this->findOneByRecordId($parameters['recordId']); |
||
|
|
|||
| 52 | |||
| 53 | } else if (isset($parameters['location']) && GeneralUtility::isValidUrl($parameters['location'])) { |
||
| 54 | |||
| 55 | $doc = Doc::getInstance($parameters['location'], [], true); |
||
| 56 | |||
| 57 | if ($doc->recordId) { |
||
| 58 | $document = $this->findOneByRecordId($doc->recordId); |
||
| 59 | } |
||
| 60 | |||
| 61 | if ($document === null) { |
||
| 62 | // create new (dummy) Document object |
||
| 63 | $document = GeneralUtility::makeInstance(Document::class); |
||
| 64 | $document->setLocation($parameters['location']); |
||
| 65 | } |
||
| 66 | |||
| 67 | } |
||
| 68 | |||
| 69 | if ($document !== null && $doc === null) { |
||
| 70 | $doc = Doc::getInstance($document->getLocation(), [], true); |
||
| 71 | } |
||
| 72 | |||
| 73 | if ($doc !== null) { |
||
| 74 | $document->setDoc($doc); |
||
| 75 | } |
||
| 76 | |||
| 77 | return $document; |
||
| 78 | |||
| 79 | } |
||
| 80 | |||
| 81 | |||
| 82 | public function findByUidAndPartOf($uid, $partOf) |
||
| 83 | { |
||
| 84 | $query = $this->createQuery(); |
||
| 85 | |||
| 86 | $query->matching($query->equals('uid', $uid)); |
||
| 87 | $query->matching($query->equals('partof', $partOf)); |
||
| 88 | |||
| 89 | return $query->execute(); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Find the oldest document |
||
| 94 | * |
||
| 95 | * @return \Kitodo\Dlf\Domain\Model\Document|null |
||
| 96 | */ |
||
| 97 | public function findOldestDocument() |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param int $partOf |
||
| 109 | * @param string $structure |
||
| 110 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 111 | */ |
||
| 112 | public function getChildrenOfYearAnchor($partOf, $structure) |
||
| 113 | { |
||
| 114 | $query = $this->createQuery(); |
||
| 115 | |||
| 116 | $query->matching($query->equals('structure', Helper::getUidFromIndexName($structure, 'tx_dlf_structures'))); |
||
| 117 | $query->matching($query->equals('partof', $partOf)); |
||
| 118 | |||
| 119 | $query->setOrderings([ |
||
| 120 | 'mets_orderlabel' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING |
||
| 121 | ]); |
||
| 122 | |||
| 123 | return $query->execute(); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Finds all documents for the given settings |
||
| 128 | * |
||
| 129 | * @param int $uid |
||
| 130 | * @param array $settings |
||
| 131 | * |
||
| 132 | * @return \Kitodo\Dlf\Domain\Model\Document|null |
||
| 133 | */ |
||
| 134 | public function findOneByIdAndSettings($uid, $settings = []) |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Finds all documents for the given settings |
||
| 143 | * |
||
| 144 | * @param array $settings |
||
| 145 | * |
||
| 146 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 147 | */ |
||
| 148 | public function findDocumentsBySettings($settings = []) |
||
| 149 | { |
||
| 150 | $query = $this->createQuery(); |
||
| 151 | |||
| 152 | $constraints = []; |
||
| 153 | |||
| 154 | if ($settings['documentSets']) { |
||
| 155 | $constraints[] = $query->in('uid', GeneralUtility::intExplode(',', $settings['documentSets'])); |
||
| 156 | } |
||
| 157 | |||
| 158 | if (isset($settings['excludeOther']) && (int) $settings['excludeOther'] === 0) { |
||
| 159 | $query->getQuerySettings()->setRespectStoragePage(false); |
||
| 160 | } |
||
| 161 | |||
| 162 | if (count($constraints)) { |
||
| 163 | $query->matching( |
||
| 164 | $query->logicalAnd($constraints) |
||
| 165 | ); |
||
| 166 | } |
||
| 167 | |||
| 168 | return $query->execute(); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Finds all documents for the given collections |
||
| 173 | * |
||
| 174 | * @param array $collections |
||
| 175 | * @param int $limit |
||
| 176 | * |
||
| 177 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 178 | */ |
||
| 179 | public function findAllByCollectionsLimited($collections, $limit = 50) |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Find all the titles |
||
| 208 | * |
||
| 209 | * documents with partof == 0 |
||
| 210 | * |
||
| 211 | * @param array $settings |
||
| 212 | * |
||
| 213 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
||
| 214 | */ |
||
| 215 | public function findAllTitles($settings = []) |
||
| 216 | { |
||
| 217 | $query = $this->createQuery(); |
||
| 218 | |||
| 219 | $constraints = []; |
||
| 220 | $constraints[] = $query->equals('partof', 0); |
||
| 221 | |||
| 222 | if ($settings['collections']) { |
||
| 223 | $constraints[] = $query->in('collections.uid', GeneralUtility::intExplode(',', $settings['collections'])); |
||
| 224 | } |
||
| 225 | |||
| 226 | if (count($constraints)) { |
||
| 227 | $query->matching( |
||
| 228 | $query->logicalAnd($constraints) |
||
| 229 | ); |
||
| 230 | } |
||
| 231 | |||
| 232 | return $query->execute(); |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Count the titles |
||
| 237 | * |
||
| 238 | * documents with partof == 0 |
||
| 239 | * |
||
| 240 | * @param array $settings |
||
| 241 | * |
||
| 242 | * @return int |
||
| 243 | */ |
||
| 244 | public function countAllTitles($settings = []) |
||
| 245 | { |
||
| 246 | return $this->findAllTitles($settings)->count(); |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Count the volumes |
||
| 251 | * |
||
| 252 | * documents with partof != 0 |
||
| 253 | * |
||
| 254 | * @param array $settings |
||
| 255 | * |
||
| 256 | * @return int |
||
| 257 | */ |
||
| 258 | public function countAllVolumes($settings = []) |
||
| 259 | { |
||
| 260 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 261 | ->getQueryBuilderForTable('tx_dlf_documents'); |
||
| 262 | $subQueryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 263 | ->getQueryBuilderForTable('tx_dlf_documents'); |
||
| 264 | |||
| 265 | $subQuery = $subQueryBuilder |
||
| 266 | ->select('tx_dlf_documents.partof') |
||
| 267 | ->from('tx_dlf_documents') |
||
| 268 | ->where( |
||
| 269 | $subQueryBuilder->expr()->neq('tx_dlf_documents.partof', 0) |
||
| 270 | ) |
||
| 271 | ->groupBy('tx_dlf_documents.partof') |
||
| 272 | ->getSQL(); |
||
| 273 | |||
| 274 | $countVolumes = $queryBuilder |
||
| 275 | ->count('tx_dlf_documents.uid') |
||
| 276 | ->from('tx_dlf_documents') |
||
| 277 | ->where( |
||
| 278 | $queryBuilder->expr()->eq('tx_dlf_documents.pid', intval($settings['pages'])), |
||
| 279 | $queryBuilder->expr()->notIn('tx_dlf_documents.uid', $subQuery) |
||
| 280 | ) |
||
| 281 | ->execute() |
||
| 282 | ->fetchColumn(0); |
||
| 283 | |||
| 284 | return $countVolumes; |
||
| 285 | } |
||
| 286 | |||
| 287 | public function getStatisticsForSelectedCollection($settings) |
||
| 288 | { |
||
| 289 | // Include only selected collections. |
||
| 290 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 291 | ->getQueryBuilderForTable('tx_dlf_documents'); |
||
| 292 | |||
| 293 | $countTitles = $queryBuilder |
||
| 294 | ->count('tx_dlf_documents.uid') |
||
| 295 | ->from('tx_dlf_documents') |
||
| 296 | ->innerJoin( |
||
| 297 | 'tx_dlf_documents', |
||
| 298 | 'tx_dlf_relations', |
||
| 299 | 'tx_dlf_relations_joins', |
||
| 300 | $queryBuilder->expr()->eq( |
||
| 301 | 'tx_dlf_relations_joins.uid_local', |
||
| 302 | 'tx_dlf_documents.uid' |
||
| 303 | ) |
||
| 304 | ) |
||
| 305 | ->innerJoin( |
||
| 306 | 'tx_dlf_relations_joins', |
||
| 307 | 'tx_dlf_collections', |
||
| 308 | 'tx_dlf_collections_join', |
||
| 309 | $queryBuilder->expr()->eq( |
||
| 310 | 'tx_dlf_relations_joins.uid_foreign', |
||
| 311 | 'tx_dlf_collections_join.uid' |
||
| 312 | ) |
||
| 313 | ) |
||
| 314 | ->where( |
||
| 315 | $queryBuilder->expr()->eq('tx_dlf_documents.pid', intval($settings['pages'])), |
||
| 316 | $queryBuilder->expr()->eq('tx_dlf_collections_join.pid', intval($settings['pages'])), |
||
| 317 | $queryBuilder->expr()->eq('tx_dlf_documents.partof', 0), |
||
| 318 | $queryBuilder->expr()->in('tx_dlf_collections_join.uid', |
||
| 319 | $queryBuilder->createNamedParameter(GeneralUtility::intExplode(',', |
||
| 320 | $settings['collections']), Connection::PARAM_INT_ARRAY)), |
||
| 321 | $queryBuilder->expr()->eq('tx_dlf_relations_joins.ident', |
||
| 322 | $queryBuilder->createNamedParameter('docs_colls')) |
||
| 323 | ) |
||
| 324 | ->execute() |
||
| 325 | ->fetchColumn(0); |
||
| 326 | |||
| 327 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 328 | ->getQueryBuilderForTable('tx_dlf_documents'); |
||
| 329 | $subQueryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 330 | ->getQueryBuilderForTable('tx_dlf_documents'); |
||
| 331 | |||
| 332 | $subQuery = $subQueryBuilder |
||
| 333 | ->select('tx_dlf_documents.partof') |
||
| 334 | ->from('tx_dlf_documents') |
||
| 335 | ->where( |
||
| 336 | $subQueryBuilder->expr()->neq('tx_dlf_documents.partof', 0) |
||
| 337 | ) |
||
| 338 | ->groupBy('tx_dlf_documents.partof') |
||
| 339 | ->getSQL(); |
||
| 340 | |||
| 341 | $countVolumes = $queryBuilder |
||
| 342 | ->count('tx_dlf_documents.uid') |
||
| 343 | ->from('tx_dlf_documents') |
||
| 344 | ->innerJoin( |
||
| 345 | 'tx_dlf_documents', |
||
| 346 | 'tx_dlf_relations', |
||
| 347 | 'tx_dlf_relations_joins', |
||
| 348 | $queryBuilder->expr()->eq( |
||
| 349 | 'tx_dlf_relations_joins.uid_local', |
||
| 350 | 'tx_dlf_documents.uid' |
||
| 351 | ) |
||
| 352 | ) |
||
| 353 | ->innerJoin( |
||
| 354 | 'tx_dlf_relations_joins', |
||
| 355 | 'tx_dlf_collections', |
||
| 356 | 'tx_dlf_collections_join', |
||
| 357 | $queryBuilder->expr()->eq( |
||
| 358 | 'tx_dlf_relations_joins.uid_foreign', |
||
| 359 | 'tx_dlf_collections_join.uid' |
||
| 360 | ) |
||
| 361 | ) |
||
| 362 | ->where( |
||
| 363 | $queryBuilder->expr()->eq('tx_dlf_documents.pid', intval($settings['pages'])), |
||
| 364 | $queryBuilder->expr()->eq('tx_dlf_collections_join.pid', intval($settings['pages'])), |
||
| 365 | $queryBuilder->expr()->notIn('tx_dlf_documents.uid', $subQuery), |
||
| 366 | $queryBuilder->expr()->in('tx_dlf_collections_join.uid', |
||
| 367 | $queryBuilder->createNamedParameter(GeneralUtility::intExplode(',', |
||
| 368 | $settings['collections']), Connection::PARAM_INT_ARRAY)), |
||
| 369 | $queryBuilder->expr()->eq('tx_dlf_relations_joins.ident', |
||
| 370 | $queryBuilder->createNamedParameter('docs_colls')) |
||
| 371 | ) |
||
| 372 | ->execute() |
||
| 373 | ->fetchColumn(0); |
||
| 374 | |||
| 375 | return ['titles' => $countTitles, 'volumes' => $countVolumes]; |
||
| 376 | } |
||
| 377 | |||
| 378 | public function getTableOfContentsFromDb($uid, $pid, $settings) |
||
| 379 | { |
||
| 380 | // Build table of contents from database. |
||
| 381 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 382 | ->getQueryBuilderForTable('tx_dlf_documents'); |
||
| 383 | |||
| 384 | $excludeOtherWhere = ''; |
||
| 385 | if ($settings['excludeOther']) { |
||
| 386 | $excludeOtherWhere = 'tx_dlf_documents.pid=' . intval($settings['pages']); |
||
| 387 | } |
||
| 388 | // Check if there are any metadata to suggest. |
||
| 389 | $result = $queryBuilder |
||
| 390 | ->select( |
||
| 391 | 'tx_dlf_documents.uid AS uid', |
||
| 392 | 'tx_dlf_documents.title AS title', |
||
| 393 | 'tx_dlf_documents.volume AS volume', |
||
| 394 | 'tx_dlf_documents.mets_label AS mets_label', |
||
| 395 | 'tx_dlf_documents.mets_orderlabel AS mets_orderlabel', |
||
| 396 | 'tx_dlf_structures_join.index_name AS type' |
||
| 397 | ) |
||
| 398 | ->innerJoin( |
||
| 399 | 'tx_dlf_documents', |
||
| 400 | 'tx_dlf_structures', |
||
| 401 | 'tx_dlf_structures_join', |
||
| 402 | $queryBuilder->expr()->eq( |
||
| 403 | 'tx_dlf_structures_join.uid', |
||
| 404 | 'tx_dlf_documents.structure' |
||
| 405 | ) |
||
| 406 | ) |
||
| 407 | ->from('tx_dlf_documents') |
||
| 408 | ->where( |
||
| 409 | $queryBuilder->expr()->eq('tx_dlf_documents.partof', intval($uid)), |
||
| 410 | $queryBuilder->expr()->eq('tx_dlf_structures_join.pid', intval($pid)), |
||
| 411 | $excludeOtherWhere |
||
| 412 | ) |
||
| 413 | ->addOrderBy('tx_dlf_documents.volume_sorting') |
||
| 414 | ->addOrderBy('tx_dlf_documents.mets_orderlabel') |
||
| 415 | ->execute(); |
||
| 416 | return $result; |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Find one document by given settings and identifier |
||
| 421 | * |
||
| 422 | * @param array $settings |
||
| 423 | * @param array $parameters |
||
| 424 | * |
||
| 425 | * @return array The found document object |
||
| 426 | */ |
||
| 427 | public function getOaiRecord($settings, $parameters) |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Finds all documents for the given settings |
||
| 462 | * |
||
| 463 | * @param array $settings |
||
| 464 | * @param array $documentsToProcess |
||
| 465 | * |
||
| 466 | * @return array The found document objects |
||
| 467 | */ |
||
| 468 | public function getOaiDocumentList($settings, $documentsToProcess) |
||
| 469 | { |
||
| 470 | $connection = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 471 | ->getConnectionForTable('tx_dlf_documents'); |
||
| 472 | |||
| 473 | $sql = 'SELECT `tx_dlf_documents`.*, GROUP_CONCAT(DISTINCT `tx_dlf_collections`.`oai_name` ORDER BY `tx_dlf_collections`.`oai_name` SEPARATOR " ") AS `collections` ' . |
||
| 474 | 'FROM `tx_dlf_documents` ' . |
||
| 475 | 'INNER JOIN `tx_dlf_relations` ON `tx_dlf_relations`.`uid_local` = `tx_dlf_documents`.`uid` ' . |
||
| 476 | 'INNER JOIN `tx_dlf_collections` ON `tx_dlf_collections`.`uid` = `tx_dlf_relations`.`uid_foreign` ' . |
||
| 477 | 'WHERE `tx_dlf_documents`.`uid` IN ( ? ) ' . |
||
| 478 | 'AND `tx_dlf_relations`.`ident`="docs_colls" ' . |
||
| 479 | 'AND ' . Helper::whereExpression('tx_dlf_collections') . ' ' . |
||
| 480 | 'GROUP BY `tx_dlf_documents`.`uid` '; |
||
| 481 | |||
| 482 | $values = [ |
||
| 483 | $documentsToProcess, |
||
| 484 | ]; |
||
| 485 | |||
| 486 | $types = [ |
||
| 487 | Connection::PARAM_INT_ARRAY, |
||
| 488 | ]; |
||
| 489 | |||
| 490 | // Create a prepared statement for the passed SQL query, bind the given params with their binding types and execute the query |
||
| 491 | $documents = $connection->executeQuery($sql, $values, $types); |
||
| 492 | |||
| 493 | return $documents; |
||
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Finds all documents with given uids |
||
| 498 | * |
||
| 499 | * @param string $uids separated by comma |
||
| 500 | * |
||
| 501 | * @return objects |
||
| 502 | */ |
||
| 503 | private function findAllByUids($uids) |
||
| 537 | } |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Find all documents with given collection from Solr |
||
| 541 | * |
||
| 542 | * @param \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult $collections |
||
| 543 | * @param array $settings |
||
| 544 | * @param array $searchParams |
||
| 545 | * @param \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult $listedMetadata |
||
| 546 | * @return array |
||
| 547 | */ |
||
| 548 | public function findSolrByCollection($collections, $settings, $searchParams, $listedMetadata = null) { |
||
| 696 | } |
||
| 697 | |||
| 698 | /** |
||
| 699 | * Find all listed metadata for given document |
||
| 700 | * |
||
| 701 | * @param int $uid the uid of the document |
||
| 702 | * @param array $settings |
||
| 703 | * @param \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult $listedMetadata |
||
| 704 | * @return array |
||
| 705 | */ |
||
| 706 | protected function fetchMetadataFromSolr($uid, $settings, $listedMetadata = []) { |
||
| 756 |