We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 49 |
| Total Lines | 254 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| 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) |
||
| 53 | { |
||
| 54 | $this->xml = $xml; |
||
| 55 | $this->metadata = $metadata; |
||
| 56 | |||
| 57 | $this->xml->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3'); |
||
| 58 | |||
| 59 | $this->getAuthors(); |
||
| 60 | $this->getHolders(); |
||
| 61 | $this->getPlaces(); |
||
| 62 | $this->getYears(); |
||
| 63 | |||
| 64 | $metadata = $this->metadata; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Get "author" and "author_sorting". |
||
| 69 | * |
||
| 70 | * @access private |
||
| 71 | * |
||
| 72 | * @return void |
||
| 73 | */ |
||
| 74 | private function getAuthors() { |
||
| 75 | $authors = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="aut"]'); |
||
| 76 | |||
| 77 | // Get "author" and "author_sorting" again if that was too sophisticated. |
||
| 78 | if (empty($authors)) { |
||
| 79 | // Get all names which do not have any role term assigned and assume these are authors. |
||
| 80 | $authors = $this->xml->xpath('./mods:name[not(./mods:role)]'); |
||
| 81 | } |
||
| 82 | if (!empty($authors)) { |
||
| 83 | for ($i = 0, $j = count($authors); $i < $j; $i++) { |
||
| 84 | $authors[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3'); |
||
| 85 | |||
| 86 | $identifier = $authors[$i]->xpath('./mods:nameIdentifier'); |
||
| 87 | if ($identifier[0]['type'] == 'orcid' && !empty((string) $identifier[0])) { |
||
| 88 | $this->getAuthorFromOrcidApi($identifier, $authors, $i); |
||
| 89 | } else { |
||
| 90 | $this->getAuthorFromXml($authors, $i); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | private function getAuthorFromOrcidApi($identifier, $authors, $i) { |
||
| 97 | $orcidUrl = (string) $identifier[0]; |
||
| 98 | $orcidIdParts = explode('/', $orcidUrl); |
||
| 99 | $orcidId = trim(end($orcidIdParts)); |
||
| 100 | if (!str_contains($orcidId, 'orcid=')) { |
||
| 101 | $profile = new OrcidProfile($orcidId); |
||
| 102 | $name = $profile->getFullName(); |
||
| 103 | if (!empty($name)) { |
||
| 104 | $this->metadata['author'][$i] = [ |
||
| 105 | 'name' => $name, |
||
| 106 | 'url' => $orcidUrl |
||
| 107 | ]; |
||
| 108 | } else { |
||
| 109 | //fallback into display form |
||
| 110 | $this->getAuthorFromXmlDisplayForm($authors, $i); |
||
| 111 | } |
||
| 112 | } else { |
||
| 113 | //fallback into display form |
||
| 114 | $this->getAuthorFromXmlDisplayForm($authors, $i); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | private function getAuthorFromXml($authors, $i) { |
||
| 119 | $this->getAuthorFromXmlDisplayForm($authors, $i); |
||
| 120 | |||
| 121 | $nameParts = $authors[$i]->xpath('./mods:namePart'); |
||
| 122 | |||
| 123 | if (empty($this->metadata['author'][$i]) && $nameParts) { |
||
| 124 | $name = []; |
||
| 125 | $k = 4; |
||
| 126 | foreach ($nameParts as $namePart) { |
||
| 127 | if ( |
||
| 128 | isset($namePart['type']) |
||
| 129 | && (string) $namePart['type'] == 'family' |
||
| 130 | ) { |
||
| 131 | $name[0] = (string) $namePart; |
||
| 132 | } elseif ( |
||
| 133 | isset($namePart['type']) |
||
| 134 | && (string) $namePart['type'] == 'given' |
||
| 135 | ) { |
||
| 136 | $name[1] = (string) $namePart; |
||
| 137 | } elseif ( |
||
| 138 | isset($namePart['type']) |
||
| 139 | && (string) $namePart['type'] == 'termsOfAddress' |
||
| 140 | ) { |
||
| 141 | $name[2] = (string) $namePart; |
||
| 142 | } elseif ( |
||
| 143 | isset($namePart['type']) |
||
| 144 | && (string) $namePart['type'] == 'date' |
||
| 145 | ) { |
||
| 146 | $name[3] = (string) $namePart; |
||
| 147 | } else { |
||
| 148 | $name[$k] = (string) $namePart; |
||
| 149 | } |
||
| 150 | $k++; |
||
| 151 | } |
||
| 152 | ksort($name); |
||
| 153 | $this->metadata['author'][$i] = trim(implode(', ', $name)); |
||
| 154 | } |
||
| 155 | // Append "valueURI" to name using Unicode unit separator. |
||
| 156 | if (isset($authors[$i]['valueURI'])) { |
||
| 157 | $this->metadata['author'][$i] .= chr(31) . (string) $authors[$i]['valueURI']; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | private function getAuthorFromXmlDisplayForm($authors, $i) { |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Get holder. |
||
| 170 | * |
||
| 171 | * @access private |
||
| 172 | * |
||
| 173 | * @return void |
||
| 174 | */ |
||
| 175 | private function getHolders() { |
||
| 176 | $holders = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="prv"]'); |
||
| 177 | |||
| 178 | if (!empty($holders)) { |
||
| 179 | for ($i = 0, $j = count($holders); $i < $j; $i++) { |
||
| 180 | $holders[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3'); |
||
| 181 | |||
| 182 | $identifier = $holders[$i]->xpath('./mods:nameIdentifier'); |
||
| 183 | if ($identifier[0]['type'] == 'viaf') { |
||
| 184 | $this->getHolderFromViafApi($identifier, $holders, $i); |
||
| 185 | } else { |
||
| 186 | $this->getHolderFromXml($holders, $i); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | private function getHolderFromViafApi($identifier, $holders, $i) { |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | private function getHolderFromXml($holders, $i) { |
||
| 208 | $this->getHolderFromXmlDisplayForm($holders, $i); |
||
| 209 | // Append "valueURI" to name using Unicode unit separator. |
||
| 210 | if (isset($holders[$i]['valueURI'])) { |
||
| 211 | $this->metadata['holder'][$i] .= chr(31) . (string) $holders[$i]['valueURI']; |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | private function getHolderFromXmlDisplayForm($holders, $i) { |
||
| 216 | // Check if there is a display form. |
||
| 217 | $displayForm = $holders[$i]->xpath('./mods:displayForm'); |
||
| 218 | if ($displayForm) { |
||
| 219 | $this->metadata['holder'][$i] = (string) $displayForm[0]; |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Get "place" and "place_sorting". |
||
| 225 | * |
||
| 226 | * @access private |
||
| 227 | * |
||
| 228 | * @return void |
||
| 229 | */ |
||
| 230 | private function getPlaces() { |
||
| 242 | } |
||
| 243 | } |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Get "year" and "year_sorting". |
||
| 249 | * |
||
| 250 | * @access private |
||
| 251 | * |
||
| 252 | * @return void |
||
| 253 | */ |
||
| 254 | private function getYears() { |
||
| 280 | } |
||
| 281 | } |
||
| 282 | } |
||
| 283 | } |
||
| 284 | } |
||
| 285 |