Passed
Push — master ( f02d33...dc5694 )
by Ralf
19:35
created

InternalFormat::getFiles()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 61
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 61
rs 7.3333
c 0
b 0
f 0
cc 10
nc 10
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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);
0 ignored issues
show
Bug introduced by
It seems like $typeXpath can also be of type null; however, parameter $xpathString of EWW\Dpf\Helper\InternalFormat::setValue() 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

102
        $this->setValue(/** @scrutinizer ignore-type */ $typeXpath, $type);
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $stateXpath can also be of type null; however, parameter $xpathString of EWW\Dpf\Helper\InternalFormat::setValue() 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

114
        $this->setValue(/** @scrutinizer ignore-type */ $stateXpath,$state);
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $processNumberXpath can also be of type null; however, parameter $xpathString of EWW\Dpf\Helper\InternalFormat::setValue() 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

130
        $this->setValue(/** @scrutinizer ignore-type */ $processNumberXpath, $processNumber);
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $titleXpath can also be of type null; however, parameter $xpathString of EWW\Dpf\Helper\InternalFormat::setValue() 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

152
        $this->setValue(/** @scrutinizer ignore-type */ $titleXpath, $title);
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $dateXpath can also be of type null; however, parameter $xpathString of EWW\Dpf\Helper\InternalFormat::setValue() 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

221
        $this->setValue(/** @scrutinizer ignore-type */ $dateXpath, $date);
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $primaryUrnXpath can also be of type null; however, parameter $xpathString of EWW\Dpf\Helper\InternalFormat::setValue() 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

270
        $this->setValue(/** @scrutinizer ignore-type */ $primaryUrnXpath, $urn);
Loading history...
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
    /**
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);
0 ignored issues
show
Bug introduced by
It seems like $creatorXpath can also be of type null; however, parameter $xpathString of EWW\Dpf\Helper\InternalFormat::setValue() 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

350
        $this->setValue(/** @scrutinizer ignore-type */ $creatorXpath, $creator);
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $xpath can also be of type null; however, parameter $xpathString of EWW\Dpf\Helper\InternalFormat::setValue() 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

362
        $this->setValue(/** @scrutinizer ignore-type */ $xpath, $creationDate);
Loading history...
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)) {
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...
395
            return '';
396
        }
397
398
        $xpath = $this->getXpath();
399
        $data = [];
400
        $sourceDetailsXpaths = $this->clientConfigurationManager->getSourceDetailsXpaths();
401
        $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

401
        $sourceDetailsXpathList = explode(";", trim(/** @scrutinizer ignore-type */ $sourceDetailsXpaths," ;"));
Loading history...
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)
479
    {
480
        $notesXpath = $this->clientConfigurationManager->getPrivateNotesXpath();
481
482
        $parserGenerator = new ParserGenerator($this->clientPid);
483
        $parserGenerator->setXml($this->xml->saveXML());
484
        $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

484
        $parserGenerator->customXPath(/** @scrutinizer ignore-type */ $notesXpath,true, $noteContent);
Loading history...
485
        $this->xml = new \DOMDocument();
486
        $this->xml->loadXML($parserGenerator->getXMLData());
487
    }
488
489
    public function getAuthors()
