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 | 228 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| 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) |
||
| 52 | { |
||
| 53 | $this->xml = $xml; |
||
| 54 | $this->metadata = $metadata; |
||
| 55 | |||
| 56 | $this->xml->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3'); |
||
| 57 | |||
| 58 | $this->getAuthors(); |
||
| 59 | $this->getHolders(); |
||
| 60 | $this->getPlaces(); |
||
| 61 | $this->getYears(); |
||
| 62 | |||
| 63 | $metadata = $this->metadata; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Get "author" and "author_sorting". |
||
| 68 | * |
||
| 69 | * @access private |
||
| 70 | * |
||
| 71 | * @return void |
||
| 72 | */ |
||
| 73 | private function getAuthors() { |
||
| 74 | $authors = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="aut"]'); |
||
| 75 | |||
| 76 | // Get "author" and "author_sorting" again if that was too sophisticated. |
||
| 77 | if (empty($authors)) { |
||
| 78 | // Get all names which do not have any role term assigned and assume these are authors. |
||
| 79 | $authors = $this->xml->xpath('./mods:name[not(./mods:role)]'); |
||
| 80 | } |
||
| 81 | if (!empty($authors)) { |
||
| 82 | for ($i = 0, $j = count($authors); $i < $j; $i++) { |
||
| 83 | $authors[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3'); |
||
| 84 | |||
| 85 | $identifier = $authors[$i]->xpath('./mods:nameIdentifier'); |
||
| 86 | if ($identifier[0]['type'] == 'orcid' && !empty((string) $identifier[0])) { |
||
| 87 | $orcidIdParts = explode('/', (string) $identifier[0]); |
||
| 88 | $orcidId = trim(end($orcidIdParts)); |
||
| 89 | if (!str_contains($orcidId, 'orcid=')) { |
||
| 90 | $profile = new Profile($orcidId); |
||
| 91 | $this->metadata['author'][$i] = $profile->getFullName(); |
||
| 92 | } |
||
| 93 | } else { |
||
| 94 | // Check if there is a display form. |
||
| 95 | if (($displayForm = $authors[$i]->xpath('./mods:displayForm'))) { |
||
| 96 | $this->metadata['author'][$i] = (string) $displayForm[0]; |
||
| 97 | } elseif (($nameParts = $authors[$i]->xpath('./mods:namePart'))) { |
||
| 98 | $name = []; |
||
| 99 | $k = 4; |
||
| 100 | foreach ($nameParts as $namePart) { |
||
| 101 | if ( |
||
| 102 | isset($namePart['type']) |
||
| 103 | && (string) $namePart['type'] == 'family' |
||
| 104 | ) { |
||
| 105 | $name[0] = (string) $namePart; |
||
| 106 | } elseif ( |
||
| 107 | isset($namePart['type']) |
||
| 108 | && (string) $namePart['type'] == 'given' |
||
| 109 | ) { |
||
| 110 | $name[1] = (string) $namePart; |
||
| 111 | } elseif ( |
||
| 112 | isset($namePart['type']) |
||
| 113 | && (string) $namePart['type'] == 'termsOfAddress' |
||
| 114 | ) { |
||
| 115 | $name[2] = (string) $namePart; |
||
| 116 | } elseif ( |
||
| 117 | isset($namePart['type']) |
||
| 118 | && (string) $namePart['type'] == 'date' |
||
| 119 | ) { |
||
| 120 | $name[3] = (string) $namePart; |
||
| 121 | } else { |
||
| 122 | $name[$k] = (string) $namePart; |
||
| 123 | } |
||
| 124 | $k++; |
||
| 125 | } |
||
| 126 | ksort($name); |
||
| 127 | $this->metadata['author'][$i] = trim(implode(', ', $name)); |
||
| 128 | } |
||
| 129 | // Append "valueURI" to name using Unicode unit separator. |
||
| 130 | if (isset($authors[$i]['valueURI'])) { |
||
| 131 | $this->metadata['author'][$i] .= chr(31) . (string) $authors[$i]['valueURI']; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | } |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get holder. |
||
| 140 | * |
||
| 141 | * @access private |
||
| 142 | * |
||
| 143 | * @return void |
||
| 144 | */ |
||
| 145 | private function getHolders() { |
||
| 146 | $holders = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="prv"]'); |
||
| 147 | |||
| 148 | if (!empty($holders)) { |
||
| 149 | for ($i = 0, $j = count($holders); $i < $j; $i++) { |
||
| 150 | $holders[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3'); |
||
| 151 | |||
| 152 | //$identifier = $holders[$i]->xpath('./mods:nameIdentifier'); |
||
| 153 | if (1 != 1) { |
||
| 154 | //TODO: reading VIAF API |
||
| 155 | } else { |
||
| 156 | // Check if there is a display form. |
||
| 157 | if (($displayForm = $holders[$i]->xpath('./mods:displayForm'))) { |
||
| 158 | $this->metadata['holder'][$i] = (string) $displayForm[0]; |
||
| 159 | } elseif (($nameParts = $holders[$i]->xpath('./mods:namePart'))) { |
||
| 160 | $name = []; |
||
| 161 | $k = 4; |
||
| 162 | foreach ($nameParts as $namePart) { |
||
| 163 | if ( |
||
| 164 | isset($namePart['type']) |
||
| 165 | && (string) $namePart['type'] == 'family' |
||
| 166 | ) { |
||
| 167 | $name[0] = (string) $namePart; |
||
| 168 | } elseif ( |
||
| 169 | isset($namePart['type']) |
||
| 170 | && (string) $namePart['type'] == 'given' |
||
| 171 | ) { |
||
| 172 | $name[1] = (string) $namePart; |
||
| 173 | } elseif ( |
||
| 174 | isset($namePart['type']) |
||
| 175 | && (string) $namePart['type'] == 'termsOfAddress' |
||
| 176 | ) { |
||
| 177 | $name[2] = (string) $namePart; |
||
| 178 | } elseif ( |
||
| 179 | isset($namePart['type']) |
||
| 180 | && (string) $namePart['type'] == 'date' |
||
| 181 | ) { |
||
| 182 | $name[3] = (string) $namePart; |
||
| 183 | } else { |
||
| 184 | $name[$k] = (string) $namePart; |
||
| 185 | } |
||
| 186 | $k++; |
||
| 187 | } |
||
| 188 | ksort($name); |
||
| 189 | $this->metadata['holder'][$i] = trim(implode(', ', $name)); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | } |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Get "place" and "place_sorting". |
||
| 198 | * |
||
| 199 | * @access private |
||
| 200 | * |
||
| 201 | * @return void |
||
| 202 | */ |
||
| 203 | private function getPlaces() { |
||
| 215 | } |
||
| 216 | } |
||
| 217 | } |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Get "year" and "year_sorting". |
||
| 222 | * |
||
| 223 | * @access private |
||
| 224 | * |
||
| 225 | * @return void |
||
| 226 | */ |
||
| 227 | private function getYears() { |
||
| 253 | } |
||
| 254 | } |
||
| 255 | } |
||
| 256 | } |
||
| 258 |