We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 88 |
| Total Lines | 339 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| 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 |
||
| 32 | class MetadataController extends AbstractController |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * @var CollectionRepository |
||
| 36 | */ |
||
| 37 | protected $collectionRepository; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param CollectionRepository $collectionRepository |
||
| 41 | */ |
||
| 42 | public function injectCollectionRepository(CollectionRepository $collectionRepository) |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var MetadataRepository |
||
| 49 | */ |
||
| 50 | protected $metadataRepository; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param MetadataRepository $metadataRepository |
||
| 54 | */ |
||
| 55 | public function injectMetadataRepository(MetadataRepository $metadataRepository) |
||
| 56 | { |
||
| 57 | $this->metadataRepository = $metadataRepository; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var StructureRepository |
||
| 62 | */ |
||
| 63 | protected $structureRepository; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param StructureRepository $structureRepository |
||
| 67 | */ |
||
| 68 | public function injectStructureRepository(StructureRepository $structureRepository) |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @return string|void |
||
| 75 | */ |
||
| 76 | public function mainAction() |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Prepares the metadata array for output |
||
| 126 | * |
||
| 127 | * @access protected |
||
| 128 | * |
||
| 129 | * @param array $metadata: The metadata array |
||
| 130 | * @param bool $useOriginalIiifManifestMetadata: Output IIIF metadata as simple key/value pairs? |
||
| 131 | * |
||
| 132 | * @return string The metadata array ready for output |
||
| 133 | */ |
||
| 134 | protected function printMetadata(array $metadata, $useOriginalIiifManifestMetadata = false) |
||
| 135 | { |
||
| 136 | if ($useOriginalIiifManifestMetadata) { |
||
| 137 | $iiifData = []; |
||
| 138 | foreach ($metadata as $row) { |
||
| 139 | foreach ($row as $key => $group) { |
||
| 140 | if ($key == '_id' || $key === '_active') { |
||
| 141 | continue; |
||
| 142 | } |
||
| 143 | if (!is_array($group)) { |
||
| 144 | if ( |
||
| 145 | IRI::isAbsoluteIri($group) |
||
| 146 | && (($scheme = (new IRI($group))->getScheme()) == 'http' || $scheme == 'https') |
||
| 147 | ) { |
||
| 148 | // Build link |
||
| 149 | $iiifData[$key] = [ |
||
| 150 | 'label' => $key, |
||
| 151 | 'value' => $group, |
||
| 152 | 'buildUrl' => true, |
||
| 153 | ]; |
||
| 154 | } else { |
||
| 155 | // Data output |
||
| 156 | $iiifData[$key] = [ |
||
| 157 | 'label' => $key, |
||
| 158 | 'value' => $group, |
||
| 159 | 'buildUrl' => false, |
||
| 160 | ]; |
||
| 161 | } |
||
| 162 | } else { |
||
| 163 | foreach ($group as $label => $value) { |
||
| 164 | if ($label === '_id' || $label === '_active') { |
||
| 165 | continue; |
||
| 166 | } |
||
| 167 | if (is_array($value)) { |
||
| 168 | $value = implode($this->settings['separator'], $value); |
||
| 169 | } |
||
| 170 | // NOTE: Labels are to be escaped in Fluid template |
||
| 171 | if (IRI::isAbsoluteIri($value) && (($scheme = (new IRI($value))->getScheme()) == 'http' || $scheme == 'https')) { |
||
| 172 | $nolabel = $value == $label; |
||
| 173 | $iiifData[$key]['data'][] = [ |
||
| 174 | 'label' => $nolabel ? '' : $label, |
||
| 175 | 'value' => $value, |
||
| 176 | 'buildUrl' => true, |
||
| 177 | ]; |
||
| 178 | } else { |
||
| 179 | $iiifData[$key]['data'][] = [ |
||
| 180 | 'label' => $label, |
||
| 181 | 'value' => $value, |
||
| 182 | 'buildUrl' => false, |
||
| 183 | ]; |
||
| 184 | } |
||
| 185 | } |
||
| 186 | } |
||
| 187 | $this->view->assign('useIiif', true); |
||
| 188 | $this->view->assign('iiifData', $iiifData); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | } else { |
||
| 192 | // findBySettings also sorts entries by the `sorting` field |
||
| 193 | $metadataResult = $this->metadataRepository->findBySettings([ |
||
| 194 | 'is_listed' => !$this->settings['showFull'], |
||
| 195 | ]); |
||
| 196 | |||
| 197 | // Collect raw metadata into an array that will be passed as data to cObj. |
||
| 198 | // This lets metadata wraps reference (own or foreign) values via TypoScript "field". |
||
| 199 | $metaCObjData = []; |
||
| 200 | |||
| 201 | $buildUrl = []; |
||
| 202 | $externalUrl = []; |
||
| 203 | $i = 0; |
||
| 204 | foreach ($metadata as $section) { |
||
| 205 | $metaCObjData[$i] = []; |
||
| 206 | |||
| 207 | foreach ($section as $name => $value) { |
||
| 208 | // NOTE: Labels are to be escaped in Fluid template |
||
| 209 | |||
| 210 | $metaCObjData[$i][$name] = is_array($value) |
||
| 211 | ? implode($this->settings['separator'], $value) |
||
| 212 | : $value; |
||
| 213 | |||
| 214 | if ($name == 'title') { |
||
| 215 | // Get title of parent document if needed. |
||
| 216 | if (empty(implode('', $value)) && $this->settings['getTitle'] && $this->document->getPartof()) { |
||
| 217 | $superiorTitle = Doc::getTitle($this->document->getPartof(), true); |
||
| 218 | if (!empty($superiorTitle)) { |
||
| 219 | $metadata[$i][$name] = ['[' . $superiorTitle . ']']; |
||
| 220 | } |
||
| 221 | } |
||
| 222 | if (!empty($value)) { |
||
| 223 | $metadata[$i][$name][0] = $metadata[$i][$name][0]; |
||
| 224 | // Link title to pageview. |
||
| 225 | if ($this->settings['linkTitle'] && $section['_id']) { |
||
| 226 | $details = $this->document->getDoc()->getLogicalStructure($section['_id']); |
||
| 227 | $buildUrl[$i][$name]['buildUrl'] = [ |
||
| 228 | 'id' => $this->document->getUid(), |
||
| 229 | 'page' => (!empty($details['points']) ? intval($details['points']) : 1), |
||
| 230 | 'targetPid' => (!empty($this->settings['targetPid']) ? $this->settings['targetPid'] : 0), |
||
| 231 | ]; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | } elseif (($name == 'author' || $name == 'holder') && !empty($value) && !empty($value[0]['url'])) { |
||
| 235 | $externalUrl[$i][$name]['externalUrl'] = $value[0]; |
||
| 236 | } elseif (($name == 'geonames' || $name == 'wikidata' || $name == 'wikipedia') && !empty($value)) { |
||
| 237 | $externalUrl[$i][$name]['externalUrl'] = [ |
||
| 238 | 'name' => $value[0], |
||
| 239 | 'url' => $value[0] |
||
| 240 | ]; |
||
| 241 | } elseif ($name == 'owner' && empty($value)) { |
||
| 242 | // no owner is found by metadata records --> take the one associated to the document |
||
| 243 | $library = $this->document->getOwner(); |
||
| 244 | if ($library) { |
||
| 245 | $metadata[$i][$name][0] = $library->getLabel(); |
||
| 246 | } |
||
| 247 | } elseif ($name == 'type' && !empty($value)) { |
||
| 248 | // Translate document type. |
||
| 249 | $structure = $this->structureRepository->findOneByIndexName($metadata[$i][$name][0]); |
||
| 250 | if ($structure) { |
||
| 251 | $metadata[$i][$name][0] = $structure->getLabel(); |
||
| 252 | } |
||
| 253 | } elseif ($name == 'collection' && !empty($value)) { |
||
| 254 | // Check if collections isn't hidden. |
||
| 255 | $j = 0; |
||
| 256 | foreach ($value as $entry) { |
||
| 257 | $collection = $this->collectionRepository->findOneByIndexName($entry); |
||
| 258 | if ($collection) { |
||
| 259 | $metadata[$i][$name][$j] = $collection->getLabel() ? : ''; |
||
| 260 | $j++; |
||
| 261 | } |
||
| 262 | } |
||
| 263 | } elseif ($name == 'language' && !empty($value)) { |
||
| 264 | // Translate ISO 639 language code. |
||
| 265 | foreach ($metadata[$i][$name] as &$langValue) { |
||
| 266 | $langValue = Helper::getLanguageName($langValue); |
||
| 267 | } |
||
| 268 | } elseif (!empty($value)) { |
||
| 269 | $metadata[$i][$name][0] = $metadata[$i][$name][0]; |
||
| 270 | } |
||
| 271 | |||
| 272 | if (is_array($metadata[$i][$name])) { |
||
| 273 | $metadata[$i][$name] = array_values(array_filter($metadata[$i][$name], function($metadataValue) |
||
| 274 | { |
||
| 275 | return !empty($metadataValue); |
||
| 276 | })); |
||
| 277 | } |
||
| 278 | } |
||
| 279 | $i++; |
||
| 280 | } |
||
| 281 | |||
| 282 | $this->view->assign('buildUrl', $buildUrl); |
||
| 283 | $this->view->assign('externalUrl', $externalUrl); |
||
| 284 | $this->view->assign('documentMetadataSections', $metadata); |
||
| 285 | $this->view->assign('configMetadata', $metadataResult); |
||
| 286 | $this->view->assign('separator', $this->settings['separator']); |
||
| 287 | $this->view->assign('metaCObjData', $metaCObjData); |
||
| 288 | } |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Get metadata for given id array. |
||
| 293 | * |
||
| 294 | * @access private |
||
| 295 | * |
||
| 296 | * @return array metadata |
||
| 297 | */ |
||
| 298 | private function getMetadata() |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Get metadata for given id array. |
||
| 347 | * |
||
| 348 | * @access private |
||
| 349 | * |
||
| 350 | * @param array $id: An array with ids |
||
| 351 | * @param array $metadata: An array with metadata |
||
| 352 | * |
||
| 353 | * @return array metadata |
||
| 354 | */ |
||
| 355 | private function getMetadataForIds($id, $metadata) |
||
| 371 | } |
||
| 372 | } |
||
| 373 |