| Total Complexity | 47 |
| Total Lines | 227 |
| Duplicated Lines | 0 % |
| Changes | 8 | ||
| 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) |
||
| 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:name/mods:nameIdentifier[@type="orcid"]'); |
||
| 87 | if (!empty((string) $identifier[0])) { |
||
| 88 | $profile = new OrcidProfile((string) $identifier[0]); |
||
| 89 | $this->metadata['author'][$i] = $profile->getFullName(); |
||
| 90 | } else { |
||
| 91 | // Check if there is a display form. |
||
| 92 | if (($displayForm = $authors[$i]->xpath('./mods:displayForm'))) { |
||
| 93 | $this->metadata['author'][$i] = (string) $displayForm[0]; |
||
| 94 | } elseif (($nameParts = $authors[$i]->xpath('./mods:namePart'))) { |
||
| 95 | $name = []; |
||
| 96 | $k = 4; |
||
| 97 | foreach ($nameParts as $namePart) { |
||
| 98 | if ( |
||
| 99 | isset($namePart['type']) |
||
| 100 | && (string) $namePart['type'] == 'family' |
||
| 101 | ) { |
||
| 102 | $name[0] = (string) $namePart; |
||
| 103 | } elseif ( |
||
| 104 | isset($namePart['type']) |
||
| 105 | && (string) $namePart['type'] == 'given' |
||
| 106 | ) { |
||
| 107 | $name[1] = (string) $namePart; |
||
| 108 | } elseif ( |
||
| 109 | isset($namePart['type']) |
||
| 110 | && (string) $namePart['type'] == 'termsOfAddress' |
||
| 111 | ) { |
||
| 112 | $name[2] = (string) $namePart; |
||
| 113 | } elseif ( |
||
| 114 | isset($namePart['type']) |
||
| 115 | && (string) $namePart['type'] == 'date' |
||
| 116 | ) { |
||
| 117 | $name[3] = (string) $namePart; |
||
| 118 | } else { |
||
| 119 | $name[$k] = (string) $namePart; |
||
| 120 | } |
||
| 121 | $k++; |
||
| 122 | } |
||
| 123 | ksort($name); |
||
| 124 | $this->metadata['author'][$i] = trim(implode(', ', $name)); |
||
| 125 | } |
||
| 126 | // Append "valueURI" to name using Unicode unit separator. |
||
| 127 | if (isset($authors[$i]['valueURI'])) { |
||
| 128 | $this->metadata['author'][$i] .= chr(31) . (string) $authors[$i]['valueURI']; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get holder. |
||
| 137 | * |
||
| 138 | * @access private |
||
| 139 | * |
||
| 140 | * @return void |
||
| 141 | */ |
||
| 142 | private function getHolders() { |
||
| 143 | $holders = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="prv"]'); |
||
| 144 | |||
| 145 | if (!empty($holders)) { |
||
| 146 | for ($i = 0, $j = count($holders); $i < $j; $i++) { |
||
| 147 | $holders[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3'); |
||
| 148 | |||
| 149 | $identifier = $holders[$i]->xpath('./mods:name/mods:nameIdentifier[@type="viaf"]'); |
||
| 150 | if (!empty((string) $identifier[0])) { |
||
| 151 | $viaf = (string) $identifier[0]; |
||
| 152 | $profile = new ViafProfile($viaf); |
||
| 153 | $this->metadata['holder'][$i] = $profile->getFullName(); |
||
| 154 | } else { |
||
| 155 | // Check if there is a display form. |
||
| 156 | if (($displayForm = $holders[$i]->xpath('./mods:displayForm'))) { |
||
| 157 | $this->metadata['holder'][$i] = (string) $displayForm[0]; |
||
| 158 | } elseif (($nameParts = $holders[$i]->xpath('./mods:namePart'))) { |
||
| 159 | $name = []; |
||
| 160 | $k = 4; |
||
| 161 | foreach ($nameParts as $namePart) { |
||
| 162 | if ( |
||
| 163 | isset($namePart['type']) |
||
| 164 | && (string) $namePart['type'] == 'family' |
||
| 165 | ) { |
||
| 166 | $name[0] = (string) $namePart; |
||
| 167 | } elseif ( |
||
| 168 | isset($namePart['type']) |
||
| 169 | && (string) $namePart['type'] == 'given' |
||
| 170 | ) { |
||
| 171 | $name[1] = (string) $namePart; |
||
| 172 | } elseif ( |
||
| 173 | isset($namePart['type']) |
||
| 174 | && (string) $namePart['type'] == 'termsOfAddress' |
||
| 175 | ) { |
||
| 176 | $name[2] = (string) $namePart; |
||
| 177 | } elseif ( |
||
| 178 | isset($namePart['type']) |
||
| 179 | && (string) $namePart['type'] == 'date' |
||
| 180 | ) { |
||
| 181 | $name[3] = (string) $namePart; |
||
| 182 | } else { |
||
| 183 | $name[$k] = (string) $namePart; |
||
| 184 | } |
||
| 185 | $k++; |
||
| 186 | } |
||
| 187 | $k++; |
||
| 188 | } |
||
| 189 | ksort($name); |
||
| 190 | $this->metadata['holder'][$i] = trim(implode(', ', $name)); |
||
| 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 | } |
||
| 258 |