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