Total Complexity | 109 |
Total Lines | 723 |
Duplicated Lines | 0 % |
Changes | 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 |
||
26 | class InternalFormat |
||
27 | { |
||
28 | const rootNode = '//data/'; |
||
29 | |||
30 | /** |
||
31 | * clientConfigurationManager |
||
32 | * |
||
33 | * @var \EWW\Dpf\Configuration\ClientConfigurationManager |
||
34 | */ |
||
35 | protected $clientConfigurationManager; |
||
36 | |||
37 | /** |
||
38 | * xml |
||
39 | * |
||
40 | * @var \DOMDocument |
||
41 | */ |
||
42 | protected $xml; |
||
43 | |||
44 | /** |
||
45 | * @var int |
||
46 | */ |
||
47 | protected $clientPid = 0; |
||
48 | |||
49 | /** |
||
50 | * InternalFormat constructor. |
||
51 | * @param string $xml |
||
52 | * @param int $clientPid |
||
53 | */ |
||
54 | public function __construct(string $xml, $clientPid = 0) |
||
66 | } |
||
67 | |||
68 | public function setXml($xml) |
||
69 | { |
||
70 | if (empty($xml)) { |
||
71 | $xml = "<data></data>"; |
||
72 | } |
||
73 | |||
74 | $dom = new \DOMDocument(); |
||
75 | $dom->loadXML($xml); |
||
76 | $this->xml = $dom; |
||
77 | } |
||
78 | |||
79 | public function getXml() |
||
80 | { |
||
81 | return $this->xml->saveXML(); |
||
82 | } |
||
83 | |||
84 | public function getDocument() { |
||
85 | return $this->xml; |
||
86 | } |
||
87 | |||
88 | public function getXpath() |
||
89 | { |
||
90 | return $domXPath = \EWW\Dpf\Helper\XPath::create($this->xml); |
||
|
|||
91 | } |
||
92 | |||
93 | public function getDocumentType() |
||
94 | { |
||
95 | $typeXpath = $this->clientConfigurationManager->getTypeXpath(); |
||
96 | return $this->getValue($typeXpath); |
||
97 | } |
||
98 | |||
99 | public function setDocumentType($type) |
||
100 | { |
||
101 | $typeXpath = $this->clientConfigurationManager->getTypeXpath(); |
||
102 | $this->setValue($typeXpath, $type); |
||
103 | } |
||
104 | |||
105 | public function getRepositoryState() |
||
106 | { |
||
107 | $stateXpath = $this->clientConfigurationManager->getStateXpath(); |
||
108 | return $this->getValue($stateXpath); |
||
109 | } |
||
110 | |||
111 | public function setRepositoryState($state) |
||
112 | { |
||
113 | $stateXpath = $this->clientConfigurationManager->getStateXpath(); |
||
114 | $this->setValue($stateXpath,$state); |
||
115 | } |
||
116 | |||
117 | public function getProcessNumber() |
||
118 | { |
||
119 | $processNumberXpath = $this->clientConfigurationManager->getProcessNumberXpath(); |
||
120 | if ($processNumberXpath) { |
||
121 | return $this->getValue($processNumberXpath); |
||
122 | } else { |
||
123 | return ""; |
||
124 | } |
||
125 | } |
||
126 | |||
127 | public function setProcessNumber($processNumber) |
||
128 | { |
||
129 | $processNumberXpath = $this->clientConfigurationManager->getProcessNumberXpath(); |
||
130 | $this->setValue($processNumberXpath, $processNumber); |
||
131 | } |
||
132 | |||
133 | public function getTitle() |
||
134 | { |
||
135 | $titleXpath = $this->clientConfigurationManager->getTitleXpath(); |
||
136 | $xpath = $this->getXpath(); |
||
137 | |||
138 | if (!$titleXpath) { |
||
139 | $titleXpath = "titleInfo/title"; |
||
140 | } |
||
141 | |||
142 | $stateList = $xpath->query(self::rootNode . $titleXpath); |
||
143 | return $stateList->item(0)->nodeValue; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @param string $title |
||
148 | */ |
||
149 | public function setTitle($title) |
||
150 | { |
||
151 | $titleXpath = $this->clientConfigurationManager->getTitleXpath(); |
||
152 | $this->setValue($titleXpath, $title); |
||
153 | } |
||
154 | |||
155 | public function getFiles() |
||
156 | { |
||
157 | $xpath = $this->getXpath(); |
||
158 | |||
159 | $fileXpath = $this->clientConfigurationManager->getFileXpath(); |
||
160 | $fileIdXpath = $this->clientConfigurationManager->getFileIdXpath(); |
||
161 | $fileMimetypeXpath = $this->clientConfigurationManager->getFileMimetypeXpath(); |
||
162 | $fileHrefXpath = $this->clientConfigurationManager->getFileHrefXpath(); |
||
163 | $fileDownloadXpath = $this->clientConfigurationManager->getFileDownloadXpath(); |
||
164 | $fileArchiveXpath = $this->clientConfigurationManager->getFileArchiveXpath(); |
||
165 | $fileDeletedXpath = $this->clientConfigurationManager->getFileDeletedXpath(); |
||
166 | $fileTitleXpath = $this->clientConfigurationManager->getFileTitleXpath(); |
||
167 | |||
168 | $fileNodes = $xpath->query(self::rootNode . $fileXpath); |
||
169 | $files = []; |
||
170 | |||
171 | foreach ($fileNodes as $file) { |
||
172 | $fileAttrArray = [ |
||
173 | 'id' => '', |
||
174 | 'mimetype' => '', |
||
175 | 'href' => '', |
||
176 | 'title' => '', |
||
177 | 'download' => false, |
||
178 | 'archive' => false, |
||
179 | 'deleted' => false |
||
180 | ]; |
||
181 | foreach ($file->childNodes as $fileAttributes) { |
||
182 | switch ($fileAttributes->tagName) { |
||
183 | case $fileIdXpath: |
||
184 | $fileAttrArray['id'] = $fileAttributes->nodeValue; |
||
185 | break; |
||
186 | |||
187 | case $fileMimetypeXpath: |
||
188 | $fileAttrArray['mimetype'] = $fileAttributes->nodeValue; |
||
189 | break; |
||
190 | |||
191 | case $fileHrefXpath: |
||
192 | $fileAttrArray['href'] = $fileAttributes->nodeValue; |
||
193 | break; |
||
194 | |||
195 | case $fileTitleXpath: |
||
196 | $fileAttrArray['title'] = $fileAttributes->nodeValue; |
||
197 | break; |
||
198 | |||
199 | case $fileDownloadXpath: |
||
200 | $fileAttrArray['download'] = !empty($fileAttributes->nodeValue); |
||
201 | break; |
||
202 | |||
203 | case $fileArchiveXpath: |
||
204 | $fileAttrArray['archive'] = !empty($fileAttributes->nodeValue); |
||
205 | break; |
||
206 | |||
207 | case $fileDeletedXpath: |
||
208 | $fileAttrArray['deleted'] = !empty($fileAttributes->nodeValue); |
||
209 | break; |
||
210 | } |
||
211 | } |
||
212 | $files[] = $fileAttrArray; |
||
213 | } |
||
214 | |||
215 | return $files; |
||
216 | |||
217 | } |
||
218 | |||
219 | public function setDateIssued($date) { |
||
220 | $dateXpath = $this->clientConfigurationManager->getDateXpath(); |
||
221 | $this->setValue($dateXpath, $date); |
||
222 | } |
||
223 | |||
224 | public function getDateIssued() { |
||
225 | $dateXpath = $this->clientConfigurationManager->getDateXpath(); |
||
226 | return $this->getValue($dateXpath); |
||
227 | } |
||
228 | |||
229 | public function removeDateIssued() |
||
230 | { |
||
231 | $xpath = $this->getXpath(); |
||
232 | $dateXpath = $this->clientConfigurationManager->getDateXpath(); |
||
233 | |||
234 | $dateNodes = $xpath->query(self::rootNode . $dateXpath); |
||
235 | if ($dateNodes->length > 0) { |
||
236 | $dateNodes->item(0)->parentNode->removeChild($dateNodes->item(0)); |
||
237 | } |
||
238 | |||
239 | } |
||
240 | |||
241 | public function hasPrimaryUrn() |
||
242 | { |
||
243 | $xpath = $this->getXpath(); |
||
244 | $primaryUrnXpath = $this->clientConfigurationManager->getPrimaryUrnXpath(); |
||
245 | |||
246 | $urnNodes = $xpath->query(self::rootNode . $primaryUrnXpath); |
||
247 | if ($urnNodes->length > 0) { |
||
248 | return true; |
||
249 | } else { |
||
250 | return false; |
||
251 | } |
||
252 | } |
||
253 | |||
254 | public function getPrimaryUrn() |
||
255 | { |
||
256 | $xpath = $this->getXpath(); |
||
257 | $primaryUrnXpath = $this->clientConfigurationManager->getPrimaryUrnXpath(); |
||
258 | |||
259 | $urnNodes = $xpath->query(self::rootNode . $primaryUrnXpath); |
||
260 | if ($urnNodes->length > 0) { |
||
261 | return $urnNodes->item(0)->nodeValue; |
||
262 | } else { |
||
263 | return false; |
||
264 | } |
||
265 | } |
||
266 | |||
267 | public function setPrimaryUrn($urn) |
||
268 | { |
||
269 | $primaryUrnXpath = $this->clientConfigurationManager->getPrimaryUrnXpath(); |
||
270 | $this->setValue($primaryUrnXpath, $urn); |
||
271 | } |
||
272 | |||
273 | public function clearAllUrn() |
||
287 | } |
||
288 | } |
||
289 | |||
290 | public function getSubmitterEmail() { |
||
291 | $xpath = $this->getXpath(); |
||
292 | $submitterXpath = $urnXpath = $this->clientConfigurationManager->getSubmitterEmailXpath(); |
||
293 | |||
294 | $dateNodes = $xpath->query(self::rootNode . $submitterXpath); |
||
295 | if (!$dateNodes) { |
||
296 | return ''; |
||
297 | } else { |
||
298 | return $dateNodes->item(0)->nodeValue; |
||
299 | } |
||
300 | |||
301 | } |
||
302 | |||
303 | public function getSubmitterName() { |
||
304 | $xpath = $this->getXpath(); |
||
305 | $submitterXpath = $urnXpath = $this->clientConfigurationManager->getSubmitterNameXpath(); |
||
306 | |||
307 | $dateNodes = $xpath->query(self::rootNode . $submitterXpath); |
||
308 | |||
309 | if (!$dateNodes) { |
||
310 | return ''; |
||
311 | } else { |
||
312 | return $dateNodes->item(0)->nodeValue; |
||
313 | } |
||
314 | } |
||
315 | |||
316 | public function getSubmitterNotice() { |
||
326 | } |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * @return string |
||
331 | */ |
||
332 | public function getCreator() |
||
333 | { |
||
334 | $creatorXpath = $this->clientConfigurationManager->getCreatorXpath(); |
||
335 | $creator = $this->getValue($creatorXpath); |
||
336 | |||
337 | if (isset($creator) === true && $creator !== '') { |
||
338 | return $creator; |
||
339 | } |
||
340 | |||
341 | return '0'; |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * @param string $creator |
||
346 | */ |
||
347 | public function setCreator(string $creator) |
||
348 | { |
||
349 | $creatorXpath = $this->clientConfigurationManager->getCreatorXpath(); |
||
350 | $this->setValue($creatorXpath, $creator); |
||
351 | } |
||
352 | |||
353 | public function getCreationDate() |
||
354 | { |
||
355 | $xpath = $this->clientConfigurationManager->getCreationDateXpath(); |
||
356 | return $this->getValue($xpath); |
||
357 | } |
||
358 | |||
359 | public function setCreationDate($creationDate) |
||
360 | { |
||
361 | $xpath = $this->clientConfigurationManager->getCreationDateXpath(); |
||
362 | $this->setValue($xpath, $creationDate); |
||
363 | } |
||
364 | |||
365 | public function getRepositoryCreationDate() |
||
366 | { |
||
367 | $xpath = $this->clientConfigurationManager->getRepositoryCreationDateXpath(); |
||
368 | return $this->getValue($xpath); |
||
369 | } |
||
370 | |||
371 | public function getRepositoryLastModDate() |
||
372 | { |
||
373 | $xpath = $this->clientConfigurationManager->getRepositoryLastModDateXpath(); |
||
374 | return $this->getValue($xpath); |
||
375 | } |
||
376 | |||
377 | public function getPublishingYear() |
||
378 | { |
||
379 | $publishingYearXpath = $this->clientConfigurationManager->getPublishingYearXpath(); |
||
380 | return $this->getValue($publishingYearXpath); |
||
381 | } |
||
382 | |||
383 | public function getOriginalSourceTitle() |
||
384 | { |
||
385 | $originalSourceTitleXpath = $this->clientConfigurationManager->getOriginalSourceTitleXpath(); |
||
386 | return $this->getValue($originalSourceTitleXpath); |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * @return string |
||
391 | */ |
||
392 | public function getSourceDetails() |
||
393 | { |
||
394 | if (empty($sourceDetailsXpaths)) { |
||
395 | return ''; |
||
396 | } |
||
397 | |||
398 | $xpath = $this->getXpath(); |
||
399 | $data = []; |
||
400 | $sourceDetailsXpaths = $this->clientConfigurationManager->getSourceDetailsXpaths(); |
||
401 | $sourceDetailsXpathList = explode(";", trim($sourceDetailsXpaths," ;")); |
||
402 | $dataNodes = []; |
||
403 | |||
404 | foreach ($sourceDetailsXpathList as $sourceDetailsXpathItem) { |
||
405 | $dataNodes[] = $xpath->query(self::rootNode . trim($sourceDetailsXpathItem)); |
||
406 | } |
||
407 | |||
408 | foreach ($dataNodes as $dataNode) { |
||
409 | if (is_iterable($dataNode)) { |
||
410 | foreach ($dataNode as $node) { |
||
411 | if ($node->hasChildNodes()) { |
||
412 | foreach ($node->childNodes as $n) { |
||
413 | $data[] = preg_replace('/\s+/', ' ', $n->textContent); |
||
414 | } |
||
415 | } else { |
||
416 | $data[] = preg_replace('/\s+/', ' ', $node->textContent); |
||
417 | } |
||
418 | } |
||
419 | } |
||
420 | } |
||
421 | |||
422 | $output = trim(implode(' ', $data)); |
||
423 | $output = preg_replace('/\s+/ ', ' ', $output); |
||
424 | return $output; |
||
425 | } |
||
426 | |||
427 | /** |
||
428 | * Get all related FOB-IDs |
||
429 | * |
||
430 | * @return array |
||
431 | */ |
||
432 | public function getPersonFisIdentifiers(): array |
||
433 | { |
||
434 | $xpath = $this->getXpath(); |
||
435 | $personXpath = $this->clientConfigurationManager->getPersonXpath(); |
||
436 | $fisIdentifierXpath = $this->clientConfigurationManager->getPersonFisIdentifierXpath(); |
||
437 | $personNodes = $xpath->query(self::rootNode . $personXpath); |
||
438 | $identifiers = []; |
||
439 | foreach ($personNodes as $key => $node) { |
||
440 | $identifierNodes = $xpath->query($fisIdentifierXpath, $node); |
||
441 | if ($identifierNodes->length > 0) { |
||
442 | $identifiers[] = $identifierNodes->item(0)->nodeValue; |
||
443 | } |
||
444 | } |
||
445 | |||
446 | return $identifiers; |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * @return string |
||
451 | */ |
||
452 | public function getDepositLicense() |
||
453 | { |
||
454 | $depositLicenseXpath = $this->clientConfigurationManager->getDepositLicenseXpath(); |
||
455 | return $this->getValue($depositLicenseXpath); |
||
456 | } |
||
457 | |||
458 | /** |
||
459 | * @return array |
||
460 | */ |
||
461 | public function getNotes() |
||
462 | { |
||
463 | $notesXpath = $this->clientConfigurationManager->getAllNotesXpath(); |
||
464 | |||
465 | $xpath = $this->getXpath(); |
||
466 | $notesNodes = $xpath->query(self::rootNode . $notesXpath); |
||
467 | |||
468 | $notes = array(); |
||
469 | |||
470 | for ($i=0; $i < $notesNodes->length; $i++) |
||
471 | { |
||
472 | $notes[] = $notesNodes->item($i)->nodeValue; |
||
473 | } |
||
474 | |||
475 | return $notes; |
||
476 | } |
||
477 | |||
478 | public function addNote($noteContent) |
||
487 | } |
||
488 | |||
489 | public function getAuthors() |
||
492 | } |
||
493 | |||
494 | public function getPublishers() |
||
495 | { |
||
496 | return $this->getPersons($this->clientConfigurationManager->getPersonPublisherRole()); |
||
497 | } |
||
498 | |||
499 | /** |
||
500 | * Get persons of the given role |
||
501 | * |
||
502 | * @param string $role |
||
503 | * @return array |
||
504 | */ |
||
505 | public function getPersons($role = '') |
||
585 | } |
||
586 | } |
||
587 | |||
588 | /** |
||
589 | * @return bool |
||
590 | */ |
||
591 | public function getValidation() |
||
592 | { |
||
593 | $validationXpath = $this->clientConfigurationManager->getValidationXpath(); |
||
594 | $validation = $this->getValue($validationXpath); |
||
595 | return (strtolower($validation) === 'true')? true : false; |
||
596 | } |
||
597 | |||
598 | /** |
||
599 | * @param bool $validated |
||
600 | */ |
||
601 | public function setValidation($validated) |
||
602 | { |
||
603 | $validationXpath = $this->clientConfigurationManager->getValidationXpath(); |
||
604 | $this->setValue($validationXpath, ($validated? 'true' : 'false')); |
||
605 | } |
||
606 | |||
607 | /** |
||
608 | * @param string $fisId |
||
609 | */ |
||
610 | public function setFisId($fisId) |
||
611 | { |
||
612 | $fisIdXpath = $this->clientConfigurationManager->getFisIdXpath(); |
||
613 | $this->setValue($fisIdXpath, $fisId); |
||
614 | } |
||
615 | |||
616 | /** |
||
617 | * @param string $xpathString |
||
618 | * @return string |
||
619 | */ |
||
620 | protected function getValue($xpathString) |
||
628 | } |
||
629 | |||
630 | /** |
||
631 | * @param string $xpathString |
||
632 | * @param string $value |
||
633 | */ |
||
634 | protected function setValue(string $xpathString, string $value) |
||
635 | { |
||
636 | $xpath = $this->getXpath(); |
||
637 | $nodes = $xpath->query(self::rootNode . $xpathString); |
||
638 | if ($nodes->length > 0) { |
||
639 | $nodes->item(0)->nodeValue = $value; |
||
640 | } elseif(isset($value) === true && $value !== '') { |
||
641 | $parserGenerator = new ParserGenerator($this->clientPid); |
||
642 | $parserGenerator->setXml($this->xml->saveXML()); |
||
643 | $parserGenerator->customXPath($xpathString,true, $value); |
||
644 | $this->xml = new \DOMDocument(); |
||
645 | $this->xml->loadXML($parserGenerator->getXMLData()); |
||
646 | } |
||
647 | } |
||
648 | |||
649 | /** |
||
650 | * Removes all file nodes from the internal xml |
||
651 | */ |
||
652 | public function removeAllFiles() { |
||
658 | } |
||
659 | } |
||
660 | |||
661 | /** |
||
662 | * @param DOMNode $fileNode |
||
663 | * @param string $nodeXpath |
||
664 | * @param string $value |
||
665 | */ |
||
666 | public function setFileData(DOMNode $fileNode, string $nodeXpath, string $value) |
||
667 | { |
||
668 | $xpath = $this->getXpath(); |
||
669 | |||
670 | if ($fileNode) { |
||
671 | $nodes = $xpath->query($nodeXpath, $fileNode); |
||
672 | |||
673 | if ($nodes->length > 0) { |
||
674 | $nodes->item(0)->nodeValue = $value; |
||
675 | } else { |
||
676 | /** @var XPathXMLGenerator $xPathXMLGenerator */ |
||
677 | $xPathXMLGenerator = new XPathXMLGenerator(); |
||
678 | $xPathXMLGenerator->generateXmlFromXPath($nodeXpath . "='" . $value . "'"); |
||
679 | |||
680 | // FIXME: XPATHXmlGenerator XPATH does not generate any namespaces, |
||
681 | // which DOMDocument cannot cope with. Actually, namespaces should not be necessary here, |
||
682 | // since it is about child elements that are then added to the overall XML. |
||
683 | libxml_use_internal_errors(true); |
||
684 | $dom = new \DOMDocument(); |
||
685 | $domLoaded = $dom->loadXML($xPathXMLGenerator->getXML()); |
||
686 | libxml_use_internal_errors(false); |
||
687 | |||
688 | if ($domLoaded) { |
||
689 | $newField = $this->xml->importNode($dom->firstChild, true); |
||
690 | $fileNode->appendChild($newField); |
||
691 | } |
||
692 | } |
||
693 | } |
||
694 | } |
||
695 | |||
696 | /** |
||
697 | * @param ObjectStorage<File> $files |
||
698 | * @throws \Exception |
||
699 | */ |
||
700 | public function completeFileData(ObjectStorage $files) |
||
749 | } |
||
750 | } |
||
751 | } |
||
752 | } |
||
753 | } |
||
754 | } |
||
755 |