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