We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 57 |
| Total Lines | 344 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Mods 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 Mods, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Mods implements \Kitodo\Dlf\Common\MetadataInterface |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * The metadata XML |
||
| 30 | * |
||
| 31 | * @var \SimpleXMLElement |
||
| 32 | **/ |
||
| 33 | private $xml; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The metadata array |
||
| 37 | * |
||
| 38 | * @var array |
||
| 39 | **/ |
||
| 40 | private $metadata; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * This extracts the essential MODS metadata from XML |
||
| 44 | * |
||
| 45 | * @access public |
||
| 46 | * |
||
| 47 | * @param \SimpleXMLElement $xml: The XML to extract the metadata from |
||
| 48 | * @param array &$metadata: The metadata array to fill |
||
| 49 | * |
||
| 50 | * @return void |
||
| 51 | */ |
||
| 52 | public function extractMetadata(\SimpleXMLElement $xml, array &$metadata) |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Get "author" and "author_sorting". |
||
| 74 | * |
||
| 75 | * @access private |
||
| 76 | * |
||
| 77 | * @return void |
||
| 78 | */ |
||
| 79 | private function getAuthors() { |
||
| 80 | $authors = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="aut"]'); |
||
| 81 | |||
| 82 | // Get "author" and "author_sorting" again if that was too sophisticated. |
||
| 83 | if (empty($authors)) { |
||
| 84 | // Get all names which do not have any role term assigned and assume these are authors. |
||
| 85 | $authors = $this->xml->xpath('./mods:name[not(./mods:role)]'); |
||
| 86 | } |
||
| 87 | |||
| 88 | if (!empty($authors)) { |
||
| 89 | for ($i = 0, $j = count($authors); $i < $j; $i++) { |
||
| 90 | $authors[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3'); |
||
| 91 | |||
| 92 | $identifier = $authors[$i]->xpath('./mods:nameIdentifier'); |
||
| 93 | if ($identifier[0]['type'] == 'orcid' && !empty((string) $identifier[0])) { |
||
| 94 | $this->getAuthorsFromOrcidApi($identifier, $authors, $i); |
||
| 95 | } else { |
||
| 96 | $this->getAuthorsFromXml($authors, $i); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | private function getAuthorsFromOrcidApi($identifier, $authors, $i) { |
||
| 103 | $orcidUrl = (string) $identifier[0]; |
||
| 104 | $orcidIdParts = explode('/', $orcidUrl); |
||
| 105 | $orcidId = trim(end($orcidIdParts)); |
||
| 106 | if (!str_contains($orcidId, 'orcid=')) { |
||
| 107 | $profile = new OrcidProfile($orcidId); |
||
| 108 | $name = $profile->getFullName(); |
||
| 109 | if (!empty($name)) { |
||
| 110 | $this->metadata['author'][$i] = [ |
||
| 111 | 'name' => $name, |
||
| 112 | 'url' => $orcidUrl |
||
| 113 | ]; |
||
| 114 | } else { |
||
| 115 | //fallback into display form |
||
| 116 | $this->getAuthorFromXmlDisplayForm($authors, $i); |
||
| 117 | } |
||
| 118 | } else { |
||
| 119 | //fallback into display form |
||
| 120 | $this->getAuthorFromXmlDisplayForm($authors, $i); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | private function getAuthorsFromXml($authors, $i) { |
||
| 125 | $this->getAuthorFromXmlDisplayForm($authors, $i); |
||
| 126 | |||
| 127 | $nameParts = $authors[$i]->xpath('./mods:namePart'); |
||
| 128 | |||
| 129 | if (empty($this->metadata['author'][$i]) && $nameParts) { |
||
| 130 | $name = []; |
||
| 131 | $k = 4; |
||
| 132 | foreach ($nameParts as $namePart) { |
||
| 133 | if ( |
||
| 134 | isset($namePart['type']) |
||
| 135 | && (string) $namePart['type'] == 'family' |
||
| 136 | ) { |
||
| 137 | $name[0] = (string) $namePart; |
||
| 138 | } elseif ( |
||
| 139 | isset($namePart['type']) |
||
| 140 | && (string) $namePart['type'] == 'given' |
||
| 141 | ) { |
||
| 142 | $name[1] = (string) $namePart; |
||
| 143 | } elseif ( |
||
| 144 | isset($namePart['type']) |
||
| 145 | && (string) $namePart['type'] == 'termsOfAddress' |
||
| 146 | ) { |
||
| 147 | $name[2] = (string) $namePart; |
||
| 148 | } elseif ( |
||
| 149 | isset($namePart['type']) |
||
| 150 | && (string) $namePart['type'] == 'date' |
||
| 151 | ) { |
||
| 152 | $name[3] = (string) $namePart; |
||
| 153 | } else { |
||
| 154 | $name[$k] = (string) $namePart; |
||
| 155 | } |
||
| 156 | $k++; |
||
| 157 | } |
||
| 158 | ksort($name); |
||
| 159 | $this->metadata['author'][$i] = trim(implode(', ', $name)); |
||
| 160 | } |
||
| 161 | // Append "valueURI" to name using Unicode unit separator. |
||
| 162 | if (isset($authors[$i]['valueURI'])) { |
||
| 163 | $this->metadata['author'][$i] .= chr(31) . (string) $authors[$i]['valueURI']; |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | private function getAuthorFromXmlDisplayForm($authors, $i) { |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Get holder. |
||
| 176 | * |
||
| 177 | * @access private |
||
| 178 | * |
||
| 179 | * @return void |
||
| 180 | */ |
||
| 181 | private function getHolders() { |
||
| 182 | $holders = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="prv"]'); |
||
| 183 | |||
| 184 | if (!empty($holders)) { |
||
| 185 | for ($i = 0, $j = count($holders); $i < $j; $i++) { |
||
| 186 | $holders[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3'); |
||
| 187 | |||
| 188 | $identifier = $holders[$i]->xpath('./mods:nameIdentifier'); |
||
| 189 | if ($identifier[0]['type'] == 'viaf') { |
||
| 190 | $this->getHoldersFromViafApi($identifier,$holders, $i); |
||
| 191 | } else { |
||
| 192 | $this->getHoldersFromXml($holders, $i); |
||
| 193 | } |
||
| 194 | } |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | private function getHoldersFromViafApi($identifier, $holders, $i) { |
||
| 199 | $viafUrl = (string) $identifier[0]; |
||
| 200 | $profile = new ViafProfile($viafUrl); |
||
| 201 | $name = $profile->getFullName(); |
||
| 202 | if (!empty($name)) { |
||
| 203 | $this->metadata['holder'][$i] = [ |
||
| 204 | 'name' => $name, |
||
| 205 | 'url' => $viafUrl |
||
| 206 | ]; |
||
| 207 | } else { |
||
| 208 | //fallback into display form |
||
| 209 | $displayForm = $holders[$i]->xpath('./mods:displayForm'); |
||
| 210 | if ($displayForm) { |
||
| 211 | $this->metadata['holder'][$i] = (string) $displayForm[0]; |
||
| 212 | } |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | private function getHoldersFromXml($holders, $i) { |
||
| 217 | // Check if there is a display form. |
||
| 218 | $displayForm = $holders[$i]->xpath('./mods:displayForm'); |
||
| 219 | if ($displayForm) { |
||
| 220 | $this->metadata['holder'][$i] = (string) $displayForm[0]; |
||
| 221 | } |
||
| 222 | // Append "valueURI" to name using Unicode unit separator. |
||
| 223 | if (isset($holders[$i]['valueURI'])) { |
||
| 224 | $this->metadata['holder'][$i] .= chr(31) . (string) $holders[$i]['valueURI']; |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get "place" and "place_sorting". |
||
| 230 | * |
||
| 231 | * @access private |
||
| 232 | * |
||
| 233 | * @return void |
||
| 234 | */ |
||
| 235 | private function getPlaces() { |
||
| 236 | $places = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:place/mods:placeTerm'); |
||
| 237 | // Get "place" and "place_sorting" again if that was to sophisticated. |
||
| 238 | if (empty($places)) { |
||
| 239 | // Get all places and assume these are places of publication. |
||
| 240 | $places = $this->xml->xpath('./mods:originInfo/mods:place/mods:placeTerm'); |
||
| 241 | } |
||
| 242 | if (!empty($places)) { |
||
| 243 | foreach ($places as $place) { |
||
| 244 | $this->metadata['place'][] = (string) $place; |
||
| 245 | if (!$this->metadata['place_sorting'][0]) { |
||
| 246 | $this->metadata['place_sorting'][0] = preg_replace('/[[:punct:]]/', '', (string) $place); |
||
| 247 | } |
||
| 248 | } |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Get "year" and "year_sorting". |
||
| 254 | * |
||
| 255 | * @access private |
||
| 256 | * |
||
| 257 | * @return void |
||
| 258 | */ |
||
| 259 | private function getYears() { |
||
| 260 | // Get "year_sorting". |
||
| 261 | if (($years_sorting = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:dateOther[@type="order" and @encoding="w3cdtf"]'))) { |
||
| 262 | foreach ($years_sorting as $year_sorting) { |
||
| 263 | $this->metadata['year_sorting'][0] = intval($year_sorting); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | // Get "year" and "year_sorting" if not specified separately. |
||
| 267 | $years = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:dateIssued[@keyDate="yes"]'); |
||
| 268 | // Get "year" and "year_sorting" again if that was to sophisticated. |
||
| 269 | if (empty($years)) { |
||
| 270 | // Get all dates and assume these are dates of publication. |
||
| 271 | $years = $this->xml->xpath('./mods:originInfo/mods:dateIssued'); |
||
| 272 | } |
||
| 273 | if (!empty($years)) { |
||
| 274 | foreach ($years as $year) { |
||
| 275 | $this->metadata['year'][] = (string) $year; |
||
| 276 | if (!$this->metadata['year_sorting'][0]) { |
||
| 277 | $year_sorting = str_ireplace('x', '5', preg_replace('/[^\d.x]/i', '', (string) $year)); |
||
| 278 | if ( |
||
| 279 | strpos($year_sorting, '.') |
||
|
|
|||
| 280 | || strlen($year_sorting) < 3 |
||
| 281 | ) { |
||
| 282 | $year_sorting = ((intval(trim($year_sorting, '.')) - 1) * 100) + 50; |
||
| 283 | } |
||
| 284 | $this->metadata['year_sorting'][0] = intval($year_sorting); |
||
| 285 | } |
||
| 286 | } |
||
| 287 | } |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Get "description" stored in recordInfoNote. |
||
| 292 | * |
||
| 293 | * @access private |
||
| 294 | * |
||
| 295 | * @return void |
||
| 296 | */ |
||
| 297 | private function getDescription() { |
||
| 298 | $this->getSingleMetadata('description', './mods:recordInfo/mods:recordInfoNote/text()'); |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Get "identifier" - example: hotels (built public accommodations), AAT ID: 300007166. |
||
| 303 | * |
||
| 304 | * @access private |
||
| 305 | * |
||
| 306 | * @return void |
||
| 307 | */ |
||
| 308 | private function getIdentifier() { |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Get "license" - license on which object is published. |
||
| 314 | * |
||
| 315 | * @access private |
||
| 316 | * |
||
| 317 | * @return void |
||
| 318 | */ |
||
| 319 | private function getLicense() { |
||
| 320 | $this->getSingleMetadata('license', './mods:accessCondition/text()'); |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Get names of the original object: |
||
| 325 | * - main name |
||
| 326 | * - alternative names |
||
| 327 | * |
||
| 328 | * @access private |
||
| 329 | * |
||
| 330 | * @return void |
||
| 331 | */ |
||
| 332 | private function getObjectNames() { |
||
| 333 | $this->getSingleMetadata('object_name', './mods:relatedItem/mods:titleInfo[not(@displayLabel="alternative")]/mods:title/text()'); |
||
| 334 | $this->getSingleMetadata('object_alternative_names', './mods:relatedItem/mods:titleInfo[@displayLabel="alternative"]/mods:title/text()'); |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Get location information about the original object: |
||
| 339 | * - city (object_location) |
||
| 340 | * - geonames |
||
| 341 | * - wikidata |
||
| 342 | * - wikipedia |
||
| 343 | * |
||
| 344 | * @access private |
||
| 345 | * |
||
| 346 | * @return void |
||
| 347 | */ |
||
| 348 | private function getObjectLocationMetadata() { |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Save to the metadata array the last found matching metadata. |
||
| 357 | * |
||
| 358 | * @access private |
||
| 359 | * |
||
| 360 | * @param string $metadataIndex: The index in the array by which metadata is going to be accessible |
||
| 361 | * @param string $xpath: The xpath for searching metadata in MODS |
||
| 362 | * |
||
| 363 | * @return void |
||
| 364 | */ |
||
| 365 | private function getSingleMetadata($metadataIndex, $xpath) { |
||
| 370 | } |
||
| 371 | } |
||
| 372 | } |
||
| 373 | } |
||
| 374 |