490
    {
491
        return $this->getPersons($this->clientConfigurationManager->getPersonAuthorRole());
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 = '')
506
    {
507
        $personXpath = $this->clientConfigurationManager->getPersonXpath();
508
        $familyXpath = $this->clientConfigurationManager->getPersonFamilyXpath();
509
        $givenXpath = $this->clientConfigurationManager->getPersonGivenXpath();
510
        $roleXpath = $this->clientConfigurationManager->getPersonRoleXpath();
511
        $fisIdentifierXpath =  $this->clientConfigurationManager->getPersonFisIdentifierXpath();
512
        $affiliationXpath =  $this->clientConfigurationManager->getPersonAffiliationXpath();
513
        $affiliationIdentifierXpath =  $this->clientConfigurationManager->getPersonAffiliationIdentifierXpath();
514
515
        $xpath = $this->getXpath();
516
        $personNodes = $xpath->query(self::rootNode . $personXpath);
517
518
        $persons = [];
519
520
        foreach ($personNodes as $key => $personNode) {
521
            $familyNodes = $xpath->query($familyXpath, $personNode);
522
            $givenNodes = $xpath->query($givenXpath, $personNode);
523
            $roleNodes = $xpath->query($roleXpath, $personNode);
524
            $identifierNodes = $xpath->query($fisIdentifierXpath, $personNode);
525
            $affiliationNodes = $xpath->query($affiliationXpath, $personNode);
526
            $affiliationIdentifierNodes = $xpath->query($affiliationIdentifierXpath, $personNode);
527
528
            $person['affiliations'] = [];
529
            foreach ($affiliationNodes as $key => $affiliationNode) {
0 ignored issues
show
Comprehensibility Bug introduced by
$key is overwriting a variable from outer foreach loop.
Loading history...
530
                $person['affiliations'][] = $affiliationNode->nodeValue;
531
            }
532
533
            $person['affiliationIdentifiers'] = [];
534
            foreach ($affiliationIdentifierNodes as $key => $affiliationIdentifierNode) {
535
                $person['affiliationIdentifiers'][] = $affiliationIdentifierNode->nodeValue;
536
            }
537
538
            $given = '';
539
            $family = '';
540
541
            if ($givenNodes->length > 0) {
542
                $given = $givenNodes->item(0)->nodeValue;
543
            }
544
545
            if ($familyNodes->length > 0) {
546
                $family = $familyNodes->item(0)->nodeValue;
547
            }
548
549
            $person['given'] = trim($given);
550
            $person['family'] = trim($family);
551
552
            $name = [];
553
            if ($person['given']) {
554
                $name[] = $person['given'];
555
            }
556
            if ($person['family']) {
557
                $name[] = $person['family'];
558
            }
559
560
            $person['name'] = implode(' ', $name);
561
562
            $person['role'] = '';
563
            if ($roleNodes->length > 0) {
564
                $person['role'] = $roleNodes->item(0)->nodeValue;
565
            }
566
567
            $person['fobId'] = '';
568
            if ($identifierNodes->length > 0) {
569
                $person['fobId'] = $identifierNodes->item(0)->nodeValue;
570
            }
571
572
            $person['index'] = $key;
573
            $persons[] = $person;
574
        }
575
576
        if ($role) {
577
            $result = [];
578
            foreach ($persons as $person) {
579
                if ($person['role'] == $role)
580
                    $result[] = $person;
581
            }
582
            return $result;
583
        } else {
584
            return $persons;
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'));
0 ignored issues
show
Bug introduced by
It seems like $validationXpath can also be of type null; however, parameter $xpathString of EWW\Dpf\Helper\InternalFormat::setValue() 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

604
        $this->setValue(/** @scrutinizer ignore-type */ $validationXpath, ($validated? 'true' : 'false'));
Loading history...
605
    }
606
607
    /**
608
     * @param string $fisId
609
     */
610
    public function setFisId($fisId)
611
    {
612
        $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

612
        /** @scrutinizer ignore-call */ 
613
        $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...
613
        $this->setValue($fisIdXpath, $fisId);
614
    }
615
616
    /**
617
     * @param string $xpathString
618
     * @return string
619
     */
620
    protected function getValue($xpathString)
621
    {
622
        $xpath = $this->getXpath();
623
        $nodeList = $xpath->query(self::rootNode . $xpathString);
624
        if ($nodeList->length > 0) {
625
            return $nodeList->item(0)->nodeValue;
626
        }
627
        return '';
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);
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

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

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

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

738
                            $this->xml->firstChild->/** @scrutinizer ignore-call */ 
739
                                                    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...
739
                        }
740
                    }
741
                } else {
742
                    $fileNodes = $xpath->query(
743
                        self::rootNode . $fileXpath . '[./'.trim($idXpath, '@/ ').'="'.$file->getFileIdentifier().'"]'
744
                    );
745
746
                    if ($fileNodes->length > 0) {
747
                        $this->setFileData($fileNodes->item(0), $idXpath, $fileId->getId($file));
748
                        $this->setFileData($fileNodes->item(0), $mimeTypeXpath, $file->getContentType());
749
                    }
750
                }
751
            }
752
        }
753
    }
754
}
755