We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 111 | 
| Total Lines | 891 | 
| Duplicated Lines | 0 % | 
| Changes | 9 | ||
| 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 | ||
| 28 | class DocumentRepository extends \TYPO3\CMS\Extbase\Persistence\Repository | ||
| 29 | { | ||
| 30 | /** | ||
| 31 | * The controller settings passed to the repository for some special actions. | ||
| 32 | * | ||
| 33 | * @var array | ||
| 34 | * @access protected | ||
| 35 | */ | ||
| 36 | protected $settings; | ||
| 37 | |||
| 38 | /** | ||
| 39 | * Find one document by given parameters | ||
| 40 | * | ||
| 41 | * GET parameters may be: | ||
| 42 | * | ||
| 43 | * - 'id': the uid of the document | ||
| 44 | * - 'location': the URL of the location of the XML file | ||
| 45 | * - 'recordId': the record_id of the document | ||
| 46 | * | ||
| 47 | * @param array $parameters | ||
| 48 | * | ||
| 49 | * @return \Kitodo\Dlf\Domain\Model\Document|null | ||
| 50 | */ | ||
| 51 | public function findOneByParameters($parameters) | ||
| 52 |     { | ||
| 53 | $doc = null; | ||
| 54 | $document = null; | ||
| 55 | |||
| 56 |         if (isset($parameters['id']) && MathUtility::canBeInterpretedAsInteger($parameters['id'])) { | ||
| 57 | |||
| 58 | $document = $this->findOneByIdAndSettings($parameters['id']); | ||
| 59 | |||
| 60 |         } else if (isset($parameters['recordId'])) { | ||
| 61 | |||
| 62 | $document = $this->findOneByRecordId($parameters['recordId']); | ||
|  | |||
| 63 | |||
| 64 |         } else if (isset($parameters['location']) && GeneralUtility::isValidUrl($parameters['location'])) { | ||
| 65 | |||
| 66 | $doc = Doc::getInstance($parameters['location'], [], true); | ||
| 67 | |||
| 68 |             if ($doc->recordId) { | ||
| 69 | $document = $this->findOneByRecordId($doc->recordId); | ||
| 70 | } | ||
| 71 | |||
| 72 |             if ($document === null) { | ||
| 73 | // create new (dummy) Document object | ||
| 74 | $document = GeneralUtility::makeInstance(Document::class); | ||
| 75 | $document->setLocation($parameters['location']); | ||
| 76 | } | ||
| 77 | |||
| 78 | } | ||
| 79 | |||
| 80 |         if ($document !== null && $doc === null) { | ||
| 81 | $doc = Doc::getInstance($document->getLocation(), [], true); | ||
| 82 | } | ||
| 83 | |||
| 84 |         if ($doc !== null) { | ||
| 85 | $document->setDoc($doc); | ||
| 86 | } | ||
| 87 | |||
| 88 | return $document; | ||
| 89 | |||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | public function findByUidAndPartOf($uid, $partOf) | ||
| 94 |     { | ||
| 95 | $query = $this->createQuery(); | ||
| 96 | |||
| 97 |         $query->matching($query->equals('uid', $uid)); | ||
| 98 |         $query->matching($query->equals('partof', $partOf)); | ||
| 99 | |||
| 100 | return $query->execute(); | ||
| 101 | } | ||
| 102 | |||
| 103 | /** | ||
| 104 | * Find the oldest document | ||
| 105 | * | ||
| 106 | * @return \Kitodo\Dlf\Domain\Model\Document|null | ||
| 107 | */ | ||
| 108 | public function findOldestDocument() | ||
| 116 | } | ||
| 117 | |||
| 118 | /** | ||
| 119 | * @param int $partOf | ||
| 120 | * @param string $structure | ||
| 121 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface | ||
| 122 | */ | ||
| 123 | public function getChildrenOfYearAnchor($partOf, $structure) | ||
| 124 |     { | ||
| 125 | $query = $this->createQuery(); | ||
| 126 | |||
| 127 |         $query->matching($query->equals('structure', Helper::getUidFromIndexName($structure, 'tx_dlf_structures'))); | ||
| 128 |         $query->matching($query->equals('partof', $partOf)); | ||
| 129 | |||
| 130 | $query->setOrderings([ | ||
| 131 | 'mets_orderlabel' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING | ||
| 132 | ]); | ||
| 133 | |||
| 134 | return $query->execute(); | ||
| 135 | } | ||
| 136 | |||
| 137 | /** | ||
| 138 | * Finds all documents for the given settings | ||
| 139 | * | ||
| 140 | * @param int $uid | ||
| 141 | * @param array $settings | ||
| 142 | * | ||
| 143 | * @return \Kitodo\Dlf\Domain\Model\Document|null | ||
| 144 | */ | ||
| 145 | public function findOneByIdAndSettings($uid, $settings = []) | ||
| 150 | } | ||
| 151 | |||
| 152 | /** | ||
| 153 | * Finds all documents for the given settings | ||
| 154 | * | ||
| 155 | * @param array $settings | ||
| 156 | * | ||
| 157 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface | ||
| 158 | */ | ||
| 159 | public function findDocumentsBySettings($settings = []) | ||
| 160 |     { | ||
| 161 | $query = $this->createQuery(); | ||
| 162 | |||
| 163 | $constraints = []; | ||
| 164 | |||
| 165 |         if ($settings['documentSets']) { | ||
| 166 |             $constraints[] = $query->in('uid', GeneralUtility::intExplode(',', $settings['documentSets'])); | ||
| 167 | } | ||
| 168 | |||
| 169 |         if (isset($settings['excludeOther']) && (int) $settings['excludeOther'] === 0) { | ||
| 170 | $query->getQuerySettings()->setRespectStoragePage(false); | ||
| 171 | } | ||
| 172 | |||
| 173 |         if (count($constraints)) { | ||
| 174 | $query->matching( | ||
| 175 | $query->logicalAnd($constraints) | ||
| 176 | ); | ||
| 177 | } | ||
| 178 | |||
| 179 | return $query->execute(); | ||
| 180 | } | ||
| 181 | |||
| 182 | /** | ||
| 183 | * Finds all documents for the given collections | ||
| 184 | * | ||
| 185 | * @param array $collections | ||
| 186 | * @param int $limit | ||
| 187 | * | ||
| 188 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface | ||
| 189 | */ | ||
| 190 | public function findAllByCollectionsLimited($collections, $limit = 50) | ||
| 215 | } | ||
| 216 | |||
| 217 | /** | ||
| 218 | * Find all the titles | ||
| 219 | * | ||
| 220 | * documents with partof == 0 | ||
| 221 | * | ||
| 222 | * @param array $settings | ||
| 223 | * | ||
| 224 | * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface | ||
| 225 | */ | ||
| 226 | public function findAllTitles($settings = []) | ||
| 227 |     { | ||
| 228 | $query = $this->createQuery(); | ||
| 229 | |||
| 230 | $constraints = []; | ||
| 231 |         $constraints[] = $query->equals('partof', 0); | ||
| 232 | |||
| 233 |         if ($settings['collections']) { | ||
| 234 |             $constraints[] = $query->in('collections.uid', GeneralUtility::intExplode(',', $settings['collections'])); | ||
| 235 | } | ||
| 236 | |||
| 237 |         if (count($constraints)) { | ||
| 238 | $query->matching( | ||
| 239 | $query->logicalAnd($constraints) | ||
| 240 | ); | ||
| 241 | } | ||
| 242 | |||
| 243 | return $query->execute(); | ||
| 244 | } | ||
| 245 | |||
| 246 | /** | ||
| 247 | * Count the titles | ||
| 248 | * | ||
| 249 | * documents with partof == 0 | ||
| 250 | * | ||
| 251 | * @param array $settings | ||
| 252 | * | ||
| 253 | * @return int | ||
| 254 | */ | ||
| 255 | public function countAllTitles($settings = []) | ||
| 256 |     { | ||
| 257 | return $this->findAllTitles($settings)->count(); | ||
| 258 | } | ||
| 259 | |||
| 260 | /** | ||
| 261 | * Count the volumes | ||
| 262 | * | ||
| 263 | * documents with partof != 0 | ||
| 264 | * | ||
| 265 | * @param array $settings | ||
| 266 | * | ||
| 267 | * @return int | ||
| 268 | */ | ||
| 269 | public function countAllVolumes($settings = []) | ||
| 270 |     { | ||
| 271 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) | ||
| 272 |             ->getQueryBuilderForTable('tx_dlf_documents'); | ||
| 273 | $subQueryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) | ||
| 274 |             ->getQueryBuilderForTable('tx_dlf_documents'); | ||
| 275 | |||
| 276 | $subQuery = $subQueryBuilder | ||
| 277 |             ->select('tx_dlf_documents.partof') | ||
| 278 |             ->from('tx_dlf_documents') | ||
| 279 | ->where( | ||
| 280 |                 $subQueryBuilder->expr()->neq('tx_dlf_documents.partof', 0) | ||
| 281 | ) | ||
| 282 |             ->groupBy('tx_dlf_documents.partof') | ||
| 283 | ->getSQL(); | ||
| 284 | |||
| 285 | $countVolumes = $queryBuilder | ||
| 286 |             ->count('tx_dlf_documents.uid') | ||
| 287 |             ->from('tx_dlf_documents') | ||
| 288 | ->where( | ||
| 289 |                 $queryBuilder->expr()->eq('tx_dlf_documents.pid', intval($settings['pages'])), | ||
| 290 |                 $queryBuilder->expr()->notIn('tx_dlf_documents.uid', $subQuery) | ||
| 291 | ) | ||
| 292 | ->execute() | ||
| 293 | ->fetchColumn(0); | ||
| 294 | |||
| 295 | return $countVolumes; | ||
| 296 | } | ||
| 297 | |||
| 298 | public function getStatisticsForSelectedCollection($settings) | ||
| 299 |     { | ||
| 300 | // Include only selected collections. | ||
| 301 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) | ||
| 302 |             ->getQueryBuilderForTable('tx_dlf_documents'); | ||
| 303 | |||
| 304 | $countTitles = $queryBuilder | ||
| 305 |             ->count('tx_dlf_documents.uid') | ||
| 306 |             ->from('tx_dlf_documents') | ||
| 307 | ->innerJoin( | ||
| 308 | 'tx_dlf_documents', | ||
| 309 | 'tx_dlf_relations', | ||
| 310 | 'tx_dlf_relations_joins', | ||
| 311 | $queryBuilder->expr()->eq( | ||
| 312 | 'tx_dlf_relations_joins.uid_local', | ||
| 313 | 'tx_dlf_documents.uid' | ||
| 314 | ) | ||
| 315 | ) | ||
| 316 | ->innerJoin( | ||
| 317 | 'tx_dlf_relations_joins', | ||
| 318 | 'tx_dlf_collections', | ||
| 319 | 'tx_dlf_collections_join', | ||
| 320 | $queryBuilder->expr()->eq( | ||
| 321 | 'tx_dlf_relations_joins.uid_foreign', | ||
| 322 | 'tx_dlf_collections_join.uid' | ||
| 323 | ) | ||
| 324 | ) | ||
| 325 | ->where( | ||
| 326 |                 $queryBuilder->expr()->eq('tx_dlf_documents.pid', intval($settings['pages'])), | ||
| 327 |                 $queryBuilder->expr()->eq('tx_dlf_collections_join.pid', intval($settings['pages'])), | ||
| 328 |                 $queryBuilder->expr()->eq('tx_dlf_documents.partof', 0), | ||
| 329 |                 $queryBuilder->expr()->in('tx_dlf_collections_join.uid', | ||
| 330 |                     $queryBuilder->createNamedParameter(GeneralUtility::intExplode(',', | ||
| 331 | $settings['collections']), Connection::PARAM_INT_ARRAY)), | ||
| 332 |                 $queryBuilder->expr()->eq('tx_dlf_relations_joins.ident', | ||
| 333 |                     $queryBuilder->createNamedParameter('docs_colls')) | ||
| 334 | ) | ||
| 335 | ->execute() | ||
| 336 | ->fetchColumn(0); | ||
| 337 | |||
| 338 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) | ||
| 339 |             ->getQueryBuilderForTable('tx_dlf_documents'); | ||
| 340 | $subQueryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) | ||
| 341 |             ->getQueryBuilderForTable('tx_dlf_documents'); | ||
| 342 | |||
| 343 | $subQuery = $subQueryBuilder | ||
| 344 |             ->select('tx_dlf_documents.partof') | ||
| 345 |             ->from('tx_dlf_documents') | ||
| 346 | ->where( | ||
| 347 |                 $subQueryBuilder->expr()->neq('tx_dlf_documents.partof', 0) | ||
| 348 | ) | ||
| 349 |             ->groupBy('tx_dlf_documents.partof') | ||
| 350 | ->getSQL(); | ||
| 351 | |||
| 352 | $countVolumes = $queryBuilder | ||
| 353 |             ->count('tx_dlf_documents.uid') | ||
| 354 |             ->from('tx_dlf_documents') | ||
| 355 | ->innerJoin( | ||
| 356 | 'tx_dlf_documents', | ||
| 357 | 'tx_dlf_relations', | ||
| 358 | 'tx_dlf_relations_joins', | ||
| 359 | $queryBuilder->expr()->eq( | ||
| 360 | 'tx_dlf_relations_joins.uid_local', | ||
| 361 | 'tx_dlf_documents.uid' | ||
| 362 | ) | ||
| 363 | ) | ||
| 364 | ->innerJoin( | ||
| 365 | 'tx_dlf_relations_joins', | ||
| 366 | 'tx_dlf_collections', | ||
| 367 | 'tx_dlf_collections_join', | ||
| 368 | $queryBuilder->expr()->eq( | ||
| 369 | 'tx_dlf_relations_joins.uid_foreign', | ||
| 370 | 'tx_dlf_collections_join.uid' | ||
| 371 | ) | ||
| 372 | ) | ||
| 373 | ->where( | ||
| 374 |                 $queryBuilder->expr()->eq('tx_dlf_documents.pid', intval($settings['pages'])), | ||
| 375 |                 $queryBuilder->expr()->eq('tx_dlf_collections_join.pid', intval($settings['pages'])), | ||
| 376 |                 $queryBuilder->expr()->notIn('tx_dlf_documents.uid', $subQuery), | ||
| 377 |                 $queryBuilder->expr()->in('tx_dlf_collections_join.uid', | ||
| 378 |                     $queryBuilder->createNamedParameter(GeneralUtility::intExplode(',', | ||
| 379 | $settings['collections']), Connection::PARAM_INT_ARRAY)), | ||
| 380 |                 $queryBuilder->expr()->eq('tx_dlf_relations_joins.ident', | ||
| 381 |                     $queryBuilder->createNamedParameter('docs_colls')) | ||
| 382 | ) | ||
| 383 | ->execute() | ||
| 384 | ->fetchColumn(0); | ||
| 385 | |||
| 386 | return ['titles' => $countTitles, 'volumes' => $countVolumes]; | ||
| 387 | } | ||
| 388 | |||
| 389 | public function getTableOfContentsFromDb($uid, $pid, $settings) | ||
| 390 |     { | ||
| 391 | // Build table of contents from database. | ||
| 392 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) | ||
| 393 |             ->getQueryBuilderForTable('tx_dlf_documents'); | ||
| 394 | |||
| 395 | $excludeOtherWhere = ''; | ||
| 396 |         if ($settings['excludeOther']) { | ||
| 397 | $excludeOtherWhere = 'tx_dlf_documents.pid=' . intval($settings['pages']); | ||
| 398 | } | ||
| 399 | // Check if there are any metadata to suggest. | ||
| 400 | $result = $queryBuilder | ||
| 401 | ->select( | ||
| 402 | 'tx_dlf_documents.uid AS uid', | ||
| 403 | 'tx_dlf_documents.title AS title', | ||
| 404 | 'tx_dlf_documents.volume AS volume', | ||
| 405 | 'tx_dlf_documents.mets_label AS mets_label', | ||
| 406 | 'tx_dlf_documents.mets_orderlabel AS mets_orderlabel', | ||
| 407 | 'tx_dlf_structures_join.index_name AS type' | ||
| 408 | ) | ||
| 409 | ->innerJoin( | ||
| 410 | 'tx_dlf_documents', | ||
| 411 | 'tx_dlf_structures', | ||
| 412 | 'tx_dlf_structures_join', | ||
| 413 | $queryBuilder->expr()->eq( | ||
| 414 | 'tx_dlf_structures_join.uid', | ||
| 415 | 'tx_dlf_documents.structure' | ||
| 416 | ) | ||
| 417 | ) | ||
| 418 |             ->from('tx_dlf_documents') | ||
| 419 | ->where( | ||
| 420 |                 $queryBuilder->expr()->eq('tx_dlf_documents.partof', intval($uid)), | ||
| 421 |                 $queryBuilder->expr()->eq('tx_dlf_structures_join.pid', intval($pid)), | ||
| 422 | $excludeOtherWhere | ||
| 423 | ) | ||
| 424 |             ->addOrderBy('tx_dlf_documents.volume_sorting') | ||
| 425 |             ->addOrderBy('tx_dlf_documents.mets_orderlabel') | ||
| 426 | ->execute(); | ||
| 427 | return $result; | ||
| 428 | } | ||
| 429 | |||
| 430 | /** | ||
| 431 | * Find one document by given settings and identifier | ||
| 432 | * | ||
| 433 | * @param array $settings | ||
| 434 | * @param array $parameters | ||
| 435 | * | ||
| 436 | * @return array The found document object | ||
| 437 | */ | ||
| 438 | public function getOaiRecord($settings, $parameters) | ||
| 469 | } | ||
| 470 | |||
| 471 | /** | ||
| 472 | * Finds all documents for the given settings | ||
| 473 | * | ||
| 474 | * @param array $settings | ||
| 475 | * @param array $documentsToProcess | ||
| 476 | * | ||
| 477 | * @return array The found document objects | ||
| 478 | */ | ||
| 479 | public function getOaiDocumentList($settings, $documentsToProcess) | ||
| 480 |     { | ||
| 481 | $connection = GeneralUtility::makeInstance(ConnectionPool::class) | ||
| 482 |             ->getConnectionForTable('tx_dlf_documents'); | ||
| 483 | |||
| 484 | $sql = 'SELECT `tx_dlf_documents`.*, GROUP_CONCAT(DISTINCT `tx_dlf_collections`.`oai_name` ORDER BY `tx_dlf_collections`.`oai_name` SEPARATOR " ") AS `collections` ' . | ||
| 485 | 'FROM `tx_dlf_documents` ' . | ||
| 486 | 'INNER JOIN `tx_dlf_relations` ON `tx_dlf_relations`.`uid_local` = `tx_dlf_documents`.`uid` ' . | ||
| 487 | 'INNER JOIN `tx_dlf_collections` ON `tx_dlf_collections`.`uid` = `tx_dlf_relations`.`uid_foreign` ' . | ||
| 488 | 'WHERE `tx_dlf_documents`.`uid` IN ( ? ) ' . | ||
| 489 | 'AND `tx_dlf_relations`.`ident`="docs_colls" ' . | ||
| 490 |             'AND ' . Helper::whereExpression('tx_dlf_collections') . ' ' . | ||
| 491 | 'GROUP BY `tx_dlf_documents`.`uid` '; | ||
| 492 | |||
| 493 | $values = [ | ||
| 494 | $documentsToProcess, | ||
| 495 | ]; | ||
| 496 | |||
| 497 | $types = [ | ||
| 498 | Connection::PARAM_INT_ARRAY, | ||
| 499 | ]; | ||
| 500 | |||
| 501 | // Create a prepared statement for the passed SQL query, bind the given params with their binding types and execute the query | ||
| 502 | $documents = $connection->executeQuery($sql, $values, $types); | ||
| 503 | |||
| 504 | return $documents; | ||
| 505 | } | ||
| 506 | |||
| 507 | /** | ||
| 508 | * Finds all documents with given uids | ||
| 509 | * | ||
| 510 | * @param array $uids | ||
| 511 | * | ||
| 512 | * @return array | ||
| 513 | */ | ||
| 514 | private function findAllByUids($uids) | ||
| 548 | } | ||
| 549 | |||
| 550 | /** | ||
| 551 | * Find all documents with given collection from Solr | ||
| 552 | * | ||
| 553 | * @param \Kitodo\Dlf\Domain\Model\Collection $collection | ||
| 554 | * @param array $settings | ||
| 555 | * @param array $searchParams | ||
| 556 | * @param \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult $listedMetadata | ||
| 557 | * @return array | ||
| 558 | */ | ||
| 559 | public function findSolrByCollection($collection, $settings, $searchParams, $listedMetadata = null) | ||
| 766 | } | ||
| 767 | |||
| 768 | /** | ||
| 769 | * Find all listed metadata for given document | ||
| 770 | * | ||
| 771 | * @param int $uid the uid of the document | ||
| 772 | * @param \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult $listedMetadata | ||
| 773 | * @return array | ||
| 774 | */ | ||
| 775 | public function fetchMetadataFromSolr($uid, $listedMetadata = []) | ||
| 813 | } | ||
| 814 | |||
| 815 | /** | ||
| 816 | * Processes a search request | ||
| 817 | * | ||
| 818 | * @access public | ||
| 819 | * | ||
| 820 | * @param array $parameters: Additional search parameters | ||
| 821 | * @param boolean $enableCache: Enable caching of Solr requests | ||
| 822 | * | ||
| 823 | * @return array The Apache Solr Documents that were fetched | ||
| 824 | */ | ||
| 825 | protected function searchSolr($parameters = [], $enableCache = true) | ||
| 922 |