Total Complexity | 106 |
Total Lines | 711 |
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() |
||
274 | { |
||
275 | $xpath = $this->getXpath(); |
||
276 | $urnXpath = $this->clientConfigurationManager->getUrnXpath(); |
||
277 | $primaryUrnXpath = $this->clientConfigurationManager->getPrimaryUrnXpath(); |
||
278 | |||
279 | $urnNodes = $xpath->query(self::rootNode . $urnXpath); |
||
280 | foreach ($urnNodes as $urnNode) { |
||
281 | $urnNode->parentNode->removeChild($urnNode); |
||
282 | } |
||
283 | |||
284 | $primaryUrnNodes = $xpath->query(self::rootNode . $primaryUrnXpath); |
||
285 | foreach ($primaryUrnNodes as $primaryUrnNode) { |
||
286 | $primaryUrnNode->parentNode->removeChild($primaryUrnNode); |
||
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() { |
||
317 | $xpath = $this->getXpath(); |
||
318 | $submitterXpath = $urnXpath = $this->clientConfigurationManager->getSubmitterNoticeXpath(); |
||
319 | |||
320 | $dateNodes = $xpath->query(self::rootNode . $submitterXpath); |
||
321 | |||
322 | if (!$dateNodes) { |
||
323 | return ''; |
||
324 | } else { |
||
325 | return $dateNodes->item(0)->nodeValue; |
||
326 | } |
||
327 | } |
||
328 | |||
329 | public function getCreator() |
||
330 | { |
||
331 | $creatorXpath = $this->clientConfigurationManager->getCreatorXpath(); |
||
332 | return $this->getValue($creatorXpath); |
||
333 | } |
||
334 | |||
335 | public function setCreator($creator) |
||
336 | { |
||
337 | $creatorXpath = $this->clientConfigurationManager->getCreatorXpath(); |
||
338 | $this->setValue($creatorXpath, $creator); |
||
339 | } |
||
340 | |||
341 | public function getCreationDate() |
||
342 | { |
||
343 | $xpath = $this->clientConfigurationManager->getCreationDateXpath(); |
||
344 | return $this->getValue($xpath); |
||
345 | } |
||
346 | |||
347 | public function setCreationDate($creationDate) |
||
348 | { |
||
349 | $xpath = $this->clientConfigurationManager->getCreationDateXpath(); |
||
350 | $this->setValue($xpath, $creationDate); |
||
351 | } |
||
352 | |||
353 | public function getRepositoryCreationDate() |
||
354 | { |
||
355 | $xpath = $this->clientConfigurationManager->getRepositoryCreationDateXpath(); |
||
356 | return $this->getValue($xpath); |
||
357 | } |
||
358 | |||
359 | public function getRepositoryLastModDate() |
||
360 | { |
||
361 | $xpath = $this->clientConfigurationManager->getRepositoryLastModDateXpath(); |
||
362 | return $this->getValue($xpath); |
||
363 | } |
||
364 | |||
365 | public function getPublishingYear() |
||
366 | { |
||
367 | $publishingYearXpath = $this->clientConfigurationManager->getPublishingYearXpath(); |
||
368 | return $this->getValue($publishingYearXpath); |
||
369 | } |
||
370 | |||
371 | public function getOriginalSourceTitle() |
||
372 | { |
||
373 | $originalSourceTitleXpath = $this->clientConfigurationManager->getOriginalSourceTitleXpath(); |
||
374 | return $this->getValue($originalSourceTitleXpath); |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * @return string |
||
379 | */ |
||
380 | public function getSourceDetails() |
||
381 | { |
||
382 | if (empty($sourceDetailsXpaths)) { |
||
383 | return ''; |
||
384 | } |
||
385 | |||
386 | $xpath = $this->getXpath(); |
||
387 | $data = []; |
||
388 | $sourceDetailsXpaths = $this->clientConfigurationManager->getSourceDetailsXpaths(); |
||
389 | $sourceDetailsXpathList = explode(";", trim($sourceDetailsXpaths," ;")); |
||
390 | $dataNodes = []; |
||
391 | |||
392 | foreach ($sourceDetailsXpathList as $sourceDetailsXpathItem) { |
||
393 | $dataNodes[] = $xpath->query(self::rootNode . trim($sourceDetailsXpathItem)); |
||
394 | } |
||
395 | |||
396 | foreach ($dataNodes as $dataNode) { |
||
397 | if (is_iterable($dataNode)) { |
||
398 | foreach ($dataNode as $node) { |
||
399 | if ($node->hasChildNodes()) { |
||
400 | foreach ($node->childNodes as $n) { |
||
401 | $data[] = preg_replace('/\s+/', ' ', $n->textContent); |
||
402 | } |
||
403 | } else { |
||
404 | $data[] = preg_replace('/\s+/', ' ', $node->textContent); |
||
405 | } |
||
406 | } |
||
407 | } |
||
408 | } |
||
409 | |||
410 | $output = trim(implode(' ', $data)); |
||
411 | $output = preg_replace('/\s+/ ', ' ', $output); |
||
412 | return $output; |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * Get all related FOB-IDs |
||
417 | * |
||
418 | * @return array |
||
419 | */ |
||
420 | public function getPersonFisIdentifiers(): array |
||
421 | { |
||
422 | $xpath = $this->getXpath(); |
||
423 | $personXpath = $this->clientConfigurationManager->getPersonXpath(); |
||
424 | $fisIdentifierXpath = $this->clientConfigurationManager->getPersonFisIdentifierXpath(); |
||
425 | $personNodes = $xpath->query(self::rootNode . $personXpath); |
||
426 | $identifiers = []; |
||
427 | foreach ($personNodes as $key => $node) { |
||
428 | $identifierNodes = $xpath->query($fisIdentifierXpath, $node); |
||
429 | if ($identifierNodes->length > 0) { |
||
430 | $identifiers[] = $identifierNodes->item(0)->nodeValue; |
||
431 | } |
||
432 | } |
||
433 | |||
434 | return $identifiers; |
||
435 | } |
||
436 | |||
437 | /** |
||
438 | * @return string |
||
439 | */ |
||
440 | public function getDepositLicense() |
||
441 | { |
||
442 | $depositLicenseXpath = $this->clientConfigurationManager->getDepositLicenseXpath(); |
||
443 | return $this->getValue($depositLicenseXpath); |
||
444 | } |
||
445 | |||
446 | /** |
||
447 | * @return array |
||
448 | */ |
||
449 | public function getNotes() |
||
450 | { |
||
451 | $notesXpath = $this->clientConfigurationManager->getAllNotesXpath(); |
||
452 | |||
453 | $xpath = $this->getXpath(); |
||
454 | $notesNodes = $xpath->query(self::rootNode . $notesXpath); |
||
455 | |||
456 | $notes = array(); |
||
457 | |||
458 | for ($i=0; $i < $notesNodes->length; $i++) |
||
459 | { |
||
460 | $notes[] = $notesNodes->item($i)->nodeValue; |
||
461 | } |
||
462 | |||
463 | return $notes; |
||
464 | } |
||
465 | |||
466 | public function addNote($noteContent) |
||
475 | } |
||
476 | |||
477 | public function getAuthors() |
||
480 | } |
||
481 | |||
482 | public function getPublishers() |
||
483 | { |
||
484 | return $this->getPersons($this->clientConfigurationManager->getPersonPublisherRole()); |
||
485 | } |
||
486 | |||
487 | /** |
||
488 | * Get persons of the given role |
||
489 | * |
||
490 | * @param string $role |
||
491 | * @return array |
||
492 | */ |
||
493 | public function getPersons($role = '') |
||
573 | } |
||
574 | } |
||
575 | |||
576 | /** |
||
577 | * @return bool |
||
578 | */ |
||
579 | public function getValidation() |
||
580 | { |
||
581 | $validationXpath = $this->clientConfigurationManager->getValidationXpath(); |
||
582 | $validation = $this->getValue($validationXpath); |
||
583 | return (strtolower($validation) === 'true')? true : false; |
||
584 | } |
||
585 | |||
586 | /** |
||
587 | * @param bool $validated |
||
588 | */ |
||
589 | public function setValidation($validated) |
||
590 | { |
||
591 | $validationXpath = $this->clientConfigurationManager->getValidationXpath(); |
||
592 | $this->setValue($validationXpath, ($validated? 'true' : 'false')); |
||
593 | } |
||
594 | |||
595 | /** |
||
596 | * @param string $fisId |
||
597 | */ |
||
598 | public function setFisId($fisId) |
||
599 | { |
||
600 | $fisIdXpath = $this->clientConfigurationManager->getFisIdXpath(); |
||
601 | $this->setValue($fisIdXpath, $fisId); |
||
602 | } |
||
603 | |||
604 | /** |
||
605 | * @param string $xpathString |
||
606 | * @return string |
||
607 | */ |
||
608 | protected function getValue($xpathString) |
||
616 | } |
||
617 | |||
618 | /** |
||
619 | * @param string $xpathString |
||
620 | * @param string $value |
||
621 | */ |
||
622 | protected function setValue($xpathString, $value) |
||
623 | { |
||
624 | $xpath = $this->getXpath(); |
||
625 | $nodes = $xpath->query(self::rootNode . $xpathString); |
||
626 | if ($nodes->length > 0) { |
||
627 | $nodes->item(0)->nodeValue = $value; |
||
628 | } elseif(!empty($value)) { |
||
629 | $parserGenerator = new ParserGenerator($this->clientPid); |
||
630 | $parserGenerator->setXml($this->xml->saveXML()); |
||
631 | $parserGenerator->customXPath($xpathString,true, $value); |
||
632 | $this->xml = new \DOMDocument(); |
||
633 | $this->xml->loadXML($parserGenerator->getXMLData()); |
||
634 | } |
||
635 | } |
||
636 | |||
637 | /** |
||
638 | * Removes all file nodes from the internal xml |
||
639 | */ |
||
640 | public function removeAllFiles() { |
||
646 | } |
||
647 | } |
||
648 | |||
649 | /** |
||
650 | * @param DOMNode $fileNode |
||
651 | * @param string $nodeXpath |
||
652 | * @param string $value |
||
653 | */ |
||
654 | public function setFileData(DOMNode $fileNode, string $nodeXpath, string $value) |
||
655 | { |
||
656 | $xpath = $this->getXpath(); |
||
657 | |||
658 | if ($fileNode) { |
||
659 | $nodes = $xpath->query($nodeXpath, $fileNode); |
||
660 | |||
661 | if ($nodes->length > 0) { |
||
662 | $nodes->item(0)->nodeValue = $value; |
||
663 | } else { |
||
664 | /** @var XPathXMLGenerator $xPathXMLGenerator */ |
||
665 | $xPathXMLGenerator = new XPathXMLGenerator(); |
||
666 | $xPathXMLGenerator->generateXmlFromXPath($nodeXpath . "='" . $value . "'"); |
||
667 | |||
668 | // FIXME: XPATHXmlGenerator XPATH does not generate any namespaces, |
||
669 | // which DOMDocument cannot cope with. Actually, namespaces should not be necessary here, |
||
670 | // since it is about child elements that are then added to the overall XML. |
||
671 | libxml_use_internal_errors(true); |
||
672 | $dom = new \DOMDocument(); |
||
673 | $domLoaded = $dom->loadXML($xPathXMLGenerator->getXML()); |
||
674 | libxml_use_internal_errors(false); |
||
675 | |||
676 | if ($domLoaded) { |
||
677 | $newField = $this->xml->importNode($dom->firstChild, true); |
||
678 | $fileNode->appendChild($newField); |
||
679 | } |
||
680 | } |
||
681 | } |
||
682 | } |
||
683 | |||
684 | /** |
||
685 | * @param ObjectStorage<File> $files |
||
686 | * @throws \Exception |
||
687 | */ |
||
688 | public function completeFileData(ObjectStorage $files) |
||
737 | } |
||
738 | } |
||
739 | } |
||
740 | } |
||
741 | } |
||
742 | } |
||
743 |