Passed
Pull Request — master (#207)
by
unknown
12:49
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
    /**
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)
55
    {
56
        $this->clientPid = $clientPid;
57
58
        $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(ObjectManager::class);
59
        $this->clientConfigurationManager = $objectManager->get(ClientConfigurationManager::class);
60
61
        if ($clientPid) {
62
            $this->clientConfigurationManager->setConfigurationPid($clientPid);
63
        }
64
65
        $this->setXml($xml);
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);
0 ignored issues
show
Unused Code introduced by
The assignment to $domXPath is dead and can be removed.
Loading history...
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));
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

236
            $dateNodes->item(0)->parentNode->/** @scrutinizer ignore-call */ 
237
                                             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...
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();
0 ignored issues
show
Unused Code introduced by
The assignment to $urnXpath is dead and can be removed.
Loading history...
293
294
        $dateNodes = $xpath->query(self::rootNode . $submitterXpath);
295
        if (!$dateNodes) {
0 ignored issues
show
introduced by
$dateNodes is of type DOMNodeList, thus it always evaluated to true.
Loading history...
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();
0 ignored issues
show
Unused Code introduced by
The assignment to $urnXpath is dead and can be removed.
Loading history...
306
307
        $dateNodes = $xpath->query(self::rootNode . $submitterXpath);
308
309
        if (!$dateNodes) {
0 ignored issues
show
introduced by
$dateNodes is of type DOMNodeList, thus it always evaluated to true.
Loading history...
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();
0 ignored issues
show
Unused Code introduced by
The assignment to $urnXpath is dead and can be removed.
Loading history...
319
320
        $dateNodes = $xpath->query(self::rootNode . $submitterXpath);
321
322
        if (!$dateNodes) {
0 ignored issues
show
introduced by
$dateNodes is of type DOMNodeList, thus it always evaluated to true.
Loading history...
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)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sourceDetailsXpaths seems to never exist and therefore empty should always be true.
Loading history...
383
            return '';
384
        }
385
386
        $xpath = $this->getXpath();
387
        $data = [];
388
        $sourceDetailsXpaths = $this->clientConfigurationManager->getSourceDetailsXpaths();
389
        $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

389
        $sourceDetailsXpathList = explode(";", trim(/** @scrutinizer ignore-type */ $sourceDetailsXpaths," ;"));
Loading history...
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)
467
    {
468
        $notesXpath = $this->clientConfigurationManager->getPrivateNotesXpath();
469
470
        $parserGenerator = new ParserGenerator($this->clientPid);
471
        $parserGenerator->setXml($this->xml->saveXML());
472
        $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

472
        $parserGenerator->customXPath(/** @scrutinizer ignore-type */ $notesXpath,true, $noteContent);
Loading history...
473
        $this->xml = new \DOMDocument();
474
        $this->xml->loadXML($parserGenerator->getXMLData());
475
    }
476
477
    public function getAuthors()
478
    {
479
        return $this->getPersons($this->clientConfigurationManager->getPersonAuthorRole());
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 = '')
494
    {
495
        $personXpath = $this->clientConfigurationManager->getPersonXpath();
496
        $familyXpath = $this->clientConfigurationManager->getPersonFamilyXpath();
497
        $givenXpath = $this->clientConfigurationManager->getPersonGivenXpath();
498
        $roleXpath = $this->clientConfigurationManager->getPersonRoleXpath();
499
        $fisIdentifierXpath =  $this->clientConfigurationManager->getPersonFisIdentifierXpath();
500
        $affiliationXpath =  $this->clientConfigurationManager->getPersonAffiliationXpath();
501
        $affiliationIdentifierXpath =  $this->clientConfigurationManager->getPersonAffiliationIdentifierXpath();
502
503
        $xpath = $this->getXpath();
504
        $personNodes = $xpath->query(self::rootNode . $personXpath);
505
506
        $persons = [];
507
508
        foreach ($personNodes as $key => $personNode) {
509
            $familyNodes = $xpath->query($familyXpath, $personNode);
510
            $givenNodes = $xpath->query($givenXpath, $personNode);
511
            $roleNodes = $xpath->query($roleXpath, $personNode);
512
            $identifierNodes = $xpath->query($fisIdentifierXpath, $personNode);
513
            $affiliationNodes = $xpath->query($affiliationXpath, $personNode);
514
            $affiliationIdentifierNodes = $xpath->query($affiliationIdentifierXpath, $personNode);
515
516
            $person['affiliations'] = [];
517
            foreach ($affiliationNodes as $key => $affiliationNode) {
0 ignored issues
show
Comprehensibility Bug introduced by
$key is overwriting a variable from outer foreach loop.
Loading history...
518
                $person['affiliations'][] = $affiliationNode->nodeValue;
519
            }
520
521
            $person['affiliationIdentifiers'] = [];
522
            foreach ($affiliationIdentifierNodes as $key => $affiliationIdentifierNode) {
523
                $person['affiliationIdentifiers'][] = $affiliationIdentifierNode->nodeValue;
524
            }
525
526
            $given = '';
527
            $family = '';
528
529
            if ($givenNodes->length > 0) {
530
                $given = $givenNodes->item(0)->nodeValue;
531
            }
532
533
            if ($familyNodes->length > 0) {
534
                $family = $familyNodes->item(0)->nodeValue;
535
            }
536
537
            $person['given'] = trim($given);
538
            $person['family'] = trim($family);
539
540
            $name = [];
541
            if ($person['given']) {
542
                $name[] = $person['given'];
543
            }
544
            if ($person['family']) {
545
                $name[] = $person['family'];
546
            }
547
548
            $person['name'] = implode(' ', $name);
549
550
            $person['role'] = '';
551
            if ($roleNodes->length > 0) {
552
                $person['role'] = $roleNodes->item(0)->nodeValue;
553
            }
554
555
            $person['fobId'] = '';
556
            if ($identifierNodes->length > 0) {
557
                $person['fobId'] = $identifierNodes->item(0)->nodeValue;
558
            }
559
560
            $person['index'] = $key;
561
            $persons[] = $person;
562
        }
563
564
        if ($role) {
565
            $result = [];
566
            foreach ($persons as $person) {
567
                if ($person['role'] == $role)
568
                    $result[] = $person;
569
            }
570
            return $result;
571
        } else {
572
            return $persons;
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();
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

600
        /** @scrutinizer ignore-call */ 
601
        $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...
601
        $this->setValue($fisIdXpath, $fisId);
602
    }
603
604
    /**
605
     * @param string $xpathString
606
     * @return string
607
     */
608
    protected function getValue($xpathString)
609
    {
610
        $xpath = $this->getXpath();
611
        $nodeList = $xpath->query(self::rootNode . $xpathString);
612
        if ($nodeList->length > 0) {
613
            return $nodeList->item(0)->nodeValue;
614
        }
615
        return '';
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);
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

631
            $parserGenerator->customXPath(/** @scrutinizer ignore-type */ $xpathString,true, $value);
Loading history...
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() {
641
        $xpath = $this->getXpath();
642
        $fileXpath = $this->clientConfigurationManager->getFileXpath();
643
        $fileNodes = $xpath->query(self::rootNode . $fileXpath);
644
        foreach ($fileNodes as $fileNode) {
645
            $fileNode->parentNode->removeChild($fileNode);
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) {
0 ignored issues
show
introduced by
$fileNode is of type DOMNode, thus it always evaluated to true.
Loading history...
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);
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

677
                    $newField = $this->xml->importNode(/** @scrutinizer ignore-type */ $dom->firstChild, true);
Loading history...
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)
689
    {
690
        $fileId = new FileId($files);
691
692
        $xpath = $this->getXpath();
693
694
        $fileXpath = $this->clientConfigurationManager->getFileXpath();
695
        $mimeTypeXpath = $this->clientConfigurationManager->getFileMimetypeXpath();
696
        $idXpath = $this->clientConfigurationManager->getFileIdXpath();
697
        $deletedXpath = $this->clientConfigurationManager->getFileDeletedXpath();
698
699
        /** @var File $file */
700
        foreach ($files as $file) {
701
702
            $dataStreamIdentifier = $file->getDatastreamIdentifier();
703
704
            if (!$file->isFileGroupDeleted()) {
705
706
                if ($file->isDeleted()) {
707
708
                    if (!empty($dataStreamIdentifier)) {
709
                        /** @var XPathXMLGenerator $xPathXMLGenerator */
710
                        $xPathXMLGenerator = new XPathXMLGenerator();
711
                        $xPathXMLGenerator->generateXmlFromXPath($fileXpath);
712
713
                        // FIXME: XPATHXmlGenerator XPATH does not generate any namespaces,
714
                        // which DOMDocument cannot cope with. Actually, namespaces should not be necessary here,
715
                        // since it is about child elements that are then added to the overall XML.
716
                        libxml_use_internal_errors(true);
717
                        $dom = new \DOMDocument();
718
                        $domLoaded = $dom->loadXML($xPathXMLGenerator->getXML());
719
                        libxml_use_internal_errors(false);
720
721
                        if ($domLoaded) {
722
                            $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

722
                            $newFile = $this->xml->importNode(/** @scrutinizer ignore-type */ $dom->firstChild, true);
Loading history...
723
                            $newFile->setAttribute('usage', 'delete');
724
                            $this->setFileData($newFile, $idXpath, $file->getDatastreamIdentifier());
725
                            $this->setFileData($newFile, $deletedXpath, 'yes');
726
                            $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

726
                            $this->xml->firstChild->/** @scrutinizer ignore-call */ 
727
                                                    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...
727
                        }
728
                    }
729
                } else {
730
                    $fileNodes = $xpath->query(
731
                        self::rootNode . $fileXpath . '[./'.trim($idXpath, '@/ ').'="'.$file->getFileIdentifier().'"]'
732
                    );
733
734
                    if ($fileNodes->length > 0) {
735
                        $this->setFileData($fileNodes->item(0), $idXpath, $fileId->getId($file));
736
                        $this->setFileData($fileNodes->item(0), $mimeTypeXpath, $file->getContentType());
737
                    }
738
                }
739
            }
740
        }
741
    }
742
}
743