We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 87 |
| Total Lines | 340 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| 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 |
||
| 23 | class MetadataController extends AbstractController |
||
| 24 | { |
||
| 25 | public $prefixId = 'tx_dlf'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @return string|void |
||
| 29 | */ |
||
| 30 | public function mainAction() |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Prepares the metadata array for output |
||
| 129 | * |
||
| 130 | * @access protected |
||
| 131 | * |
||
| 132 | * @param array $metadataArray: The metadata array |
||
| 133 | * @param bool $useOriginalIiifManifestMetadata: Output IIIF metadata as simple key/value pairs? |
||
| 134 | * |
||
| 135 | * @return string The metadata array ready for output |
||
| 136 | */ |
||
| 137 | protected function printMetadata(array $metadataArray, $useOriginalIiifManifestMetadata = false) |
||
| 138 | { |
||
| 139 | // Save original data array. |
||
| 140 | $cObjData = $this->cObj->data; |
||
| 141 | // Get list of metadata to show. |
||
| 142 | $metaList = []; |
||
| 143 | if ($useOriginalIiifManifestMetadata) { |
||
| 144 | if ($this->settings['iiifMetadataWrap']) { |
||
| 145 | $iiifwrap = $this->parseTS($this->settings['iiifMetadataWrap']); |
||
| 146 | } else { |
||
| 147 | $iiifwrap['key.']['wrap'] = '<dt>|</dt>'; |
||
| 148 | $iiifwrap['value.']['required'] = 1; |
||
| 149 | $iiifwrap['value.']['wrap'] = '<dd>|</dd>'; |
||
| 150 | } |
||
| 151 | $iiifLink = []; |
||
| 152 | $iiifLink['key.']['wrap'] = '<dt>|</dt>'; |
||
| 153 | $iiifLink['value.']['required'] = 1; |
||
| 154 | $iiifLink['value.']['setContentToCurrent'] = 1; |
||
| 155 | $iiifLink['value.']['typolink.']['parameter.']['current'] = 1; |
||
| 156 | $iiifLink['value.']['typolink.']['forceAbsoluteUrl'] = !empty($this->settings['forceAbsoluteUrl']) ? 1 : 0; |
||
| 157 | $iiifLink['value.']['typolink.']['forceAbsoluteUrl.']['scheme'] = !empty($this->settings['forceAbsoluteUrl']) && !empty($this->settings['forceAbsoluteUrlHttps']) ? 'https' : 'http'; |
||
| 158 | $iiifLink['value.']['wrap'] = '<dd>|</dd>'; |
||
| 159 | foreach ($metadataArray as $metadata) { |
||
| 160 | foreach ($metadata as $key => $group) { |
||
| 161 | $markerArray['METADATA'] = '<span class="tx-dlf-metadata-group">' . $this->pi_getLL($key) . '</span>'; |
||
| 162 | // Reset content object's data array. |
||
| 163 | $this->cObj->data = $cObjData; |
||
| 164 | if (!is_array($group)) { |
||
| 165 | if ($key == '_id') { |
||
| 166 | continue; |
||
| 167 | } |
||
| 168 | $this->cObj->data[$key] = $group; |
||
| 169 | if ( |
||
| 170 | IRI::isAbsoluteIri($this->cObj->data[$key]) |
||
| 171 | && (($scheme = (new IRI($this->cObj->data[$key]))->getScheme()) == 'http' || $scheme == 'https') |
||
| 172 | ) { |
||
| 173 | $field = $this->cObj->stdWrap('', $iiifLink['key.']); |
||
| 174 | $field .= $this->cObj->stdWrap($this->cObj->data[$key], $iiifLink['value.']); |
||
| 175 | } else { |
||
| 176 | $field = $this->cObj->stdWrap('', $iiifwrap['key.']); |
||
| 177 | $field .= $this->cObj->stdWrap($this->cObj->data[$key], $iiifwrap['value.']); |
||
| 178 | } |
||
| 179 | $markerArray['METADATA'] .= $this->cObj->stdWrap($field, $iiifwrap['all.']); |
||
| 180 | } else { |
||
| 181 | // Load all the metadata values into the content object's data array. |
||
| 182 | foreach ($group as $label => $value) { |
||
| 183 | if ($label == '_id') { |
||
| 184 | continue; |
||
| 185 | } |
||
| 186 | if (is_array($value)) { |
||
| 187 | $this->cObj->data[$label] = implode($this->settings['separator'], $value); |
||
| 188 | } else { |
||
| 189 | $this->cObj->data[$label] = $value; |
||
| 190 | } |
||
| 191 | if (IRI::isAbsoluteIri($this->cObj->data[$label]) && (($scheme = (new IRI($this->cObj->data[$label]))->getScheme()) == 'http' || $scheme == 'https')) { |
||
| 192 | $nolabel = $this->cObj->data[$label] == $label; |
||
| 193 | $field = $this->cObj->stdWrap($nolabel ? '' : htmlspecialchars($label), $iiifLink['key.']); |
||
| 194 | $field .= $this->cObj->stdWrap($this->cObj->data[$label], $iiifLink['value.']); |
||
| 195 | } else { |
||
| 196 | $field = $this->cObj->stdWrap(htmlspecialchars($label), $iiifwrap['key.']); |
||
| 197 | $field .= $this->cObj->stdWrap($this->cObj->data[$label], $iiifwrap['value.']); |
||
| 198 | } |
||
| 199 | $markerArray['METADATA'] .= $this->cObj->stdWrap($field, $iiifwrap['all.']); |
||
| 200 | } |
||
| 201 | } |
||
| 202 | } |
||
| 203 | } |
||
| 204 | } else { |
||
| 205 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 206 | ->getQueryBuilderForTable('tx_dlf_metadata'); |
||
| 207 | $result = $queryBuilder |
||
| 208 | ->select( |
||
| 209 | 'tx_dlf_metadata.index_name AS index_name', |
||
| 210 | 'tx_dlf_metadata.is_listed AS is_listed', |
||
| 211 | 'tx_dlf_metadata.wrap AS wrap', |
||
| 212 | 'tx_dlf_metadata.sys_language_uid AS sys_language_uid' |
||
| 213 | ) |
||
| 214 | ->from('tx_dlf_metadata') |
||
| 215 | ->where( |
||
| 216 | $queryBuilder->expr()->andX( |
||
| 217 | $queryBuilder->expr()->orX( |
||
| 218 | $queryBuilder->expr()->in('tx_dlf_metadata.sys_language_uid', [-1, 0]), |
||
| 219 | $queryBuilder->expr()->eq('tx_dlf_metadata.sys_language_uid', $GLOBALS['TSFE']->sys_language_uid) |
||
| 220 | ), |
||
| 221 | $queryBuilder->expr()->eq('tx_dlf_metadata.l18n_parent', 0) |
||
| 222 | ), |
||
| 223 | $queryBuilder->expr()->eq('tx_dlf_metadata.pid', intval($this->settings['pages'])) |
||
| 224 | ) |
||
| 225 | ->orderBy('tx_dlf_metadata.sorting') |
||
| 226 | ->execute(); |
||
| 227 | while ($resArray = $result->fetch()) { |
||
| 228 | if (is_array($resArray) && $resArray['sys_language_uid'] != $GLOBALS['TSFE']->sys_language_content && $GLOBALS['TSFE']->sys_language_contentOL) { |
||
| 229 | $resArray = $GLOBALS['TSFE']->sys_page->getRecordOverlay('tx_dlf_metadata', $resArray, $GLOBALS['TSFE']->sys_language_content, $GLOBALS['TSFE']->sys_language_contentOL); |
||
| 230 | } |
||
| 231 | if ($resArray) { |
||
| 232 | if ($this->settings['showFull'] || $resArray['is_listed']) { |
||
| 233 | $metaList[$resArray['index_name']] = [ |
||
| 234 | 'wrap' => $resArray['wrap'], |
||
| 235 | 'label' => Helper::translate($resArray['index_name'], 'tx_dlf_metadata', $this->settings['pages']) |
||
| 236 | ]; |
||
| 237 | } |
||
| 238 | } |
||
| 239 | } |
||
| 240 | // Get list of collections to show. |
||
| 241 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 242 | ->getQueryBuilderForTable('tx_dlf_collections'); |
||
| 243 | $collList = []; |
||
| 244 | $result = $queryBuilder |
||
| 245 | ->select('tx_dlf_collections.index_name AS index_name') |
||
| 246 | ->from('tx_dlf_collections') |
||
| 247 | ->where( |
||
| 248 | $queryBuilder->expr()->eq('tx_dlf_collections.pid', intval($this->settings['pages'])) |
||
| 249 | ) |
||
| 250 | ->execute(); |
||
| 251 | while ($resArray = $result->fetch()) { |
||
| 252 | $collList[] = $resArray['index_name']; |
||
| 253 | } |
||
| 254 | // Parse the metadata arrays. |
||
| 255 | foreach ($metadataArray as $metadata) { |
||
| 256 | $markerArray['METADATA'] = ''; |
||
| 257 | // Reset content object's data array. |
||
| 258 | $this->cObj->data = $cObjData; |
||
| 259 | // Load all the metadata values into the content object's data array. |
||
| 260 | foreach ($metadata as $index_name => $value) { |
||
| 261 | if (is_array($value)) { |
||
| 262 | $this->cObj->data[$index_name] = implode($this->settings['separator'], $value); |
||
| 263 | } else { |
||
| 264 | $this->cObj->data[$index_name] = $value; |
||
| 265 | } |
||
| 266 | } |
||
| 267 | // Process each metadate. |
||
| 268 | foreach ($metaList as $index_name => $metaConf) { |
||
| 269 | $parsedValue = ''; |
||
| 270 | $fieldwrap = $this->parseTS($metaConf['wrap']); |
||
| 271 | do { |
||
| 272 | $value = @array_shift($metadata[$index_name]); |
||
| 273 | if ($index_name == 'title') { |
||
| 274 | // Get title of parent document if needed. |
||
| 275 | if (empty($value) && $this->settings['getTitle'] && $this->doc->parentId) { |
||
| 276 | $superiorTitle = Document::getTitle($this->doc->parentId, true); |
||
| 277 | if (!empty($superiorTitle)) { |
||
| 278 | $value = '[' . $superiorTitle . ']'; |
||
| 279 | } |
||
| 280 | } |
||
| 281 | if (!empty($value)) { |
||
| 282 | $value = htmlspecialchars($value); |
||
| 283 | // Link title to pageview. |
||
| 284 | if ($this->settings['linkTitle'] && $metadata['_id']) { |
||
| 285 | $details = $this->doc->getLogicalStructure($metadata['_id']); |
||
| 286 | $uri = $this->uriBuilder->reset() |
||
| 287 | ->setArguments([ |
||
| 288 | $this->prefixId => [ |
||
| 289 | 'id' => $this->doc->uid, |
||
| 290 | 'page' => (!empty($details['points']) ? intval($details['points']) : 1) |
||
| 291 | ] |
||
| 292 | ]) |
||
| 293 | ->setTargetPageUid($this->settings['targetPid']) |
||
| 294 | ->build(); |
||
| 295 | $value = '<a href="' . $uri . '">' . $value . '</a>'; |
||
| 296 | } |
||
| 297 | } |
||
| 298 | } elseif ($index_name == 'owner' && !empty($value)) { |
||
| 299 | // Translate name of holding library. |
||
| 300 | $value = htmlspecialchars(Helper::translate($value, 'tx_dlf_libraries', $this->settings['pages'])); |
||
| 301 | } elseif ($index_name == 'type' && !empty($value)) { |
||
| 302 | // Translate document type. |
||
| 303 | $value = htmlspecialchars(Helper::translate($value, 'tx_dlf_structures', $this->settings['pages'])); |
||
| 304 | } elseif ($index_name == 'collection' && !empty($value)) { |
||
| 305 | // Check if collections isn't hidden. |
||
| 306 | if (in_array($value, $collList)) { |
||
| 307 | // Translate collection. |
||
| 308 | $value = htmlspecialchars(Helper::translate($value, 'tx_dlf_collections', $this->settings['pages'])); |
||
| 309 | } else { |
||
| 310 | $value = ''; |
||
| 311 | } |
||
| 312 | } elseif ($index_name == 'language' && !empty($value)) { |
||
| 313 | // Translate ISO 639 language code. |
||
| 314 | $value = htmlspecialchars(Helper::getLanguageName($value)); |
||
| 315 | } elseif (!empty($value)) { |
||
| 316 | // Sanitize value for output. |
||
| 317 | $value = htmlspecialchars($value); |
||
| 318 | } |
||
| 319 | // Hook for getting a customized value (requested by SBB). |
||
| 320 | foreach ($this->hookObjects as $hookObj) { |
||
| 321 | if (method_exists($hookObj, 'printMetadata_customizeMetadata')) { |
||
| 322 | $hookObj->printMetadata_customizeMetadata($value); |
||
| 323 | } |
||
| 324 | } |
||
| 325 | // $value might be empty for aggregation metadata fields including other "hidden" fields. |
||
| 326 | $value = $this->cObj->stdWrap($value, $fieldwrap['value.']); |
||
| 327 | if (!empty($value)) { |
||
| 328 | $parsedValue .= $value; |
||
| 329 | } |
||
| 330 | } while (is_array($metadata[$index_name]) && count($metadata[$index_name]) > 0); |
||
| 331 | |||
| 332 | if (!empty($parsedValue)) { |
||
| 333 | $field = $this->cObj->stdWrap(htmlspecialchars($metaConf['label']), $fieldwrap['key.']); |
||
| 334 | $field .= $parsedValue; |
||
| 335 | $markerArray['METADATA'] .= $this->cObj->stdWrap($field, $fieldwrap['all.']); |
||
| 336 | } |
||
| 337 | } |
||
| 338 | } |
||
| 339 | } |
||
| 340 | $this->view->assign('metadata', $markerArray['METADATA']); |
||
| 341 | } |
||
| 342 | |||
| 343 | // TODO: Needs to be placed in an abstract class (like before in AbstractPlugin) |
||
| 344 | /** |
||
| 345 | * Parses a string into a Typoscript array |
||
| 346 | * |
||
| 347 | * @access protected |
||
| 348 | * |
||
| 349 | * @param string $string: The string to parse |
||
| 350 | * |
||
| 351 | * @return array The resulting typoscript array |
||
| 352 | */ |
||
| 353 | protected function parseTS($string = '') |
||
| 354 | { |
||
| 355 | $parser = GeneralUtility::makeInstance(\TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser::class); |
||
| 356 | $parser->parse($string); |
||
| 357 | return $parser->setup; |
||
| 358 | } |
||
| 359 | |||
| 360 | protected function pi_getLL($label) |
||
| 363 | } |
||
| 364 | |||
| 365 | } |
||
| 366 |