| Total Complexity | 57 |
| Total Lines | 382 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 18 | class Mods |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * metadataObjectRepository |
||
| 22 | * |
||
| 23 | * @var \EWW\Dpf\Domain\Repository\MetadataObjectRepository |
||
| 24 | * @inject |
||
| 25 | */ |
||
| 26 | protected $metadataObjectRepository = null; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * metadatGroupRepository |
||
| 30 | * |
||
| 31 | * @var \EWW\Dpf\Domain\Repository\MetadataGroupRepository |
||
| 32 | * @inject |
||
| 33 | */ |
||
| 34 | protected $metadataGroupRepository = null; |
||
| 35 | |||
| 36 | |||
| 37 | protected $modsDom; |
||
| 38 | |||
| 39 | public function __construct($modsXml) |
||
| 42 | } |
||
| 43 | |||
| 44 | public function setModsXml($modsXml) |
||
| 45 | { |
||
| 46 | $modsDom = new \DOMDocument(); |
||
| 47 | if (!empty($modsXml)) { |
||
| 48 | |||
| 49 | $modsXml = preg_replace( |
||
| 50 | "/<mods:mods.*?>/", |
||
| 51 | '<mods:mods xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' |
||
| 52 | .'xmlns:mods="http://www.loc.gov/mods/v3" ' |
||
| 53 | .'xmlns:slub="http://slub-dresden.de/" ' |
||
| 54 | .'xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" ' |
||
| 55 | .'xmlns:foaf="http://xmlns.com/foaf/0.1/" ' |
||
| 56 | .'xmlns:person="http://www.w3.org/ns/person#" ' |
||
| 57 | .'xsi:schemaLocation="http://www.loc.gov/mods/v3 ' |
||
| 58 | .'http://www.loc.gov/standards/mods/v3/mods-3-7.xsd" version="3.7">', |
||
| 59 | $modsXml |
||
| 60 | ); |
||
| 61 | |||
| 62 | if (is_null(@$modsDom->loadXML($modsXml))) { |
||
| 63 | throw new \Exception("Couldn't load MODS data!"); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | $this->modsDom = $modsDom; |
||
| 67 | } |
||
| 68 | |||
| 69 | public function getModsXml() |
||
| 70 | { |
||
| 71 | return $this->modsDom->saveXML(); |
||
| 72 | } |
||
| 73 | |||
| 74 | public function getModsXpath() |
||
| 75 | { |
||
| 76 | $xpath = \EWW\Dpf\Helper\XPath::create($this->modsDom); |
||
| 77 | return $xpath; |
||
| 78 | } |
||
| 79 | |||
| 80 | public function getTitle() |
||
| 81 | { |
||
| 82 | $titleNode = $this->getModsXpath()->query('/data/titleInfo/title'); |
||
| 83 | |||
| 84 | if ($titleNode->length == 0) { |
||
| 85 | $titleNode = $this->getModsXpath()->query("/data/titleInfo/title"); |
||
| 86 | } |
||
| 87 | return $titleNode->item(0)->nodeValue; |
||
| 88 | } |
||
| 89 | |||
| 90 | |||
| 91 | public function setTitle($title) |
||
| 92 | { |
||
| 93 | $titleNode = $this->getModsXpath()->query('/mods:mods/mods:titleInfo[@usage="primary"]/mods:title'); |
||
| 94 | |||
| 95 | if ($titleNode->length == 0) { |
||
| 96 | $titleNode = $this->getModsXpath()->query("/mods:mods/mods:titleInfo/mods:title"); |
||
| 97 | } |
||
| 98 | |||
| 99 | if ($titleNode->length > 0) { |
||
| 100 | $titleNode->item(0)->nodeValue = $title; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | |||
| 105 | public function getAuthors() |
||
| 106 | { |
||
| 107 | return $this->getPersons("aut"); |
||
| 108 | } |
||
| 109 | |||
| 110 | public function getPublishers() |
||
| 111 | { |
||
| 112 | return $this->getPersons("edt"); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Get persons of the given role |
||
| 117 | * |
||
| 118 | * @param string $role |
||
| 119 | * @return array |
||
| 120 | */ |
||
| 121 | public function getPersons($role = '') |
||
| 122 | { |
||
| 123 | $xpath = $this->getModsXpath(); |
||
| 124 | $personNodes = $xpath->query('/mods:mods/mods:name[@type="personal"]'); |
||
| 125 | |||
| 126 | $persons = []; |
||
| 127 | |||
| 128 | foreach ($personNodes as $key => $personNode) { |
||
| 129 | |||
| 130 | $familyNode = $xpath->query('mods:namePart[@type="family"]', $personNode); |
||
| 131 | |||
| 132 | $givenNode = $xpath->query('mods:namePart[@type="given"]', $personNode); |
||
| 133 | |||
| 134 | $roleNode = $xpath->query('mods:role/mods:roleTerm[@type="code"]', $personNode); |
||
| 135 | |||
| 136 | $identifierNode = $xpath->query('mods:nameIdentifier[@type="FOBID"]', $personNode); |
||
| 137 | |||
| 138 | $affiliationNodes = $xpath->query('mods:affiliation', $personNode); |
||
| 139 | $affiliationIdentifierNodes = $xpath->query( |
||
| 140 | 'mods:nameIdentifier[@type="ScopusAuthorID"][@typeURI="http://www.scopus.com/authid"]', |
||
| 141 | $personNode |
||
| 142 | ); |
||
| 143 | |||
| 144 | $person['affiliations'] = []; |
||
| 145 | foreach ($affiliationNodes as $key => $affiliationNode) { |
||
|
|
|||
| 146 | $person['affiliations'][] = $affiliationNode->nodeValue; |
||
| 147 | } |
||
| 148 | |||
| 149 | $person['affiliationIdentifiers'] = []; |
||
| 150 | foreach ($affiliationIdentifierNodes as $key => $affiliationIdentifierNode) { |
||
| 151 | $person['affiliationIdentifiers'][] = $affiliationIdentifierNode->nodeValue; |
||
| 152 | } |
||
| 153 | |||
| 154 | $given = ''; |
||
| 155 | $family = ''; |
||
| 156 | |||
| 157 | if ($givenNode->length > 0) { |
||
| 158 | $given = $givenNode->item(0)->nodeValue; |
||
| 159 | } |
||
| 160 | |||
| 161 | if ($familyNode->length > 0) { |
||
| 162 | $family = $familyNode->item(0)->nodeValue; |
||
| 163 | } |
||
| 164 | |||
| 165 | $person['given'] = trim($given); |
||
| 166 | $person['family'] = trim($family); |
||
| 167 | |||
| 168 | $name = []; |
||
| 169 | if ($person['given']) { |
||
| 170 | $name[] = $person['given']; |
||
| 171 | } |
||
| 172 | if ($person['family']) { |
||
| 173 | $name[] = $person['family']; |
||
| 174 | } |
||
| 175 | |||
| 176 | $person['name'] = implode(' ', $name); |
||
| 177 | |||
| 178 | $person['role'] = ''; |
||
| 179 | if ($roleNode->length > 0) { |
||
| 180 | $person['role'] = $roleNode->item(0)->nodeValue; |
||
| 181 | } |
||
| 182 | |||
| 183 | $person['fobId'] = ''; |
||
| 184 | if ($identifierNode->length > 0) { |
||
| 185 | $person['fobId'] = $identifierNode->item(0)->nodeValue; |
||
| 186 | } |
||
| 187 | |||
| 188 | $person['index'] = $key; |
||
| 189 | |||
| 190 | $persons[] = $person; |
||
| 191 | } |
||
| 192 | |||
| 193 | if ($role) { |
||
| 194 | $result = []; |
||
| 195 | foreach ($persons as $person) { |
||
| 196 | if ($person['role'] == $role) |
||
| 197 | $result[] = $person; |
||
| 198 | } |
||
| 199 | return $result; |
||
| 200 | } else { |
||
| 201 | return $persons; |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | |||
| 206 | /** |
||
| 207 | * Get all related FOB-IDs |
||
| 208 | * |
||
| 209 | * @return array |
||
| 210 | */ |
||
| 211 | public function getFobIdentifiers(): array |
||
| 212 | { |
||
| 213 | $xpath = $this->getModsXpath(); |
||
| 214 | |||
| 215 | $nodes = $xpath->query('/mods:mods/mods:name[@type="personal"]'); |
||
| 216 | |||
| 217 | $identifiers = []; |
||
| 218 | |||
| 219 | foreach ($nodes as $key => $node) { |
||
| 220 | |||
| 221 | $identifierNode = $xpath->query('mods:nameIdentifier[@type="FOBID"]', $node); |
||
| 222 | |||
| 223 | if ($identifierNode->length > 0) { |
||
| 224 | $identifiers[] = $identifierNode->item(0)->nodeValue; |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | return $identifiers; |
||
| 229 | } |
||
| 230 | |||
| 231 | |||
| 232 | public function setDateIssued($date) |
||
| 233 | { |
||
| 234 | |||
| 235 | $originInfo = $this->getModsXpath()->query('/mods:mods/mods:originInfo[@eventType="distribution"]'); |
||
| 236 | |||
| 237 | if ($originInfo->length > 0) { |
||
| 238 | $dateIssued = $this->getModsXpath()->query('mods:dateIssued[@encoding="iso8601"][@keyDate="yes"]', $originInfo->item(0)); |
||
| 239 | |||
| 240 | if ($dateIssued->length == 0) { |
||
| 241 | $newDateIssued = $this->modsDom->createElement('mods:dateIssued'); |
||
| 242 | $newDateIssued->setAttribute('encoding', 'iso8601'); |
||
| 243 | $newDateIssued->setAttribute('keyDate', 'yes'); |
||
| 244 | $newDateIssued->nodeValue = $date; |
||
| 245 | $originInfo->item(0)->appendChild($newDateIssued); |
||
| 246 | } else { |
||
| 247 | $dateIssued->item(0)->nodeValue = $date; |
||
| 248 | } |
||
| 249 | |||
| 250 | } else { |
||
| 251 | |||
| 252 | $rootNode = $this->getModsXpath()->query('/mods:mods'); |
||
| 253 | |||
| 254 | if ($rootNode->length == 1) { |
||
| 255 | $newOriginInfo = $this->modsDom->createElement('mods:originInfo'); |
||
| 256 | $newOriginInfo->setAttribute('eventType', 'distribution'); |
||
| 257 | $rootNode->item(0)->appendChild($newOriginInfo); |
||
| 258 | |||
| 259 | $newDateIssued = $this->modsDom->createElement('mods:dateIssued'); |
||
| 260 | $newDateIssued->setAttribute('encoding', 'iso8601'); |
||
| 261 | $newDateIssued->setAttribute('keyDate', 'yes'); |
||
| 262 | $newDateIssued->nodeValue = $date; |
||
| 263 | $newOriginInfo->appendChild($newDateIssued); |
||
| 264 | } else { |
||
| 265 | throw new \Exception('Invalid xml data.'); |
||
| 266 | } |
||
| 267 | |||
| 268 | } |
||
| 269 | |||
| 270 | } |
||
| 271 | |||
| 272 | |||
| 273 | public function getPublishingYear() |
||
| 274 | { |
||
| 275 | $year = $this->getModsXpath()->query('/mods:mods/mods:originInfo[@eventType="publication"]/mods:dateIssued[@encoding="iso8601"]'); |
||
| 276 | if ($year->length > 0) { |
||
| 277 | return $year->item(0)->nodeValue; |
||
| 278 | } |
||
| 279 | |||
| 280 | return null; |
||
| 281 | } |
||
| 282 | |||
| 283 | public function getOriginalSourceTitle() |
||
| 291 | } |
||
| 292 | |||
| 293 | public function getDateIssued() |
||
| 294 | { |
||
| 295 | |||
| 296 | $dateIssued = $this->getModsXpath()->query('/mods:mods/mods:originInfo[@eventType="distribution"]/mods:dateIssued[@encoding="iso8601"][@keyDate="yes"]'); |
||
| 297 | if ($dateIssued->length > 0) { |
||
| 298 | return $dateIssued->item(0)->nodeValue; |
||
| 299 | } |
||
| 300 | |||
| 301 | return null; |
||
| 302 | } |
||
| 303 | |||
| 304 | public function removeDateIssued() |
||
| 305 | { |
||
| 306 | |||
| 307 | $dateIssued = $this->getModsXpath()->query('/mods:mods/mods:originInfo[@eventType="distribution"]/mods:dateIssued[@encoding="iso8601"][@keyDate="yes"]'); |
||
| 308 | if ($dateIssued->length > 0) { |
||
| 309 | $dateIssued->item(0)->parentNode->removeChild($dateIssued->item(0)); |
||
| 310 | } |
||
| 311 | |||
| 312 | } |
||
| 313 | |||
| 314 | public function hasQucosaUrn() |
||
| 315 | { |
||
| 316 | $urnNodeList = $this->getModsXpath()->query('/mods:mods/mods:identifier[@type="qucosa:urn"]'); |
||
| 317 | |||
| 318 | $hasUrn = false; |
||
| 319 | |||
| 320 | foreach ($urnNodeList as $urnNode) { |
||
| 321 | $value = $urnNode->nodeValue; |
||
| 322 | $hasUrn = $hasUrn || !empty($value); |
||
| 323 | } |
||
| 324 | |||
| 325 | return $hasUrn; |
||
| 326 | } |
||
| 327 | |||
| 328 | public function getQucosaUrn() |
||
| 329 | { |
||
| 330 | $urnNodeList = $this->getModsXpath()->query('/mods:mods/mods:identifier[@type="qucosa:urn"]'); |
||
| 331 | $urnList = ''; |
||
| 332 | |||
| 333 | if ($urnNodeList != null) { |
||
| 334 | foreach ($urnNodeList as $urnNode) { |
||
| 335 | $urnList = $urnNode->nodeValue; |
||
| 336 | } |
||
| 337 | } |
||
| 338 | return $urnList; |
||
| 339 | } |
||
| 340 | |||
| 341 | public function addQucosaUrn($urn) |
||
| 352 | } |
||
| 353 | |||
| 354 | } |
||
| 355 | |||
| 356 | public function clearAllUrn() |
||
| 367 | } |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @return string |
||
| 372 | */ |
||
| 373 | public function getSourceDetails() |
||
| 400 | } |
||
| 401 | } |
||
| 402 |