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