Passed
Pull Request — master (#207)
by
unknown
16:03 queued 05:02
created

InternalFormat::completeFileData()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 49
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 49
rs 8.5226
c 0
b 0
f 0
cc 7
nc 7
nop 1
1
<?php
2
namespace EWW\Dpf\Helper;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use EWW\Dpf\Configuration\ClientConfigurationManager;
18
use EWW\Dpf\Services\ParserGenerator;
19
use EWW\Dpf\Services\Transfer\FileId;
20
use EWW\Dpf\Services\XPathXMLGenerator;
21
use TYPO3\CMS\Extbase\Object\ObjectManager;
22
use EWW\Dpf\Domain\Model\File;
23
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
24
use DOMNode;
25
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
    public function __construct($xml)
45
    {
46
        $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(ObjectManager::class);
47
        $this->clientConfigurationManager = $objectManager->get(ClientConfigurationManager::class);
48
49
        $this->setXml($xml);
50
    }
51
52
    public function setXml($xml)
53
    {
54
        if (empty($xml)) {
55
            $xml = "<data></data>";
56
        }
57
58
        $dom = new \DOMDocument();
59
        $dom->loadXML($xml);
60
        $this->xml = $dom;
61
    }
62
63
    public function getXml()
64
    {
65
        return $this->xml->saveXML();
66
    }
67
68
    public function getDocument() {
69
        return $this->xml;
70
    }
71
72
    public function getXpath()
73
    {
74
        return $domXPath = \EWW\Dpf\Helper\XPath::create($this->xml);
0 ignored issues
show
Unused Code introduced by
The assignment to $domXPath is dead and can be removed.
Loading history...
75
    }
76
77
    public function getDocumentType()
78
    {
79
        $typeXpath = $this->clientConfigurationManager->getTypeXpath();
80
        return $this->getValue($typeXpath);
81
    }
82
83
    public function setDocumentType($type)
84
    {
85
        $typeXpath = $this->clientConfigurationManager->getTypeXpath();
86
        $this->setValue($typeXpath, $type);
87
    }
88
89
    public function getRepositoryState()
90
    {
91
        $stateXpath = $this->clientConfigurationManager->getStateXpath();
92
        return $this->getValue($stateXpath);
93
    }
94
95
    public function setRepositoryState($state)
96
    {
97
        $stateXpath = $this->clientConfigurationManager->getStateXpath();
98
        $this->setValue($stateXpath,$state);
99
    }
100
101
    public function getProcessNumber()
102
    {
103
        $processNumberXpath = $this->clientConfigurationManager->getProcessNumberXpath();
104
        if ($processNumberXpath) {
105
            return $this->getValue($processNumberXpath);
106
        } else {
107
            return "";
108
        }
109
    }
110
111
    public function setProcessNumber($processNumber)
112
    {
113
        $processNumberXpath = $this->clientConfigurationManager->getProcessNumberXpath();
114
        $this->setValue($processNumberXpath, $processNumber);
115
    }
116
117
    public function getTitle()
118
    {
119
        $titleXpath = $this->clientConfigurationManager->getTitleXpath();
120
        $xpath = $this->getXpath();
121
122
        if (!$titleXpath) {
123
            $titleXpath = "titleInfo/title";
124
        }
125
126
        $stateList = $xpath->query(self::rootNode . $titleXpath);
127
        return $stateList->item(0)->nodeValue;
128
    }
129
130
    /**
131
     * @param string $title
132
     */
133
    public function setTitle($title)
134
    {
135
        $titleXpath = $this->clientConfigurationManager->getTitleXpath();
136
        $this->setValue($titleXpath, $title);
137
    }
138
139
    public function getFiles()
140
    {
141
        $xpath = $this->getXpath();
142
143
        $fileXpath = $this->clientConfigurationManager->getFileXpath();
144
        $fileIdXpath = $this->clientConfigurationManager->getFileIdXpath();
145
        $fileMimetypeXpath = $this->clientConfigurationManager->getFileMimetypeXpath();
146
        $fileHrefXpath = $this->clientConfigurationManager->getFileHrefXpath();
147
        $fileDownloadXpath = $this->clientConfigurationManager->getFileDownloadXpath();
148
        $fileArchiveXpath = $this->clientConfigurationManager->getFileArchiveXpath();
149
        $fileDeletedXpath = $this->clientConfigurationManager->getFileDeletedXpath();
150
        $fileTitleXpath = $this->clientConfigurationManager->getFileTitleXpath();
151
152
        $fileNodes = $xpath->query(self::rootNode . $fileXpath);
153
        $files = [];
154
155
        foreach ($fileNodes as $file) {
156
            $fileAttrArray = [
157
                'id' => '',
158
                'mimetype' => '',
159
                'href' => '',
160
                'title' => '',
161
                'download' => false,
162
                'archive' => false,
163
                'deleted' => false
164
            ];
165
            foreach ($file->childNodes as $fileAttributes) {
166
                switch ($fileAttributes->tagName) {
167
                    case $fileIdXpath:
168
                        $fileAttrArray['id'] = $fileAttributes->nodeValue;
169
                        break;
170
171
                    case $fileMimetypeXpath:
172
                        $fileAttrArray['mimetype'] = $fileAttributes->nodeValue;
173
                        break;
174
175
                    case $fileHrefXpath:
176
                        $fileAttrArray['href'] = $fileAttributes->nodeValue;
177
                        break;
178
179
                    case $fileTitleXpath:
180
                        $fileAttrArray['title'] = $fileAttributes->nodeValue;
181
                        break;
182
183
                    case $fileDownloadXpath:
184
                        $fileAttrArray['download'] = !empty($fileAttributes->nodeValue);
185
                        break;
186
187
                    case $fileArchiveXpath:
188
                        $fileAttrArray['archive'] = !empty($fileAttributes->nodeValue);
189
                        break;
190
191
                    case $fileDeletedXpath:
192
                        $fileAttrArray['deleted'] = !empty($fileAttributes->nodeValue);
193
                        break;
194
                }
195
            }
196
            $files[] = $fileAttrArray;
197
        }
198
199
        return $files;
200
201
    }
202
203
    public function setDateIssued($date) {
204
        $dateXpath = $this->clientConfigurationManager->getDateXpath();
205
        $this->setValue($dateXpath, $date);
206
    }
207
208
    public function getDateIssued() {
209
        $dateXpath = $this->clientConfigurationManager->getDateXpath();
210
        return $this->getValue($dateXpath);
211
    }
212
213
    public function removeDateIssued()
214
    {
215
        $xpath = $this->getXpath();
216
        $dateXpath = $this->clientConfigurationManager->getDateXpath();
217
218
        $dateNodes = $xpath->query(self::rootNode . $dateXpath);
219
        if ($dateNodes->length > 0) {
220
            $dateNodes->item(0)->parentNode->removeChild($dateNodes->item(0));
0 ignored issues
show
Bug introduced by
The method removeChild() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

220
            $dateNodes->item(0)->parentNode->/** @scrutinizer ignore-call */ 
221
                                             removeChild($dateNodes->item(0));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
221
        }
222
223
    }
224
225
    public function hasPrimaryUrn()
226
    {
227
        $xpath = $this->getXpath();
228
        $primaryUrnXpath = $this->clientConfigurationManager->getPrimaryUrnXpath();
229
230
        $urnNodes = $xpath->query(self::rootNode . $primaryUrnXpath);
231
        if ($urnNodes->length > 0) {
232
            return true;
233
        } else {
234
            return false;
235
        }
236
    }
237
238
    public function getPrimaryUrn()
239
    {
240
        $xpath = $this->getXpath();
241
        $primaryUrnXpath = $this->clientConfigurationManager->getPrimaryUrnXpath();
242
243
        $urnNodes = $xpath->query(self::rootNode . $primaryUrnXpath);
244
        if ($urnNodes->length > 0) {
245
            return $urnNodes->item(0)->nodeValue;
246
        } else {
247
            return false;
248
        }
249
    }
250
251
    public function setPrimaryUrn($urn)
252
    {
253
        $primaryUrnXpath = $this->clientConfigurationManager->getPrimaryUrnXpath();
254
        $this->setValue($primaryUrnXpath, $urn);
255
    }
256
257
    public function clearAllUrn()
258
    {
259
        $xpath = $this->getXpath();
260
        $urnXpath = $this->clientConfigurationManager->getUrnXpath();
261
        $primaryUrnXpath = $this->clientConfigurationManager->getPrimaryUrnXpath();
262
263
        $urnNodes = $xpath->query(self::rootNode . $urnXpath);
264
        foreach ($urnNodes as $urnNode) {
265
            $urnNode->parentNode->removeChild($urnNode);
266
        }
267
268
        $primaryUrnNodes = $xpath->query(self::rootNode . $primaryUrnXpath);
269
        foreach ($primaryUrnNodes as $primaryUrnNode) {
270
            $primaryUrnNode->parentNode->removeChild($primaryUrnNode);
271
        }
272
    }
273
274
    public function getSubmitterEmail() {
275
        $xpath = $this->getXpath();
276
        $submitterXpath = $urnXpath = $this->clientConfigurationManager->getSubmitterEmailXpath();
0 ignored issues
show
Unused Code introduced by
The assignment to $urnXpath is dead and can be removed.
Loading history...
277
278
        $dateNodes = $xpath->query(self::rootNode . $submitterXpath);
279
        if (!$dateNodes) {
0 ignored issues
show
introduced by
$dateNodes is of type DOMNodeList, thus it always evaluated to true.
Loading history...
280
            return '';
281
        } else {
282
            return $dateNodes->item(0)->nodeValue;
283
        }
284
285
    }
286
287
    public function getSubmitterName() {
288
        $xpath = $this->getXpath();
289
        $submitterXpath = $urnXpath = $this->clientConfigurationManager->getSubmitterNameXpath();
0 ignored issues
show
Unused Code introduced by
The assignment to $urnXpath is dead and can be removed.
Loading history...
290
291
        $dateNodes = $xpath->query(self::rootNode . $submitterXpath);
292
293
        if (!$dateNodes) {
0 ignored issues
show
introduced by
$dateNodes is of type DOMNodeList, thus it always evaluated to true.
Loading history...
294
            return '';
295
        } else {
296
            return $dateNodes->item(0)->nodeValue;
297
        }
298
    }
299
300
    public function getSubmitterNotice() {
301
        $xpath = $this->getXpath();
302
        $submitterXpath = $urnXpath = $this->clientConfigurationManager->getSubmitterNoticeXpath();
0 ignored issues
show
Unused Code introduced by
The assignment to $urnXpath is dead and can be removed.
Loading history...
303
304
        $dateNodes = $xpath->query(self::rootNode . $submitterXpath);
305
306
        if (!$dateNodes) {
0 ignored issues
show
introduced by
$dateNodes is of type DOMNodeList, thus it always evaluated to true.
Loading history...
307
            return '';
308
        } else {
309
            return $dateNodes->item(0)->nodeValue;
310
        }
311
    }
312
313
    public function getCreator()
314
    {
315
        $creatorXpath = $this->clientConfigurationManager->getCreatorXpath();
316
        return $this->getValue($creatorXpath);
317
    }
318
319
    public function setCreator($creator)
320
    {
321
        $creatorXpath = $this->clientConfigurationManager->getCreatorXpath();
322
        $this->setValue($creatorXpath, $creator);
323
    }
324
325
    public function getCreationDate()
326
    {
327
        $xpath = $this->clientConfigurationManager->getCreationDateXpath();
328
        return $this->getValue($xpath);
329
    }
330
331
    public function setCreationDate($creationDate)
332
    {
333
        $xpath = $this->clientConfigurationManager->getCreationDateXpath();
334
        $this->setValue($xpath, $creationDate);
335
    }
336
337
    public function getRepositoryCreationDate()
338
    {
339
        $xpath = $this->clientConfigurationManager->getRepositoryCreationDateXpath();
340
        return $this->getValue($xpath);
341
    }
342
343
    public function getRepositoryLastModDate()
344
    {
345
        $xpath = $this->clientConfigurationManager->getRepositoryLastModDateXpath();
346
        return $this->getValue($xpath);
347
    }
348
349
    public function getPublishingYear()
350
    {
351
        $publishingYearXpath = $this->clientConfigurationManager->getPublishingYearXpath();
352
        return $this->getValue($publishingYearXpath);
353
    }
354
355
    public function getOriginalSourceTitle()
356
    {
357
        $originalSourceTitleXpath = $this->clientConfigurationManager->getOriginalSourceTitleXpath();
358
        return $this->getValue($originalSourceTitleXpath);
359
    }
360
361
    /**
362
     * @return string
363
     */
364
    public function getSourceDetails()
365
    {
366
        $xpath = $this->getXpath();
367
        $data = [];
368
        $sourceDetailsXpaths = $this->clientConfigurationManager->getSourceDetailsXpaths();
369
        $sourceDetailsXpathList = explode(";", trim($sourceDetailsXpaths," ;"));
0 ignored issues
show
Bug introduced by
It seems like $sourceDetailsXpaths can also be of type null; however, parameter $string of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

369
        $sourceDetailsXpathList = explode(";", trim(/** @scrutinizer ignore-type */ $sourceDetailsXpaths," ;"));
Loading history...
370
        $dataNodes = [];
371
372
        foreach ($sourceDetailsXpathList as $sourceDetailsXpathItem) {
373
            $dataNodes[] = $xpath->query(self::rootNode . trim($sourceDetailsXpathItem));
374
        }
375
376
        foreach ($dataNodes as $dataNode) {
377
            foreach ($dataNode as $node) {
378
                if ($node->hasChildNodes()) {
379
                    foreach ($node->childNodes as $n) {
380
                        $data[] = preg_replace('/\s+/', ' ', $n->textContent);
381
                    }
382
                } else {
383
                    $data[] = preg_replace('/\s+/', ' ', $node->textContent);
384
                }
385
            }
386
        }
387
388
        $output = trim(implode(' ', $data));
389
        $output = preg_replace('/\s+/ ', ' ', $output);
390
        return $output;
391
    }
392
393
    /**
394
     * Get all related FOB-IDs
395
     *
396
     * @return array
397
     */
398
    public function getPersonFisIdentifiers(): array
399
    {
400
        $xpath = $this->getXpath();
401
        $personXpath = $this->clientConfigurationManager->getPersonXpath();
402
        $fisIdentifierXpath =  $this->clientConfigurationManager->getPersonFisIdentifierXpath();
403
        $personNodes = $xpath->query(self::rootNode . $personXpath);
404
        $identifiers = [];
405
        foreach ($personNodes as $key => $node) {
406
            $identifierNodes = $xpath->query($fisIdentifierXpath, $node);
407
            if ($identifierNodes->length > 0) {
408
                $identifiers[] = $identifierNodes->item(0)->nodeValue;
409
            }
410
        }
411
412
        return $identifiers;
413
    }
414
415
    /**
416
     * @return string
417
     */
418
    public function getDepositLicense()
419
    {
420
        $depositLicenseXpath = $this->clientConfigurationManager->getDepositLicenseXpath();
421
        return $this->getValue($depositLicenseXpath);
422
    }
423
424
    /**
425
     * @return array
426
     */
427
    public function getNotes()
428
    {
429
        $notesXpath = $this->clientConfigurationManager->getAllNotesXpath();
430
431
        $xpath = $this->getXpath();
432
        $notesNodes = $xpath->query(self::rootNode . $notesXpath);
433
434
        $notes = array();
435
436
        for ($i=0; $i < $notesNodes->length; $i++)
437
        {
438
            $notes[] = $notesNodes->item($i)->nodeValue;
439
        }
440
441
        return $notes;
442
    }
443
444
    public function addNote($noteContent)
445
    {
446
        $notesXpath = $this->clientConfigurationManager->getPrivateNotesXpath();
447
448
        $parserGenerator = new ParserGenerator();
449
        $parserGenerator->setXml($this->xml->saveXML());
450
        $parserGenerator->customXPath($notesXpath,true, $noteContent);
0 ignored issues
show
Bug introduced by
It seems like $notesXpath can also be of type string; however, parameter $xPath of EWW\Dpf\Services\ParserGenerator::customXPath() does only seem to accept EWW\Dpf\Services\xpath, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

450
        $parserGenerator->customXPath(/** @scrutinizer ignore-type */ $notesXpath,true, $noteContent);
Loading history...
451
        $this->xml = new \DOMDocument();
452
        $this->xml->loadXML($parserGenerator->getXMLData());
453
    }
454
455
    public function getAuthors()
456
    {
457
        return $this->getPersons($this->clientConfigurationManager->getPersonAuthorRole());
458
    }
459
460
    public function getPublishers()
461
    {
462
        return $this->getPersons($this->clientConfigurationManager->getPersonPublisherRole());
463
    }
464
465
    /**
466
     * Get persons of the given role
467
     *
468
     * @param string $role
469
     * @return array
470
     */
471
    public function getPersons($role = '')
472
    {
473
        $personXpath = $this->clientConfigurationManager->getPersonXpath();
474
        $familyXpath = $this->clientConfigurationManager->getPersonFamilyXpath();
475
        $givenXpath = $this->clientConfigurationManager->getPersonGivenXpath();
476
        $roleXpath = $this->clientConfigurationManager->getPersonRoleXpath();
477
        $fisIdentifierXpath =  $this->clientConfigurationManager->getPersonFisIdentifierXpath();
478
        $affiliationXpath =  $this->clientConfigurationManager->getPersonAffiliationXpath();
479
        $affiliationIdentifierXpath =  $this->clientConfigurationManager->getPersonAffiliationIdentifierXpath();
480
481
        $xpath = $this->getXpath();
482
        $personNodes = $xpath->query(self::rootNode . $personXpath);
483
484
        $persons = [];
485
486
        foreach ($personNodes as $key => $personNode) {
487
            $familyNodes = $xpath->query($familyXpath, $personNode);
488
            $givenNodes = $xpath->query($givenXpath, $personNode);
489
            $roleNodes = $xpath->query($roleXpath, $personNode);
490
            $identifierNodes = $xpath->query($fisIdentifierXpath, $personNode);
491
            $affiliationNodes = $xpath->query($affiliationXpath, $personNode);
492
            $affiliationIdentifierNodes = $xpath->query($affiliationIdentifierXpath, $personNode);
493
494
            $person['affiliations'] = [];
495
            foreach ($affiliationNodes as $key => $affiliationNode) {
0 ignored issues
show
Comprehensibility Bug introduced by
$key is overwriting a variable from outer foreach loop.
Loading history...
496
                $person['affiliations'][] = $affiliationNode->nodeValue;
497
            }
498
499
            $person['affiliationIdentifiers'] = [];
500
            foreach ($affiliationIdentifierNodes as $key => $affiliationIdentifierNode) {
501
                $person['affiliationIdentifiers'][] = $affiliationIdentifierNode->nodeValue;
502
            }
503
504
            $given = '';
505
            $family = '';
506
507
            if ($givenNodes->length > 0) {
508
                $given = $givenNodes->item(0)->nodeValue;
509
            }
510
511
            if ($familyNodes->length > 0) {
512
                $family = $familyNodes->item(0)->nodeValue;
513
            }
514
515
            $person['given'] = trim($given);
516
            $person['family'] = trim($family);
517
518
            $name = [];
519
            if ($person['given']) {
520
                $name[] = $person['given'];
521
            }
522
            if ($person['family']) {
523
                $name[] = $person['family'];
524
            }
525
526
            $person['name'] = implode(' ', $name);
527
528
            $person['role'] = '';
529
            if ($roleNodes->length > 0) {
530
                $person['role'] = $roleNodes->item(0)->nodeValue;
531
            }
532
533
            $person['fobId'] = '';
534
            if ($identifierNodes->length > 0) {
535
                $person['fobId'] = $identifierNodes->item(0)->nodeValue;
536
            }
537
538
            $person['index'] = $key;
539
            $persons[] = $person;
540
        }
541
542
        if ($role) {
543
            $result = [];
544
            foreach ($persons as $person) {
545
                if ($person['role'] == $role)
546
                    $result[] = $person;
547
            }
548
            return $result;
549
        } else {
550
            return $persons;
551
        }
552
    }
553
554
    /**
555
     * @return bool
556
     */
557
    public function getValidation()
558
    {
559
        $validationXpath =  $this->clientConfigurationManager->getValidationXpath();
560
        $validation = $this->getValue($validationXpath);
561
        return (strtolower($validation) === 'true')? true : false;
562
    }
563
564
    /**
565
     * @param bool $validated
566
     */
567
    public function setValidation($validated)
568
    {
569
        $validationXpath =  $this->clientConfigurationManager->getValidationXpath();
570
        $this->setValue($validationXpath, ($validated? 'true' : 'false'));
571
    }
572
573
    /**
574
     * @param string $fisId
575
     */
576
    public function setFisId($fisId)
577
    {
578
        $fisIdXpath =  $this->clientConfigurationManager->getFisIdXpath();
0 ignored issues
show
Bug introduced by
The method getFisIdXpath() does not exist on EWW\Dpf\Configuration\ClientConfigurationManager. Did you maybe mean getFileIdXpath()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

578
        /** @scrutinizer ignore-call */ 
579
        $fisIdXpath =  $this->clientConfigurationManager->getFisIdXpath();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
579
        $this->setValue($fisIdXpath, $fisId);
580
    }
581
582
    /**
583
     * @param string $xpathString
584
     * @return string
585
     */
586
    protected function getValue($xpathString)
587
    {
588
        $xpath = $this->getXpath();
589
        $nodeList = $xpath->query(self::rootNode . $xpathString);
590
        return $nodeList->item(0)->nodeValue;
591
    }
592
593
    /**
594
     * @param string $xpathString
595
     * @param string $value
596
     */
597
    protected function setValue($xpathString, $value)
598
    {
599
        $xpath = $this->getXpath();
600
        $nodes = $xpath->query(self::rootNode . $xpathString);
601
        if ($nodes->length > 0) {
602
            $nodes->item(0)->nodeValue = $value;
603
        } elseif(!empty($value)) {
604
            $parserGenerator = new ParserGenerator();
605
            $parserGenerator->setXml($this->xml->saveXML());
606
            $parserGenerator->customXPath($xpathString,true, $value);
0 ignored issues
show
Bug introduced by
$xpathString of type string is incompatible with the type EWW\Dpf\Services\xpath expected by parameter $xPath of EWW\Dpf\Services\ParserGenerator::customXPath(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

606
            $parserGenerator->customXPath(/** @scrutinizer ignore-type */ $xpathString,true, $value);
Loading history...
607
            $this->xml = new \DOMDocument();
608
            $this->xml->loadXML($parserGenerator->getXMLData());
609
        }
610
    }
611
612
    /**
613
     * Removes all file nodes from the internal xml
614
     */
615
    public function removeAllFiles() {
616
        $xpath = $this->getXpath();
617
        $fileXpath = $this->clientConfigurationManager->getFileXpath();
618
        $fileNodes = $xpath->query(self::rootNode . $fileXpath);
619
        foreach ($fileNodes as $fileNode) {
620
            $fileNode->parentNode->removeChild($fileNode);
621
        }
622
    }
623
624
    /**
625
     * @param DOMNode $fileNode
626
     * @param string $nodeXpath
627
     * @param string $value
628
     */
629
    public function setFileData(DOMNode $fileNode, string $nodeXpath, string $value)
630
    {
631
        $xpath = $this->getXpath();
632
633
        if ($fileNode) {
0 ignored issues
show
introduced by
$fileNode is of type DOMNode, thus it always evaluated to true.
Loading history...
634
            $nodes = $xpath->query($nodeXpath, $fileNode);
635
636
            if ($nodes->length > 0) {
637
                $nodes->item(0)->nodeValue = $value;
638
            } else {
639
                /** @var XPathXMLGenerator $xPathXMLGenerator */
640
                $xPathXMLGenerator = new XPathXMLGenerator();
641
                $xPathXMLGenerator->generateXmlFromXPath($nodeXpath . "='" . $value . "'");
642
643
                // FIXME: XPATHXmlGenerator XPATH does not generate any namespaces,
644
                // which DOMDocument cannot cope with. Actually, namespaces should not be necessary here,
645
                // since it is about child elements that are then added to the overall XML.
646
                libxml_use_internal_errors(true);
647
                $dom = new \DOMDocument();
648
                $domLoaded = $dom->loadXML($xPathXMLGenerator->getXML());
649
                libxml_use_internal_errors(false);
650
651
                if ($domLoaded) {
652
                    $newField = $this->xml->importNode($dom->firstChild, true);
0 ignored issues
show
Bug introduced by
It seems like $dom->firstChild can also be of type null; however, parameter $node of DOMDocument::importNode() does only seem to accept DOMNode, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

652
                    $newField = $this->xml->importNode(/** @scrutinizer ignore-type */ $dom->firstChild, true);
Loading history...
653
                    $fileNode->appendChild($newField);
654
                }
655
            }
656
        }
657
    }
658
659
    /**
660
     * @param ObjectStorage<File> $files
661
     * @throws \Exception
662
     */
663
    public function completeFileData(ObjectStorage $files)
664
    {
665
        $fileId = new FileId($files);
666
667
        $xpath = $this->getXpath();
668
669
        $fileXpath = $this->clientConfigurationManager->getFileXpath();
670
        $mimeTypeXpath = $this->clientConfigurationManager->getFileMimetypeXpath();
671
        $idXpath = $this->clientConfigurationManager->getFileIdXpath();
672
        $deletedXpath = $this->clientConfigurationManager->getFileDeletedXpath();
673
674
        /** @var File $file */
675
        foreach ($files as $file) {
676
677
            $dataStreamIdentifier = $file->getDatastreamIdentifier();
678
679
            if (!$file->isFileGroupDeleted()) {
680
681
                if ($file->isDeleted()) {
682
683
                    if (!empty($dataStreamIdentifier)) {
684
                        /** @var XPathXMLGenerator $xPathXMLGenerator */
685
                        $xPathXMLGenerator = new XPathXMLGenerator();
686
                        $xPathXMLGenerator->generateXmlFromXPath($fileXpath);
687
688
                        // FIXME: XPATHXmlGenerator XPATH does not generate any namespaces,
689
                        // which DOMDocument cannot cope with. Actually, namespaces should not be necessary here,
690
                        // since it is about child elements that are then added to the overall XML.
691
                        libxml_use_internal_errors(true);
692
                        $dom = new \DOMDocument();
693
                        $domLoaded = $dom->loadXML($xPathXMLGenerator->getXML());
694
                        libxml_use_internal_errors(false);
695
696
                        if ($domLoaded) {
697
                            $newFile = $this->xml->importNode($dom->firstChild, true);
0 ignored issues
show
Bug introduced by
It seems like $dom->firstChild can also be of type null; however, parameter $node of DOMDocument::importNode() does only seem to accept DOMNode, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

697
                            $newFile = $this->xml->importNode(/** @scrutinizer ignore-type */ $dom->firstChild, true);
Loading history...
698
                            $newFile->setAttribute('usage', 'delete');
699
                            $this->setFileData($newFile, $idXpath, $file->getDatastreamIdentifier());
700
                            $this->setFileData($newFile, $deletedXpath, 'yes');
701
                            $this->xml->firstChild->appendChild($newFile);
0 ignored issues
show
Bug introduced by
The method appendChild() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

701
                            $this->xml->firstChild->/** @scrutinizer ignore-call */ 
702
                                                    appendChild($newFile);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
702
                        }
703
                    }
704
                } else {
705
                    $fileNodes = $xpath->query(
706
                        self::rootNode . $fileXpath . '[./'.trim($idXpath, '@/ ').'="'.$file->getFileIdentifier().'"]'
707
                    );
708
709
                    if ($fileNodes->length > 0) {
710
                        $this->setFileData($fileNodes->item(0), $idXpath, $fileId->getId($file));
711
                        $this->setFileData($fileNodes->item(0), $mimeTypeXpath, $file->getContentType());
712
                    }
713
                }
714
            }
715
        }
716
    }
717
}
718