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 | 410 |
| Duplicated Lines | 0 % |
| Changes | 14 | ||
| 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 Doc |
||
| 36 | */ |
||
| 37 | private $doc; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var CollectionRepository |
||
| 41 | */ |
||
| 42 | protected $collectionRepository; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param CollectionRepository $collectionRepository |
||
| 46 | */ |
||
| 47 | public function injectCollectionRepository(CollectionRepository $collectionRepository) |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var MetadataRepository |
||
| 54 | */ |
||
| 55 | protected $metadataRepository; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param MetadataRepository $metadataRepository |
||
| 59 | */ |
||
| 60 | public function injectMetadataRepository(MetadataRepository $metadataRepository) |
||
| 61 | { |
||
| 62 | $this->metadataRepository = $metadataRepository; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var StructureRepository |
||
| 67 | */ |
||
| 68 | protected $structureRepository; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param StructureRepository $structureRepository |
||
| 72 | */ |
||
| 73 | public function injectStructureRepository(StructureRepository $structureRepository) |
||
| 74 | { |
||
| 75 | $this->structureRepository = $structureRepository; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @return string|void |
||
| 80 | */ |
||
| 81 | public function mainAction() |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Prepares the metadata array for output |
||
| 120 | * |
||
| 121 | * @access protected |
||
| 122 | * |
||
| 123 | * @param array $metadata: The metadata array |
||
| 124 | * @param bool $useOriginalIiifManifestMetadata: Output IIIF metadata as simple key/value pairs? |
||
| 125 | * |
||
| 126 | * @return string The metadata array ready for output |
||
| 127 | */ |
||
| 128 | protected function printMetadata(array $metadata, $useOriginalIiifManifestMetadata = false) |
||
| 129 | { |
||
| 130 | if ($useOriginalIiifManifestMetadata) { |
||
| 131 | $iiifData = $this->buildIiifData($metadata); |
||
| 132 | $this->view->assign('useIiif', true); |
||
| 133 | $this->view->assign('iiifData', $iiifData); |
||
| 134 | } else { |
||
| 135 | // findBySettings also sorts entries by the `sorting` field |
||
| 136 | $metadataResult = $this->metadataRepository->findBySettings([ |
||
| 137 | 'is_listed' => !$this->settings['showFull'], |
||
| 138 | ]); |
||
| 139 | |||
| 140 | foreach ($metadata as $i => $section) { |
||
| 141 | |||
| 142 | foreach ($section as $name => $value) { |
||
| 143 | // NOTE: Labels are to be escaped in Fluid template |
||
| 144 | |||
| 145 | $this->parseMetadata($i, $name, $value, $metadata); |
||
| 146 | |||
| 147 | if (is_array($metadata[$i][$name])) { |
||
| 148 | $metadata[$i][$name] = array_values(array_filter($metadata[$i][$name], function($metadataValue) |
||
| 149 | { |
||
| 150 | return !empty($metadataValue); |
||
| 151 | })); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | $this->view->assign('buildUrl', $this->buildUrlFromMetadata($metadata)); |
||
| 157 | $this->view->assign('externalUrl', $this->buildExternalUrlFromMetadata($metadata)); |
||
| 158 | $this->view->assign('documentMetadataSections', $metadata); |
||
| 159 | $this->view->assign('configMetadata', $metadataResult); |
||
| 160 | $this->view->assign('separator', $this->settings['separator']); |
||
| 161 | $this->view->assign('metaCObjData', $this->buildMetaCObjData($metadata)); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Builds the IIIF data array from metadata array |
||
| 167 | * |
||
| 168 | * @access private |
||
| 169 | * |
||
| 170 | * @param array $metadata The metadata array |
||
| 171 | * |
||
| 172 | * @return array The IIIF data array ready for output |
||
| 173 | */ |
||
| 174 | private function buildIiifData(array $metadata): array |
||
| 175 | { |
||
| 176 | $iiifData = []; |
||
| 177 | |||
| 178 | foreach ($metadata as $row) { |
||
| 179 | foreach ($row as $key => $group) { |
||
| 180 | if ($key == '_id') { |
||
| 181 | continue; |
||
| 182 | } |
||
| 183 | |||
| 184 | if (!is_array($group)) { |
||
| 185 | $iiifData[$key] = $this->buildIiifDataGroup($key, $group); |
||
| 186 | } else { |
||
| 187 | foreach ($group as $label => $value) { |
||
| 188 | if ($label == '_id') { |
||
| 189 | continue; |
||
| 190 | } |
||
| 191 | if (is_array($value)) { |
||
| 192 | $value = implode($this->settings['separator'], $value); |
||
| 193 | } |
||
| 194 | |||
| 195 | $iiifData[$key]['data'][] = $this->buildIiifDataGroup($label, $value); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | return $iiifData; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Builds the IIIF data array from label and value |
||
| 206 | * |
||
| 207 | * @access private |
||
| 208 | * |
||
| 209 | * @param string $label The label string |
||
| 210 | * @param string $value The value string |
||
| 211 | * |
||
| 212 | * @return array The IIIF data array ready for output |
||
| 213 | */ |
||
| 214 | private function buildIiifDataGroup(string $label, string $value): array |
||
| 215 | { |
||
| 216 | // NOTE: Labels are to be escaped in Fluid template |
||
| 217 | if (IRI::isAbsoluteIri($value) && ($scheme = (new IRI($value))->getScheme()) == 'http' || $scheme == 'https') { |
||
| 218 | //TODO: should really label be converted to empty string if equal to value? |
||
| 219 | $label = $value == $label ? '' : $label; |
||
| 220 | $buildUrl = true; |
||
| 221 | } else { |
||
| 222 | $buildUrl = false; |
||
| 223 | } |
||
| 224 | |||
| 225 | return [ |
||
| 226 | 'label' => $label, |
||
| 227 | 'value' => $value, |
||
| 228 | 'buildUrl' => $buildUrl, |
||
| 229 | ]; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Collects raw metadata into an array that will be passed as data to cObj. |
||
| 234 | * This lets metadata wraps reference (own or foreign) values via TypoScript "field". |
||
| 235 | * |
||
| 236 | * @access private |
||
| 237 | * |
||
| 238 | * @param array $metadata The metadata array |
||
| 239 | * |
||
| 240 | * @return array The raw metadata array ready for output |
||
| 241 | */ |
||
| 242 | private function buildMetaCObjData(array $metadata) |
||
| 243 | { |
||
| 244 | $metaCObjData = []; |
||
| 245 | |||
| 246 | foreach ($metadata as $i => $section) { |
||
| 247 | $metaCObjData[$i] = []; |
||
| 248 | |||
| 249 | foreach ($section as $name => $value) { |
||
| 250 | $metaCObjData[$i][$name] = is_array($value) |
||
| 251 | ? implode($this->settings['separator'], $value) |
||
| 252 | : $value; |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 256 | return $metaCObjData; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Builds URLs array for given metadata array. |
||
| 261 | * |
||
| 262 | * @access private |
||
| 263 | * |
||
| 264 | * @param array $metadata The metadata array |
||
| 265 | * |
||
| 266 | * @return array URLs |
||
| 267 | */ |
||
| 268 | private function buildUrlFromMetadata(array $metadata) |
||
| 269 | { |
||
| 270 | $buildUrl = []; |
||
| 271 | |||
| 272 | foreach ($metadata as $i => $section) { |
||
| 273 | if ($this->settings['linkTitle'] && $section['_id'] && isset($section['title']) && !empty($section['title'])) { |
||
| 274 | $details = $this->doc->getLogicalStructure($section['_id']); |
||
| 275 | $buildUrl[$i]['title'] = [ |
||
| 276 | 'id' => $this->document->getUid(), |
||
| 277 | 'page' => (!empty($details['points']) ? intval($details['points']) : 1), |
||
| 278 | 'targetPid' => (!empty($this->settings['targetPid']) ? $this->settings['targetPid'] : 0), |
||
| 279 | ]; |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | return $buildUrl; |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Builds external URLs array for given metadata array. |
||
| 288 | * |
||
| 289 | * @access private |
||
| 290 | * |
||
| 291 | * @param array $metadata The metadata array |
||
| 292 | * |
||
| 293 | * @return array external URLs |
||
| 294 | */ |
||
| 295 | private function buildExternalUrlFromMetadata(array $metadata) |
||
| 296 | { |
||
| 297 | $externalUrl = []; |
||
| 298 | |||
| 299 | foreach ($metadata as $i => $section) { |
||
| 300 | foreach ($section as $name => $value) { |
||
| 301 | if (($name == 'author' || $name == 'holder') && !empty($value) && !empty($value[0]['url'])) { |
||
| 302 | $externalUrl[$i][$name]['externalUrl'] = $value[0]; |
||
| 303 | } elseif (($name == 'geonames' || $name == 'wikidata' || $name == 'wikipedia') && !empty($value)) { |
||
| 304 | $externalUrl[$i][$name]['externalUrl'] = [ |
||
| 305 | 'name' => $value[0], |
||
| 306 | 'url' => $value[0] |
||
| 307 | ]; |
||
| 308 | } |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | return $externalUrl; |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Parses metadata. |
||
| 317 | * |
||
| 318 | * @access private |
||
| 319 | * |
||
| 320 | * @param int $i The index of metadata array |
||
| 321 | * @param string $name The name of section in metadata array |
||
| 322 | * @param mixed $value The value of section in metadata array |
||
| 323 | * @param array $metadata The metadata array passed as reference |
||
| 324 | * |
||
| 325 | * @return void |
||
| 326 | */ |
||
| 327 | private function parseMetadata(int $i, string $name, $value, array &$metadata) : void { |
||
| 362 | } |
||
| 363 | } |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Get metadata for given id array. |
||
| 368 | * |
||
| 369 | * @access private |
||
| 370 | * |
||
| 371 | * @return array metadata |
||
| 372 | */ |
||
| 373 | private function getMetadata() |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Get metadata for given id array. |
||
| 404 | * |
||
| 405 | * @access private |
||
| 406 | * |
||
| 407 | * @param array $id: An array with ids |
||
| 408 | * @param array $metadata: An array with metadata |
||
| 409 | * |
||
| 410 | * @return array metadata |
||
| 411 | */ |
||
| 412 | private function getMetadataForIds($id, $metadata) |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Sets default value for setting if not yet set. |
||
| 431 | * |
||
| 432 | * @access private |
||
| 433 | * |
||
| 434 | * @param string $setting name of setting |
||
| 435 | * @param int $value 0 or 1 |
||
| 436 | * |
||
| 437 | * @return void |
||
| 438 | */ |
||
| 439 | private function setDefault($setting, $value) { |
||
| 442 | } |
||
| 443 | } |
||
| 444 | } |
||
| 445 |