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