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