| Total Complexity | 82 |
| Total Lines | 540 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like InternalFormat 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 InternalFormat, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class InternalFormat |
||
| 22 | { |
||
| 23 | const rootNode = '//data/'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * clientConfigurationManager |
||
| 27 | * |
||
| 28 | * @var \EWW\Dpf\Configuration\ClientConfigurationManager |
||
| 29 | */ |
||
| 30 | protected $clientConfigurationManager; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * xml |
||
| 34 | * |
||
| 35 | * @var \DOMDocument |
||
| 36 | */ |
||
| 37 | protected $xml; |
||
| 38 | |||
| 39 | public function __construct($xml) |
||
| 45 | } |
||
| 46 | |||
| 47 | public function setXml($xml) |
||
| 48 | { |
||
| 49 | if (empty($xml)) { |
||
| 50 | $xml = "<data></data>"; |
||
| 51 | } |
||
| 52 | |||
| 53 | $dom = new \DOMDocument(); |
||
| 54 | $dom->loadXML($xml); |
||
| 55 | $this->xml = $dom; |
||
| 56 | } |
||
| 57 | |||
| 58 | public function getXml() |
||
| 59 | { |
||
| 60 | return $this->xml->saveXML(); |
||
| 61 | } |
||
| 62 | |||
| 63 | public function getDocument() { |
||
| 64 | return $this->xml; |
||
| 65 | } |
||
| 66 | |||
| 67 | public function getXpath() |
||
| 68 | { |
||
| 69 | return $domXPath = \EWW\Dpf\Helper\XPath::create($this->xml); |
||
|
|
|||
| 70 | } |
||
| 71 | |||
| 72 | public function getDocumentType() |
||
| 73 | { |
||
| 74 | $typeXpath = $this->clientConfigurationManager->getTypeXpath(); |
||
| 75 | return $this->getValue($typeXpath); |
||
| 76 | } |
||
| 77 | |||
| 78 | public function setDocumentType($type) |
||
| 79 | { |
||
| 80 | $typeXpath = $this->clientConfigurationManager->getTypeXpath(); |
||
| 81 | $this->setValue($typeXpath, $type); |
||
| 82 | } |
||
| 83 | |||
| 84 | public function getRepositoryState() |
||
| 85 | { |
||
| 86 | $stateXpath = $this->clientConfigurationManager->getStateXpath(); |
||
| 87 | return $this->getValue($stateXpath); |
||
| 88 | } |
||
| 89 | |||
| 90 | public function setRepositoryState($state) |
||
| 91 | { |
||
| 92 | $stateXpath = $this->clientConfigurationManager->getStateXpath(); |
||
| 93 | $this->setValue($stateXpath,$state); |
||
| 94 | } |
||
| 95 | |||
| 96 | public function getProcessNumber() |
||
| 97 | { |
||
| 98 | $processNumberXpath = $this->clientConfigurationManager->getProcessNumberXpath(); |
||
| 99 | if ($processNumberXpath) { |
||
| 100 | return $this->getValue($processNumberXpath); |
||
| 101 | } else { |
||
| 102 | return ""; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | public function setProcessNumber($processNumber) |
||
| 107 | { |
||
| 108 | $processNumberXpath = $this->clientConfigurationManager->getProcessNumberXpath(); |
||
| 109 | $this->getValue($processNumberXpath, $processNumber); |
||
| 110 | } |
||
| 111 | |||
| 112 | public function getTitle() |
||
| 113 | { |
||
| 114 | $titleXpath = $this->clientConfigurationManager->getTitleXpath(); |
||
| 115 | $xpath = $this->getXpath(); |
||
| 116 | |||
| 117 | if (!$titleXpath) { |
||
| 118 | $titleXpath = "titleInfo/title"; |
||
| 119 | } |
||
| 120 | |||
| 121 | $stateList = $xpath->query(self::rootNode . $titleXpath); |
||
| 122 | return $stateList->item(0)->nodeValue; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param string $title |
||
| 127 | */ |
||
| 128 | public function setTitle($title) |
||
| 129 | { |
||
| 130 | $titleXpath = $this->clientConfigurationManager->getTitleXpath(); |
||
| 131 | $this->setValue($titleXpath, $title); |
||
| 132 | } |
||
| 133 | |||
| 134 | public function getFiles() |
||
| 135 | { |
||
| 136 | $xpath = $this->getXpath(); |
||
| 137 | |||
| 138 | $fileXpath = $this->clientConfigurationManager->getFileXpath(); |
||
| 139 | |||
| 140 | $fileNodes = $xpath->query(self::rootNode . $fileXpath); |
||
| 141 | $files = []; |
||
| 142 | |||
| 143 | foreach ($fileNodes as $file) { |
||
| 144 | $fileAttrArray = []; |
||
| 145 | foreach ($file->childNodes as $fileAttributes) { |
||
| 146 | $fileAttrArray[$fileAttributes->tagName] = $fileAttributes->nodeValue; |
||
| 147 | } |
||
| 148 | $files[] = $fileAttrArray; |
||
| 149 | } |
||
| 150 | |||
| 151 | return $files; |
||
| 152 | |||
| 153 | } |
||
| 154 | |||
| 155 | public function setDateIssued($date) { |
||
| 156 | $dateXpath = $this->clientConfigurationManager->getDateXpath(); |
||
| 157 | $this->setValue($dateXpath, $date); |
||
| 158 | } |
||
| 159 | |||
| 160 | public function getDateIssued() { |
||
| 161 | $dateXpath = $this->clientConfigurationManager->getDateXpath(); |
||
| 162 | return $this->getValue($dateXpath); |
||
| 163 | } |
||
| 164 | |||
| 165 | public function removeDateIssued() |
||
| 166 | { |
||
| 167 | $xpath = $this->getXpath(); |
||
| 168 | $dateXpath = $this->clientConfigurationManager->getDateXpath(); |
||
| 169 | |||
| 170 | $dateNodes = $xpath->query(self::rootNode . $dateXpath); |
||
| 171 | if ($dateNodes->length > 0) { |
||
| 172 | $dateNodes->item(0)->parentNode->removeChild($dateNodes->item(0)); |
||
| 173 | } |
||
| 174 | |||
| 175 | } |
||
| 176 | |||
| 177 | public function hasQucosaUrn() |
||
| 178 | { |
||
| 179 | $xpath = $this->getXpath(); |
||
| 180 | $qucosaUrnXpath = $this->clientConfigurationManager->getQucosaUrnXpath(); |
||
| 181 | |||
| 182 | $urnNodes = $xpath->query(self::rootNode . $qucosaUrnXpath); |
||
| 183 | if ($urnNodes->length > 0) { |
||
| 184 | return true; |
||
| 185 | } else { |
||
| 186 | return false; |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | public function getQucosaUrn() |
||
| 191 | { |
||
| 192 | $xpath = $this->getXpath(); |
||
| 193 | $qucosaUrnXpath = $this->clientConfigurationManager->getQucosaUrnXpath(); |
||
| 194 | |||
| 195 | $urnNodes = $xpath->query(self::rootNode . $qucosaUrnXpath); |
||
| 196 | if ($urnNodes->length > 0) { |
||
| 197 | return $urnNodes->item(0)->nodeValue; |
||
| 198 | } else { |
||
| 199 | return false; |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | public function setQucosaUrn($urn) |
||
| 204 | { |
||
| 205 | $qucosaUrnXpath = $this->clientConfigurationManager->getQucosaUrnXpath(); |
||
| 206 | $this->setValue($qucosaUrnXpath, $urn); |
||
| 207 | } |
||
| 208 | |||
| 209 | public function clearAllUrn() |
||
| 210 | { |
||
| 211 | $xpath = $this->getXpath(); |
||
| 212 | $urnXpath = $this->clientConfigurationManager->getUrnXpath(); |
||
| 213 | $qucosaUrnXpath = $this->clientConfigurationManager->getQucosaUrnXpath(); |
||
| 214 | |||
| 215 | $urnNodes = $xpath->query(self::rootNode . $urnXpath); |
||
| 216 | foreach ($urnNodes as $urnNode) { |
||
| 217 | $urnNode->parentNode->removeChild($urnNode); |
||
| 218 | } |
||
| 219 | |||
| 220 | $qucosaUrnNodes = $xpath->query(self::rootNode . $qucosaUrnXpath); |
||
| 221 | foreach ($qucosaUrnNodes as $qucosaUrnNode) { |
||
| 222 | $qucosaUrnNode->parentNode->removeChild($qucosaUrnNode); |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | public function getSubmitterEmail() { |
||
| 227 | $xpath = $this->getXpath(); |
||
| 228 | $submitterXpath = $urnXpath = $this->clientConfigurationManager->getSubmitterEmailXpath(); |
||
| 229 | |||
| 230 | $dateNodes = $xpath->query(self::rootNode . $submitterXpath); |
||
| 231 | if (!$dateNodes) { |
||
| 232 | return ''; |
||
| 233 | } else { |
||
| 234 | return $dateNodes->item(0)->nodeValue; |
||
| 235 | } |
||
| 236 | |||
| 237 | } |
||
| 238 | |||
| 239 | public function getSubmitterName() { |
||
| 240 | $xpath = $this->getXpath(); |
||
| 241 | $submitterXpath = $urnXpath = $this->clientConfigurationManager->getSubmitterNameXpath(); |
||
| 242 | |||
| 243 | $dateNodes = $xpath->query(self::rootNode . $submitterXpath); |
||
| 244 | |||
| 245 | if (!$dateNodes) { |
||
| 246 | return ''; |
||
| 247 | } else { |
||
| 248 | return $dateNodes->item(0)->nodeValue; |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | public function getSubmitterNotice() { |
||
| 253 | $xpath = $this->getXpath(); |
||
| 254 | $submitterXpath = $urnXpath = $this->clientConfigurationManager->getSubmitterNoticeXpath(); |
||
| 255 | |||
| 256 | $dateNodes = $xpath->query(self::rootNode . $submitterXpath); |
||
| 257 | |||
| 258 | if (!$dateNodes) { |
||
| 259 | return ''; |
||
| 260 | } else { |
||
| 261 | return $dateNodes->item(0)->nodeValue; |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | public function getCreator() |
||
| 266 | { |
||
| 267 | $creatorXpath = $this->clientConfigurationManager->getCreatorXpath(); |
||
| 268 | return $this->getValue($creatorXpath); |
||
| 269 | } |
||
| 270 | |||
| 271 | public function setCreator($creator) |
||
| 272 | { |
||
| 273 | $creatorXpath = $this->clientConfigurationManager->getCreatorXpath(); |
||
| 274 | $this->setValue($creatorXpath, $creator); |
||
| 275 | } |
||
| 276 | |||
| 277 | public function getCreationDate() |
||
| 278 | { |
||
| 279 | $xpath = $this->clientConfigurationManager->getCreationDateXpath(); |
||
| 280 | return $this->getValue($xpath); |
||
| 281 | } |
||
| 282 | |||
| 283 | public function setCreationDate($creationDate) |
||
| 284 | { |
||
| 285 | $xpath = $this->clientConfigurationManager->getCreationDateXpath(); |
||
| 286 | $this->setValue($xpath, $creationDate); |
||
| 287 | } |
||
| 288 | |||
| 289 | public function getRepositoryCreationDate() |
||
| 290 | { |
||
| 291 | $xpath = $this->clientConfigurationManager->getRepositoryCreationDateXpath(); |
||
| 292 | return $this->getValue($xpath); |
||
| 293 | } |
||
| 294 | |||
| 295 | public function getRepositoryLastModDate() |
||
| 296 | { |
||
| 297 | $xpath = $this->clientConfigurationManager->getRepositoryLastModDateXpath(); |
||
| 298 | return $this->getValue($xpath); |
||
| 299 | } |
||
| 300 | |||
| 301 | public function getPublishingYear() |
||
| 302 | { |
||
| 303 | $publishingYearXpath = $this->clientConfigurationManager->getPublishingYearXpath(); |
||
| 304 | return $this->getValue($publishingYearXpath); |
||
| 305 | } |
||
| 306 | |||
| 307 | public function getOriginalSourceTitle() |
||
| 308 | { |
||
| 309 | $originalSourceTitleXpath = $this->clientConfigurationManager->getOriginalSourceTitleXpath(); |
||
| 310 | return $this->getValue($originalSourceTitleXpath); |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | public function getSourceDetails() |
||
| 317 | { |
||
| 318 | $xpath = $this->getXpath(); |
||
| 319 | $data = []; |
||
| 320 | $sourceDetailsXpaths = $this->clientConfigurationManager->getSourceDetailsXpaths(); |
||
| 321 | $sourceDetailsXpathList = explode(";", trim($sourceDetailsXpaths," ;")); |
||
| 322 | $dataNodes = []; |
||
| 323 | |||
| 324 | foreach ($sourceDetailsXpathList as $sourceDetailsXpathItem) { |
||
| 325 | $dataNodes[] = $xpath->query(self::rootNode . trim($sourceDetailsXpathItem)); |
||
| 326 | } |
||
| 327 | |||
| 328 | foreach ($dataNodes as $dataNode) { |
||
| 329 | foreach ($dataNode as $node) { |
||
| 330 | if ($node->hasChildNodes()) { |
||
| 331 | foreach ($node->childNodes as $n) { |
||
| 332 | $data[] = preg_replace('/\s+/', ' ', $n->textContent); |
||
| 333 | } |
||
| 334 | } else { |
||
| 335 | $data[] = preg_replace('/\s+/', ' ', $node->textContent); |
||
| 336 | } |
||
| 337 | } |
||
| 338 | } |
||
| 339 | |||
| 340 | $output = trim(implode(' ', $data)); |
||
| 341 | $output = preg_replace('/\s+/ ', ' ', $output); |
||
| 342 | return $output; |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Get all related FOB-IDs |
||
| 347 | * |
||
| 348 | * @return array |
||
| 349 | */ |
||
| 350 | public function getPersonFisIdentifiers(): array |
||
| 351 | { |
||
| 352 | $xpath = $this->getXpath(); |
||
| 353 | $personXpath = $this->clientConfigurationManager->getPersonXpath(); |
||
| 354 | $fisIdentifierXpath = $this->clientConfigurationManager->getPersonFisIdentifierXpath(); |
||
| 355 | $personNodes = $xpath->query(self::rootNode . $personXpath); |
||
| 356 | $identifiers = []; |
||
| 357 | foreach ($personNodes as $key => $node) { |
||
| 358 | $identifierNodes = $xpath->query($fisIdentifierXpath, $node); |
||
| 359 | if ($identifierNodes->length > 0) { |
||
| 360 | $identifiers[] = $identifierNodes->item(0)->nodeValue; |
||
| 361 | } |
||
| 362 | } |
||
| 363 | |||
| 364 | return $identifiers; |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @return string |
||
| 369 | */ |
||
| 370 | public function getDepositLicense() |
||
| 371 | { |
||
| 372 | $depositLicenseXpath = $this->clientConfigurationManager->getDepositLicenseXpath(); |
||
| 373 | return $this->getValue($depositLicenseXpath); |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @return array |
||
| 378 | */ |
||
| 379 | public function getNotes() |
||
| 380 | { |
||
| 381 | $notesXpath = $this->clientConfigurationManager->getAllNotesXpath(); |
||
| 382 | |||
| 383 | $xpath = $this->getXpath(); |
||
| 384 | $notesNodes = $xpath->query(self::rootNode . $notesXpath); |
||
| 385 | |||
| 386 | $notes = array(); |
||
| 387 | |||
| 388 | for ($i=0; $i < $notesNodes->length; $i++) |
||
| 389 | { |
||
| 390 | $notes[] = $notesNodes->item($i)->nodeValue; |
||
| 391 | } |
||
| 392 | |||
| 393 | return $notes; |
||
| 394 | } |
||
| 395 | |||
| 396 | public function addNote($noteContent) |
||
| 405 | } |
||
| 406 | |||
| 407 | public function getAuthors() |
||
| 410 | } |
||
| 411 | |||
| 412 | public function getPublishers() |
||
| 413 | { |
||
| 414 | return $this->getPersons($this->clientConfigurationManager->getPersonPublisherRole()); |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Get persons of the given role |
||
| 419 | * |
||
| 420 | * @param string $role |
||
| 421 | * @return array |
||
| 422 | */ |
||
| 423 | public function getPersons($role = '') |
||
| 503 | } |
||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @return bool |
||
| 508 | */ |
||
| 509 | public function getValidation() |
||
| 510 | { |
||
| 511 | $validationXpath = $this->clientConfigurationManager->getValidationXpath(); |
||
| 512 | $validation = $this->getValue($validationXpath); |
||
| 513 | return (strtolower($validation) === 'true')? true : false; |
||
| 514 | } |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @param bool $validated |
||
| 518 | */ |
||
| 519 | public function setValidation($validated) |
||
| 520 | { |
||
| 521 | $validationXpath = $this->clientConfigurationManager->getValidationXpath(); |
||
| 522 | $this->setValue($validationXpath, ($validated? 'true' : 'false')); |
||
| 523 | } |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @param string $fisId |
||
| 527 | */ |
||
| 528 | public function setFisId($fisId) |
||
| 529 | { |
||
| 530 | $fisIdXpath = $this->clientConfigurationManager->getFisIdXpath(); |
||
| 531 | $this->setValue($fisIdXpath, $fisId); |
||
| 532 | } |
||
| 533 | |||
| 534 | /** |
||
| 535 | * @param string $xpathString |
||
| 536 | * @return string |
||
| 537 | */ |
||
| 538 | protected function getValue($xpathString) |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * @param string $xpathString |
||
| 547 | * @param string $value |
||
| 548 | */ |
||
| 549 | protected function setValue($xpathString, $value) |
||
| 550 | { |
||
| 551 | $xpath = $this->getXpath(); |
||
| 552 | $nodes = $xpath->query(self::rootNode . $xpathString); |
||
| 553 | if ($nodes->length > 0) { |
||
| 554 | $nodes->item(0)->nodeValue = $value; |
||
| 555 | } elseif(!empty($value)) { |
||
| 556 | $parserGenerator = new ParserGenerator(); |
||
| 557 | $parserGenerator->setXml($this->xml->saveXML()); |
||
| 558 | $parserGenerator->customXPath($xpathString,true, $value); |
||
| 559 | $this->xml = new \DOMDocument(); |
||
| 560 | $this->xml->loadXML($parserGenerator->getXMLData()); |
||
| 561 | } |
||
| 562 | } |
||
| 563 | |||
| 564 | } |
||
| 565 |