We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 47 |
| Total Lines | 223 |
| 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 |
||
| 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) |
||
| 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:name/mods:nameIdentifier[@type="orcid"]'); |
||
| 86 | if (!empty((string) $identifier[0])) { |
||
| 87 | $this->metadata['author'][$i] = $profile->getFullName(); |
||
|
|
|||
| 88 | } else { |
||
| 89 | // Check if there is a display form. |
||
| 90 | if (($displayForm = $authors[$i]->xpath('./mods:displayForm'))) { |
||
| 91 | $this->metadata['author'][$i] = (string) $displayForm[0]; |
||
| 92 | } elseif (($nameParts = $authors[$i]->xpath('./mods:namePart'))) { |
||
| 93 | $name = []; |
||
| 94 | $k = 4; |
||
| 95 | foreach ($nameParts as $namePart) { |
||
| 96 | if ( |
||
| 97 | isset($namePart['type']) |
||
| 98 | && (string) $namePart['type'] == 'family' |
||
| 99 | ) { |
||
| 100 | $name[0] = (string) $namePart; |
||
| 101 | } elseif ( |
||
| 102 | isset($namePart['type']) |
||
| 103 | && (string) $namePart['type'] == 'given' |
||
| 104 | ) { |
||
| 105 | $name[1] = (string) $namePart; |
||
| 106 | } elseif ( |
||
| 107 | isset($namePart['type']) |
||
| 108 | && (string) $namePart['type'] == 'termsOfAddress' |
||
| 109 | ) { |
||
| 110 | $name[2] = (string) $namePart; |
||
| 111 | } elseif ( |
||
| 112 | isset($namePart['type']) |
||
| 113 | && (string) $namePart['type'] == 'date' |
||
| 114 | ) { |
||
| 115 | $name[3] = (string) $namePart; |
||
| 116 | } else { |
||
| 117 | $name[$k] = (string) $namePart; |
||
| 118 | } |
||
| 119 | $k++; |
||
| 120 | } |
||
| 121 | ksort($name); |
||
| 122 | $this->metadata['author'][$i] = trim(implode(', ', $name)); |
||
| 123 | } |
||
| 124 | // Append "valueURI" to name using Unicode unit separator. |
||
| 125 | if (isset($authors[$i]['valueURI'])) { |
||
| 126 | $this->metadata['author'][$i] .= chr(31) . (string) $authors[$i]['valueURI']; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Get holder. |
||
| 135 | * |
||
| 136 | * @access private |
||
| 137 | * |
||
| 138 | * @return void |
||
| 139 | */ |
||
| 140 | private function getHolders() { |
||
| 141 | $holders = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="prv"]'); |
||
| 142 | |||
| 143 | if (!empty($holders)) { |
||
| 144 | for ($i = 0, $j = count($holders); $i < $j; $i++) { |
||
| 145 | $holders[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3'); |
||
| 146 | |||
| 147 | //$identifier = $holders[$i]->xpath('./mods:nameIdentifier'); |
||
| 148 | if (1 != 1) { |
||
| 149 | //TODO: reading VIAF API |
||
| 150 | } else { |
||
| 151 | // Check if there is a display form. |
||
| 152 | if (($displayForm = $holders[$i]->xpath('./mods:displayForm'))) { |
||
| 153 | $this->metadata['holder'][$i] = (string) $displayForm[0]; |
||
| 154 | } elseif (($nameParts = $holders[$i]->xpath('./mods:namePart'))) { |
||
| 155 | $name = []; |
||
| 156 | $k = 4; |
||
| 157 | foreach ($nameParts as $namePart) { |
||
| 158 | if ( |
||
| 159 | isset($namePart['type']) |
||
| 160 | && (string) $namePart['type'] == 'family' |
||
| 161 | ) { |
||
| 162 | $name[0] = (string) $namePart; |
||
| 163 | } elseif ( |
||
| 164 | isset($namePart['type']) |
||
| 165 | && (string) $namePart['type'] == 'given' |
||
| 166 | ) { |
||
| 167 | $name[1] = (string) $namePart; |
||
| 168 | } elseif ( |
||
| 169 | isset($namePart['type']) |
||
| 170 | && (string) $namePart['type'] == 'termsOfAddress' |
||
| 171 | ) { |
||
| 172 | $name[2] = (string) $namePart; |
||
| 173 | } elseif ( |
||
| 174 | isset($namePart['type']) |
||
| 175 | && (string) $namePart['type'] == 'date' |
||
| 176 | ) { |
||
| 177 | $name[3] = (string) $namePart; |
||
| 178 | } else { |
||
| 179 | $name[$k] = (string) $namePart; |
||
| 180 | } |
||
| 181 | $k++; |
||
| 182 | } |
||
| 183 | ksort($name); |
||
| 184 | $this->metadata['holder'][$i] = trim(implode(', ', $name)); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | } |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get "place" and "place_sorting". |
||
| 193 | * |
||
| 194 | * @access private |
||
| 195 | * |
||
| 196 | * @return void |
||
| 197 | */ |
||
| 198 | private function getPlaces() { |
||
| 210 | } |
||
| 211 | } |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Get "year" and "year_sorting". |
||
| 217 | * |
||
| 218 | * @access private |
||
| 219 | * |
||
| 220 | * @return void |
||
| 221 | */ |
||
| 222 | private function getYears() { |
||
| 248 | } |
||
| 249 | } |
||
| 250 | } |
||
| 253 |