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