We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 95 |
| Total Lines | 418 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like MetadataController 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 MetadataController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class MetadataController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController |
||
| 32 | { |
||
| 33 | public $prefixId = 'tx_dlf'; |
||
| 34 | public $extKey = 'dlf'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var ConfigurationManager |
||
| 38 | */ |
||
| 39 | protected $configurationManager; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var \TYPO3\CMS\Core\Log\LogManager |
||
| 43 | */ |
||
| 44 | protected $logger; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * SearchController constructor. |
||
| 48 | * @param $configurationManager |
||
| 49 | */ |
||
| 50 | public function __construct(ConfigurationManager $configurationManager) |
||
| 51 | { |
||
| 52 | $this->configurationManager = $configurationManager; |
||
| 53 | $this->logger = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Log\LogManager')->getLogger(__CLASS__); |
||
| 54 | } |
||
| 55 | |||
| 56 | // TODO: Needs to be placed in an abstract class |
||
| 57 | /** |
||
| 58 | * Loads the current document into $this->doc |
||
| 59 | * |
||
| 60 | * @access protected |
||
| 61 | * |
||
| 62 | * @return void |
||
| 63 | */ |
||
| 64 | protected function loadDocument($requestData) |
||
| 65 | { |
||
| 66 | // Check for required variable. |
||
| 67 | if ( |
||
| 68 | !empty($requestData['id']) |
||
| 69 | && !empty($this->settings['pages']) |
||
| 70 | ) { |
||
| 71 | // Should we exclude documents from other pages than $this->settings['pages']? |
||
| 72 | $pid = (!empty($this->settings['excludeOther']) ? intval($this->settings['pages']) : 0); |
||
| 73 | // Get instance of \Kitodo\Dlf\Common\Document. |
||
| 74 | $this->doc = Document::getInstance($requestData['id'], $pid); |
||
| 75 | if (!$this->doc->ready) { |
||
| 76 | // Destroy the incomplete object. |
||
| 77 | $this->doc = null; |
||
| 78 | $this->logger->error('Failed to load document with UID ' . $requestData['id']); |
||
|
1 ignored issue
–
show
|
|||
| 79 | } else { |
||
| 80 | // Set configuration PID. |
||
| 81 | $this->doc->cPid = $this->settings['pages']; |
||
| 82 | } |
||
| 83 | } elseif (!empty($requestData['recordId'])) { |
||
| 84 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 85 | ->getQueryBuilderForTable('tx_dlf_documents'); |
||
| 86 | |||
| 87 | // Get UID of document with given record identifier. |
||
| 88 | $result = $queryBuilder |
||
| 89 | ->select('tx_dlf_documents.uid AS uid') |
||
| 90 | ->from('tx_dlf_documents') |
||
| 91 | ->where( |
||
| 92 | $queryBuilder->expr()->eq('tx_dlf_documents.record_id', $queryBuilder->expr()->literal($requestData['recordId'])), |
||
| 93 | Helper::whereExpression('tx_dlf_documents') |
||
| 94 | ) |
||
| 95 | ->setMaxResults(1) |
||
| 96 | ->execute(); |
||
| 97 | |||
| 98 | if ($resArray = $result->fetch()) { |
||
| 99 | $requestData['id'] = $resArray['uid']; |
||
| 100 | // Set superglobal $_GET array and unset variables to avoid infinite looping. |
||
| 101 | $_GET[$this->prefixId]['id'] = $requestData['id']; |
||
| 102 | unset($requestData['recordId'], $_GET[$this->prefixId]['recordId']); |
||
| 103 | // Try to load document. |
||
| 104 | $this->loadDocument(); |
||
| 105 | } else { |
||
| 106 | $this->logger->error('Failed to load document with record ID "' . $requestData['recordId'] . '"'); |
||
| 107 | } |
||
| 108 | } else { |
||
| 109 | $this->logger->error('Invalid UID ' . $requestData['id'] . ' or PID ' . $this->settings['pages'] . ' for document loading'); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @return string|void |
||
| 115 | */ |
||
| 116 | public function mainAction() |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Prepares the metadata array for output |
||
| 215 | * |
||
| 216 | * @access protected |
||
| 217 | * |
||
| 218 | * @param array $metadataArray: The metadata array |
||
| 219 | * @param bool $useOriginalIiifManifestMetadata: Output IIIF metadata as simple key/value pairs? |
||
| 220 | * |
||
| 221 | * @return string The metadata array ready for output |
||
| 222 | */ |
||
| 223 | protected function printMetadata(array $metadataArray, $useOriginalIiifManifestMetadata = false) |
||
| 224 | { |
||
| 225 | // Save original data array. |
||
| 226 | $cObjData = $this->cObj->data; |
||
| 227 | // Get list of metadata to show. |
||
| 228 | $metaList = []; |
||
| 229 | if ($useOriginalIiifManifestMetadata) { |
||
| 230 | if ($this->settings['iiifMetadataWrap']) { |
||
| 231 | $iiifwrap = $this->parseTS($this->settings['iiifMetadataWrap']); |
||
| 232 | } else { |
||
| 233 | $iiifwrap['key.']['wrap'] = '<dt>|</dt>'; |
||
| 234 | $iiifwrap['value.']['required'] = 1; |
||
| 235 | $iiifwrap['value.']['wrap'] = '<dd>|</dd>'; |
||
| 236 | } |
||
| 237 | $iiifLink = []; |
||
| 238 | $iiifLink['key.']['wrap'] = '<dt>|</dt>'; |
||
| 239 | $iiifLink['value.']['required'] = 1; |
||
| 240 | $iiifLink['value.']['setContentToCurrent'] = 1; |
||
| 241 | $iiifLink['value.']['typolink.']['parameter.']['current'] = 1; |
||
| 242 | $iiifLink['value.']['typolink.']['forceAbsoluteUrl'] = !empty($this->settings['forceAbsoluteUrl']) ? 1 : 0; |
||
| 243 | $iiifLink['value.']['typolink.']['forceAbsoluteUrl.']['scheme'] = !empty($this->settings['forceAbsoluteUrl']) && !empty($this->settings['forceAbsoluteUrlHttps']) ? 'https' : 'http'; |
||
| 244 | $iiifLink['value.']['wrap'] = '<dd>|</dd>'; |
||
| 245 | foreach ($metadataArray as $metadata) { |
||
| 246 | foreach ($metadata as $key => $group) { |
||
| 247 | $markerArray['METADATA'] = '<span class="tx-dlf-metadata-group">' . $this->pi_getLL($key) . '</span>'; |
||
| 248 | // Reset content object's data array. |
||
| 249 | $this->cObj->data = $cObjData; |
||
| 250 | if (!is_array($group)) { |
||
| 251 | if ($key == '_id') { |
||
| 252 | continue; |
||
| 253 | } |
||
| 254 | $this->cObj->data[$key] = $group; |
||
| 255 | if ( |
||
| 256 | IRI::isAbsoluteIri($this->cObj->data[$key]) |
||
| 257 | && (($scheme = (new IRI($this->cObj->data[$key]))->getScheme()) == 'http' || $scheme == 'https') |
||
| 258 | ) { |
||
| 259 | $field = $this->cObj->stdWrap('', $iiifLink['key.']); |
||
| 260 | $field .= $this->cObj->stdWrap($this->cObj->data[$key], $iiifLink['value.']); |
||
| 261 | } else { |
||
| 262 | $field = $this->cObj->stdWrap('', $iiifwrap['key.']); |
||
| 263 | $field .= $this->cObj->stdWrap($this->cObj->data[$key], $iiifwrap['value.']); |
||
| 264 | } |
||
| 265 | $markerArray['METADATA'] .= $this->cObj->stdWrap($field, $iiifwrap['all.']); |
||
| 266 | } else { |
||
| 267 | // Load all the metadata values into the content object's data array. |
||
| 268 | foreach ($group as $label => $value) { |
||
| 269 | if ($label == '_id') { |
||
| 270 | continue; |
||
| 271 | } |
||
| 272 | if (is_array($value)) { |
||
| 273 | $this->cObj->data[$label] = implode($this->settings['separator'], $value); |
||
| 274 | } else { |
||
| 275 | $this->cObj->data[$label] = $value; |
||
| 276 | } |
||
| 277 | if (IRI::isAbsoluteIri($this->cObj->data[$label]) && (($scheme = (new IRI($this->cObj->data[$label]))->getScheme()) == 'http' || $scheme == 'https')) { |
||
| 278 | $nolabel = $this->cObj->data[$label] == $label; |
||
| 279 | $field = $this->cObj->stdWrap($nolabel ? '' : htmlspecialchars($label), $iiifLink['key.']); |
||
| 280 | $field .= $this->cObj->stdWrap($this->cObj->data[$label], $iiifLink['value.']); |
||
| 281 | } else { |
||
| 282 | $field = $this->cObj->stdWrap(htmlspecialchars($label), $iiifwrap['key.']); |
||
| 283 | $field .= $this->cObj->stdWrap($this->cObj->data[$label], $iiifwrap['value.']); |
||
| 284 | } |
||
| 285 | $markerArray['METADATA'] .= $this->cObj->stdWrap($field, $iiifwrap['all.']); |
||
| 286 | } |
||
| 287 | } |
||
| 288 | } |
||
| 289 | } |
||
| 290 | } else { |
||
| 291 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 292 | ->getQueryBuilderForTable('tx_dlf_metadata'); |
||
| 293 | $result = $queryBuilder |
||
| 294 | ->select( |
||
| 295 | 'tx_dlf_metadata.index_name AS index_name', |
||
| 296 | 'tx_dlf_metadata.is_listed AS is_listed', |
||
| 297 | 'tx_dlf_metadata.wrap AS wrap', |
||
| 298 | 'tx_dlf_metadata.sys_language_uid AS sys_language_uid' |
||
| 299 | ) |
||
| 300 | ->from('tx_dlf_metadata') |
||
| 301 | ->where( |
||
| 302 | $queryBuilder->expr()->andX( |
||
| 303 | $queryBuilder->expr()->orX( |
||
| 304 | $queryBuilder->expr()->in('tx_dlf_metadata.sys_language_uid', [-1, 0]), |
||
| 305 | $queryBuilder->expr()->eq('tx_dlf_metadata.sys_language_uid', $GLOBALS['TSFE']->sys_language_uid) |
||
| 306 | ), |
||
| 307 | $queryBuilder->expr()->eq('tx_dlf_metadata.l18n_parent', 0) |
||
| 308 | ), |
||
| 309 | $queryBuilder->expr()->eq('tx_dlf_metadata.pid', intval($this->settings['pages'])) |
||
| 310 | ) |
||
| 311 | ->orderBy('tx_dlf_metadata.sorting') |
||
| 312 | ->execute(); |
||
| 313 | while ($resArray = $result->fetch()) { |
||
| 314 | if (is_array($resArray) && $resArray['sys_language_uid'] != $GLOBALS['TSFE']->sys_language_content && $GLOBALS['TSFE']->sys_language_contentOL) { |
||
| 315 | $resArray = $GLOBALS['TSFE']->sys_page->getRecordOverlay('tx_dlf_metadata', $resArray, $GLOBALS['TSFE']->sys_language_content, $GLOBALS['TSFE']->sys_language_contentOL); |
||
| 316 | } |
||
| 317 | if ($resArray) { |
||
| 318 | if ($this->settings['showFull'] || $resArray['is_listed']) { |
||
| 319 | $metaList[$resArray['index_name']] = [ |
||
| 320 | 'wrap' => $resArray['wrap'], |
||
| 321 | 'label' => Helper::translate($resArray['index_name'], 'tx_dlf_metadata', $this->settings['pages']) |
||
| 322 | ]; |
||
| 323 | } |
||
| 324 | } |
||
| 325 | } |
||
| 326 | // Get list of collections to show. |
||
| 327 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 328 | ->getQueryBuilderForTable('tx_dlf_collections'); |
||
| 329 | $collList = []; |
||
| 330 | $result = $queryBuilder |
||
| 331 | ->select('tx_dlf_collections.index_name AS index_name') |
||
| 332 | ->from('tx_dlf_collections') |
||
| 333 | ->where( |
||
| 334 | $queryBuilder->expr()->eq('tx_dlf_collections.pid', intval($this->settings['pages'])) |
||
| 335 | ) |
||
| 336 | ->execute(); |
||
| 337 | while ($resArray = $result->fetch()) { |
||
| 338 | $collList[] = $resArray['index_name']; |
||
| 339 | } |
||
| 340 | // Parse the metadata arrays. |
||
| 341 | foreach ($metadataArray as $metadata) { |
||
| 342 | $markerArray['METADATA'] = ''; |
||
| 343 | // Reset content object's data array. |
||
| 344 | $this->cObj->data = $cObjData; |
||
| 345 | // Load all the metadata values into the content object's data array. |
||
| 346 | foreach ($metadata as $index_name => $value) { |
||
| 347 | if (is_array($value)) { |
||
| 348 | $this->cObj->data[$index_name] = implode($this->settings['separator'], $value); |
||
| 349 | } else { |
||
| 350 | $this->cObj->data[$index_name] = $value; |
||
| 351 | } |
||
| 352 | } |
||
| 353 | // Process each metadate. |
||
| 354 | foreach ($metaList as $index_name => $metaConf) { |
||
| 355 | $parsedValue = ''; |
||
| 356 | $fieldwrap = $this->parseTS($metaConf['wrap']); |
||
| 357 | do { |
||
| 358 | $value = @array_shift($metadata[$index_name]); |
||
| 359 | if ($index_name == 'title') { |
||
| 360 | // Get title of parent document if needed. |
||
| 361 | if (empty($value) && $this->settings['getTitle'] && $this->doc->parentId) { |
||
| 362 | $superiorTitle = Document::getTitle($this->doc->parentId, true); |
||
| 363 | if (!empty($superiorTitle)) { |
||
| 364 | $value = '[' . $superiorTitle . ']'; |
||
| 365 | } |
||
| 366 | } |
||
| 367 | if (!empty($value)) { |
||
| 368 | $value = htmlspecialchars($value); |
||
| 369 | // Link title to pageview. |
||
| 370 | if ($this->settings['linkTitle'] && $metadata['_id']) { |
||
| 371 | $details = $this->doc->getLogicalStructure($metadata['_id']); |
||
| 372 | $uri = $this->uriBuilder->reset() |
||
| 373 | ->setArguments([ |
||
| 374 | $this->prefixId => [ |
||
| 375 | 'id' => $this->doc->uid, |
||
| 376 | 'page' => (!empty($details['points']) ? intval($details['points']) : 1) |
||
| 377 | ] |
||
| 378 | ]) |
||
| 379 | ->setTargetPageUid($this->settings['targetPid']) |
||
| 380 | ->build(); |
||
| 381 | $value = '<a href="' . $uri . '">' . $value . '</a>'; |
||
| 382 | } |
||
| 383 | } |
||
| 384 | } elseif ($index_name == 'owner' && !empty($value)) { |
||
| 385 | // Translate name of holding library. |
||
| 386 | $value = htmlspecialchars(Helper::translate($value, 'tx_dlf_libraries', $this->settings['pages'])); |
||
| 387 | } elseif ($index_name == 'type' && !empty($value)) { |
||
| 388 | // Translate document type. |
||
| 389 | $value = htmlspecialchars(Helper::translate($value, 'tx_dlf_structures', $this->settings['pages'])); |
||
| 390 | } elseif ($index_name == 'collection' && !empty($value)) { |
||
| 391 | // Check if collections isn't hidden. |
||
| 392 | if (in_array($value, $collList)) { |
||
| 393 | // Translate collection. |
||
| 394 | $value = htmlspecialchars(Helper::translate($value, 'tx_dlf_collections', $this->settings['pages'])); |
||
| 395 | } else { |
||
| 396 | $value = ''; |
||
| 397 | } |
||
| 398 | } elseif ($index_name == 'language' && !empty($value)) { |
||
| 399 | // Translate ISO 639 language code. |
||
| 400 | $value = htmlspecialchars(Helper::getLanguageName($value)); |
||
| 401 | } elseif (!empty($value)) { |
||
| 402 | // Sanitize value for output. |
||
| 403 | $value = htmlspecialchars($value); |
||
| 404 | } |
||
| 405 | // Hook for getting a customized value (requested by SBB). |
||
| 406 | foreach ($this->hookObjects as $hookObj) { |
||
| 407 | if (method_exists($hookObj, 'printMetadata_customizeMetadata')) { |
||
| 408 | $hookObj->printMetadata_customizeMetadata($value); |
||
| 409 | } |
||
| 410 | } |
||
| 411 | // $value might be empty for aggregation metadata fields including other "hidden" fields. |
||
| 412 | $value = $this->cObj->stdWrap($value, $fieldwrap['value.']); |
||
| 413 | if (!empty($value)) { |
||
| 414 | $parsedValue .= $value; |
||
| 415 | } |
||
| 416 | } while (is_array($metadata[$index_name]) && count($metadata[$index_name]) > 0); |
||
| 417 | |||
| 418 | if (!empty($parsedValue)) { |
||
| 419 | $field = $this->cObj->stdWrap(htmlspecialchars($metaConf['label']), $fieldwrap['key.']); |
||
| 420 | $field .= $parsedValue; |
||
| 421 | $markerArray['METADATA'] .= $this->cObj->stdWrap($field, $fieldwrap['all.']); |
||
| 422 | } |
||
| 423 | } |
||
| 424 | } |
||
| 425 | } |
||
| 426 | $this->view->assign('metadata', $markerArray['METADATA']); |
||
| 427 | } |
||
| 428 | |||
| 429 | // TODO: Needs to be placed in an abstract class (like before in AbstractPlugin) |
||
| 430 | /** |
||
| 431 | * Parses a string into a Typoscript array |
||
| 432 | * |
||
| 433 | * @access protected |
||
| 434 | * |
||
| 435 | * @param string $string: The string to parse |
||
| 436 | * |
||
| 437 | * @return array The resulting typoscript array |
||
| 438 | */ |
||
| 439 | protected function parseTS($string = '') |
||
| 440 | { |
||
| 441 | $parser = GeneralUtility::makeInstance(\TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser::class); |
||
| 442 | $parser->parse($string); |
||
| 443 | return $parser->setup; |
||
| 444 | } |
||
| 445 | |||
| 446 | protected function pi_getLL($label) |
||
| 449 | } |
||
| 450 | |||
| 451 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths