Passed
Push — master ( 134b43...f02d33 )
by Ralf
09:50
created

InternalFormat::getSourceDetails()   B

Complexity

Conditions 8
Paths 11

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
c 0
b 0
f 0
dl 0
loc 33
rs 8.4444
cc 8
nc 11
nop 0
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 TYPO3\CMS\Extbase\Object\ObjectManager;
20
21
class InternalFormat
22
{
23
    const rootNode = '//data/';
24
25
    /**
26
     * clientConfigurationManager
27
     *
28
     * @var \EWW\Dpf\Configuration\ClientConfigurationManager
29
     */
30
    protected $clientConfigurationManager;
31
32
    /**
33
     * xml
34
     *
35
     * @var \DOMDocument
36
     */
37
    protected $xml;
38
39
    /**
40
     * @var int
41
     */
42
    protected $clientPid = 0;
43
44
    /**
45
     * InternalFormat constructor.
46
     * @param string $xml
47
     * @param int $clientPid
48
     */
49
    public function __construct(string $xml, $clientPid = 0)
50
    {
51
        $this->clientPid = $clientPid;
52
53
        $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(ObjectManager::class);
54
        $this->clientConfigurationManager = $objectManager->get(ClientConfigurationManager::class);
55
56
        if ($clientPid) {
57
            $this->clientConfigurationManager->setConfigurationPid($clientPid);
58
        }
59
60
        $this->setXml($xml);
61
    }
62
63
    public function setXml($xml)
64
    {
65
        if (empty($xml)) {
66
            $xml = "<data></data>";
67
        }
68
69
        $dom = new \DOMDocument();
70
        $dom->loadXML($xml);
71
        $this->xml = $dom;
72
    }
73
74
    public function getXml()
75
    {
76
        return $this->xml->saveXML();
77
    }
78
79
    public function getDocument() {
80
        return $this->xml;
81
    }
82
83
    public function getXpath()
84
    {
85
        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...
86
    }
87
88
    public function getDocumentType()
89
    {
90
        $typeXpath = $this->clientConfigurationManager->getTypeXpath();
91
        return $this->getValue($typeXpath);
92
    }
93
94
    public function setDocumentType($type)
95
    {
96
        $typeXpath = $this->clientConfigurationManager->getTypeXpath();
97
        $this->setValue($typeXpath, $type);
98
    }
99
100
    public function getRepositoryState()
101
    {
102
        $stateXpath = $this->clientConfigurationManager->getStateXpath();
103
        return $this->getValue($stateXpath);
104
    }
105
106
    public function setRepositoryState($state)
107
    {
108
        $stateXpath = $this->clientConfigurationManager->getStateXpath();
109
        $this->setValue($stateXpath,$state);
110
    }
111
112
    public function getProcessNumber()
113
    {
114
        $processNumberXpath = $this->clientConfigurationManager->getProcessNumberXpath();
115
        if ($processNumberXpath) {
116
            return $this->getValue($processNumberXpath);
117
        } else {
118
            return "";
119
        }
120
    }
121
122
    public function setProcessNumber($processNumber)
123
    {
124
        $processNumberXpath = $this->clientConfigurationManager->getProcessNumberXpath();
125
        $this->setValue($processNumberXpath, $processNumber);
126
    }
127
128
    public function getTitle()
129
    {
130
        $titleXpath = $this->clientConfigurationManager->getTitleXpath();
131
        $xpath = $this->getXpath();
132
133
        if (!$titleXpath) {
134
            $titleXpath = "titleInfo/title";
135
        }
136
137
        $stateList = $xpath->query(self::rootNode . $titleXpath);
138
        return $stateList->item(0)->nodeValue;
139
    }
140
141
    /**
142
     * @param string $title
143
     */
144
    public function setTitle($title)
145
    {
146
        $titleXpath = $this->clientConfigurationManager->getTitleXpath();
147
        $this->setValue($titleXpath, $title);
148
    }
149
150
    public function getFiles()
151
    {
152
        $xpath = $this->getXpath();
153
154
        $fileXpath = $this->clientConfigurationManager->getFileXpath();
155
156
        $fileNodes = $xpath->query(self::rootNode . $fileXpath);
157
        $files = [];
158
159
        foreach ($fileNodes as $file) {
160
            $fileAttrArray = [];
161
            foreach ($file->childNodes as $fileAttributes) {
162
                $fileAttrArray[$fileAttributes->tagName] = $fileAttributes->nodeValue;
163
            }
164
            $files[] = $fileAttrArray;
165
        }
166
167
        return $files;
168
169
    }
170
171
    public function setDateIssued($date) {
172
        $dateXpath = $this->clientConfigurationManager->getDateXpath();
173
        $this->setValue($dateXpath, $date);
174
    }
175
176
    public function getDateIssued() {
177
        $dateXpath = $this->clientConfigurationManager->getDateXpath();
178
        return $this->getValue($dateXpath);
179
    }
180
181
    public function removeDateIssued()
182
    {
183
        $xpath = $this->getXpath();
184
        $dateXpath = $this->clientConfigurationManager->getDateXpath();
185
186
        $dateNodes = $xpath->query(self::rootNode . $dateXpath);
187
        if ($dateNodes->length > 0) {
188
            $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

188
            $dateNodes->item(0)->parentNode->/** @scrutinizer ignore-call */ 
189
                                             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...
189
        }
190
191
    }
192
193
    public function hasPrimaryUrn()
194
    {
195
        $xpath = $this->getXpath();
196
        $primaryUrnXpath = $this->clientConfigurationManager->getPrimaryUrnXpath();
197
198
        $urnNodes = $xpath->query(self::rootNode . $primaryUrnXpath);
199
        if ($urnNodes->length > 0) {
200
            return true;
201
        } else {
202
            return false;
203
        }
204
    }
205
206
    public function getPrimaryUrn()
207
    {
208
        $xpath = $this->getXpath();
209
        $primaryUrnXpath = $this->clientConfigurationManager->getPrimaryUrnXpath();
210
211
        $urnNodes = $xpath->query(self::rootNode . $primaryUrnXpath);
212
        if ($urnNodes->length > 0) {
213
            return $urnNodes->item(0)->nodeValue;
214
        } else {
215
            return false;
216
        }
217
    }
218
219
    public function setPrimaryUrn($urn)
220
    {
221
        $primaryUrnXpath = $this->clientConfigurationManager->getPrimaryUrnXpath();
222
        $this->setValue($primaryUrnXpath, $urn);
223
    }
224
225
    public function clearAllUrn()
226
    {
227
        $xpath = $this->getXpath();
228
        $urnXpath = $this->clientConfigurationManager->getUrnXpath();
229
        $primaryUrnXpath = $this->clientConfigurationManager->getPrimaryUrnXpath();
230
231
        $urnNodes = $xpath->query(self::rootNode . $urnXpath);
232
        foreach ($urnNodes as $urnNode) {
233
            $urnNode->parentNode->removeChild($urnNode);
234
        }
235
236
        $primaryUrnNodes = $xpath->query(self::rootNode . $primaryUrnXpath);
237
        foreach ($primaryUrnNodes as $primaryUrnNode) {
238
            $primaryUrnNode->parentNode->removeChild($primaryUrnNode);
239
        }
240
    }
241
242
    public function getSubmitterEmail() {
243
        $xpath = $this->getXpath();
244
        $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...
245
246
        $dateNodes = $xpath->query(self::rootNode . $submitterXpath);
247
        if (!$dateNodes) {
0 ignored issues
show
introduced by
$dateNodes is of type DOMNodeList, thus it always evaluated to true.
Loading history...
248
            return '';
249
        } else {
250
            return $dateNodes->item(0)->nodeValue;
251
        }
252
253
    }
254
255
    public function getSubmitterName() {
256
        $xpath = $this->getXpath();
257
        $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...
258
259
        $dateNodes = $xpath->query(self::rootNode . $submitterXpath);
260
261
        if (!$dateNodes) {
0 ignored issues
show
introduced by
$dateNodes is of type DOMNodeList, thus it always evaluated to true.
Loading history...
262
            return '';
263
        } else {
264
            return $dateNodes->item(0)->nodeValue;
265
        }
266
    }
267
268
    public function getSubmitterNotice() {
269
        $xpath = $this->getXpath();
270
        $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...
271
272
        $dateNodes = $xpath->query(self::rootNode . $submitterXpath);
273
274
        if (!$dateNodes) {
0 ignored issues
show
introduced by
$dateNodes is of type DOMNodeList, thus it always evaluated to true.
Loading history...
275
            return '';
276
        } else {
277
            return $dateNodes->item(0)->nodeValue;
278
        }
279
    }
280
281
    public function getCreator()
282
    {
283
        $creatorXpath = $this->clientConfigurationManager->getCreatorXpath();
284
        return $this->getValue($creatorXpath);
285
    }
286
287
    public function setCreator($creator)
288
    {
289
        $creatorXpath = $this->clientConfigurationManager->getCreatorXpath();
290
        $this->setValue($creatorXpath, $creator);
291
    }
292
293
    public function getCreationDate()
294
    {
295
        $xpath = $this->clientConfigurationManager->getCreationDateXpath();
296
        return $this->getValue($xpath);
297
    }
298
299
    public function setCreationDate($creationDate)
300
    {
301
        $xpath = $this->clientConfigurationManager->getCreationDateXpath();
302
        $this->setValue($xpath, $creationDate);
303
    }
304
305
    public function getRepositoryCreationDate()
306
    {
307
        $xpath = $this->clientConfigurationManager->getRepositoryCreationDateXpath();
308
        return $this->getValue($xpath);
309
    }
310
311
    public function getRepositoryLastModDate()
312
    {
313
        $xpath = $this->clientConfigurationManager->getRepositoryLastModDateXpath();
314
        return $this->getValue($xpath);
315
    }
316
317
    public function getPublishingYear()
318
    {
319
        $publishingYearXpath = $this->clientConfigurationManager->getPublishingYearXpath();
320
        return $this->getValue($publishingYearXpath);
321
    }
322
323
    public function getOriginalSourceTitle()
324
    {
325
        $originalSourceTitleXpath = $this->clientConfigurationManager->getOriginalSourceTitleXpath();
326
        return $this->getValue($originalSourceTitleXpath);
327
    }
328
329
    /**
330
     * @return string
331
     */
332
    public function getSourceDetails()
333
    {
334
        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...
335
            return '';
336
        }
337
338
        $xpath = $this->getXpath();
339
        $data = [];
340
        $sourceDetailsXpaths = $this->clientConfigurationManager->getSourceDetailsXpaths();
341
        $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

341
        $sourceDetailsXpathList = explode(";", trim(/** @scrutinizer ignore-type */ $sourceDetailsXpaths," ;"));
Loading history...
342
        $dataNodes = [];
343
344
        foreach ($sourceDetailsXpathList as $sourceDetailsXpathItem) {
345
            $dataNodes[] = $xpath->query(self::rootNode . trim($sourceDetailsXpathItem));
346
        }
347
348
        foreach ($dataNodes as $dataNode) {
349
            if (is_iterable($dataNode)) {
350
                foreach ($dataNode as $node) {
351
                    if ($node->hasChildNodes()) {
352
                        foreach ($node->childNodes as $n) {
353
                            $data[] = preg_replace('/\s+/', ' ', $n->textContent);
354
                        }
355
                    } else {
356
                        $data[] = preg_replace('/\s+/', ' ', $node->textContent);
357
                    }
358
                }
359
            }
360
        }
361
362
        $output = trim(implode(' ', $data));
363
        $output = preg_replace('/\s+/ ', ' ', $output);
364
        return $output;
365
    }
366
367
    /**
368
     * Get all related FOB-IDs
369
     *
370
     * @return array
371
     */
372
    public function getPersonFisIdentifiers(): array
373
    {
374
        $xpath = $this->getXpath();
375
        $personXpath = $this->clientConfigurationManager->getPersonXpath();
376
        $fisIdentifierXpath =  $this->clientConfigurationManager->getPersonFisIdentifierXpath();
377
        $personNodes = $xpath->query(self::rootNode . $personXpath);
378
        $identifiers = [];
379
        foreach ($personNodes as $key => $node) {
380
            $identifierNodes = $xpath->query($fisIdentifierXpath, $node);
381
            if ($identifierNodes->length > 0) {
382
                $identifiers[] = $identifierNodes->item(0)->nodeValue;
383
            }
384
        }
385
386
        return $identifiers;
387
    }
388
389
    /**
390
     * @return string
391
     */
392
    public function getDepositLicense()
393
    {
394
        $depositLicenseXpath = $this->clientConfigurationManager->getDepositLicenseXpath();
395
        return $this->getValue($depositLicenseXpath);
396
    }
397
398
    /**
399
     * @return array
400
     */
401
    public function getNotes()
402
    {
403
        $notesXpath = $this->clientConfigurationManager->getAllNotesXpath();
404
405
        $xpath = $this->getXpath();
406
        $notesNodes = $xpath->query(self::rootNode . $notesXpath);
407
408
        $notes = array();
409
410
        for ($i=0; $i < $notesNodes->length; $i++)
411
        {
412
            $notes[] = $notesNodes->item($i)->nodeValue;
413
        }
414
415
        return $notes;
416
    }
417
418
    public function addNote($noteContent)
419
    {
420
        $notesXpath = $this->clientConfigurationManager->getPrivateNotesXpath();
421
422
        $parserGenerator = new ParserGenerator($this->clientPid);
423
        $parserGenerator->setXml($this->xml->saveXML());
424
        $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

424
        $parserGenerator->customXPath(/** @scrutinizer ignore-type */ $notesXpath,true, $noteContent);
Loading history...
425
        $this->xml = new \DOMDocument();
426
        $this->xml->loadXML($parserGenerator->getXMLData());
427
    }
428
429
    public function getAuthors()
430
    {
431
        return $this->getPersons($this->clientConfigurationManager->getPersonAuthorRole());
432
    }
433
434
    public function getPublishers()
435
    {
436
        return $this->getPersons($this->clientConfigurationManager->getPersonPublisherRole());
437
    }
438
439
    /**
440
     * Get persons of the given role
441
     *
442
     * @param string $role
443
     * @return array
444
     */
445
    public function getPersons($role = '')
446
    {
447
        $personXpath = $this->clientConfigurationManager->getPersonXpath();
448
        $familyXpath = $this->clientConfigurationManager->getPersonFamilyXpath();
449
        $givenXpath = $this->clientConfigurationManager->getPersonGivenXpath();
450
        $roleXpath = $this->clientConfigurationManager->getPersonRoleXpath();
451
        $fisIdentifierXpath =  $this->clientConfigurationManager->getPersonFisIdentifierXpath();
452
        $affiliationXpath =  $this->clientConfigurationManager->getPersonAffiliationXpath();
453
        $affiliationIdentifierXpath =  $this->clientConfigurationManager->getPersonAffiliationIdentifierXpath();
454
455
        $xpath = $this->getXpath();
456
        $personNodes = $xpath->query(self::rootNode . $personXpath);
457
458
        $persons = [];
459
460
        foreach ($personNodes as $key => $personNode) {
461
            $familyNodes = $xpath->query($familyXpath, $personNode);
462
            $givenNodes = $xpath->query($givenXpath, $personNode);
463
            $roleNodes = $xpath->query($roleXpath, $personNode);
464
            $identifierNodes = $xpath->query($fisIdentifierXpath, $personNode);
465
            $affiliationNodes = $xpath->query($affiliationXpath, $personNode);
466
            $affiliationIdentifierNodes = $xpath->query($affiliationIdentifierXpath, $personNode);
467
468
            $person['affiliations'] = [];
469
            foreach ($affiliationNodes as $key => $affiliationNode) {
0 ignored issues
show
Comprehensibility Bug introduced by
$key is overwriting a variable from outer foreach loop.
Loading history...
470
                $person['affiliations'][] = $affiliationNode->nodeValue;
471
            }
472
473
            $person['affiliationIdentifiers'] = [];
474
            foreach ($affiliationIdentifierNodes as $key => $affiliationIdentifierNode) {
475
                $person['affiliationIdentifiers'][] = $affiliationIdentifierNode->nodeValue;
476
            }
477
478
            $given = '';
479
            $family = '';
480
481
            if ($givenNodes->length > 0) {
482
                $given = $givenNodes->item(0)->nodeValue;
483
            }
484
485
            if ($familyNodes->length > 0) {
486
                $family = $familyNodes->item(0)->nodeValue;
487
            }
488
489
            $person['given'] = trim($given);
490
            $person['family'] = trim($family);
491
492
            $name = [];
493
            if ($person['given']) {
494
                $name[] = $person['given'];
495
            }
496
            if ($person['family']) {
497
                $name[] = $person['family'];
498
            }
499
500
            $person['name'] = implode(' ', $name);
501
502
            $person['role'] = '';
503
            if ($roleNodes->length > 0) {
504
                $person['role'] = $roleNodes->item(0)->nodeValue;
505
            }
506
507
            $person['fobId'] = '';
508
            if ($identifierNodes->length > 0) {
509
                $person['fobId'] = $identifierNodes->item(0)->nodeValue;
510
            }
511
512
            $person['index'] = $key;
513
            $persons[] = $person;
514
        }
515
516
        if ($role) {
517
            $result = [];
518
            foreach ($persons as $person) {
519
                if ($person['role'] == $role)
520
                    $result[] = $person;
521
            }
522
            return $result;
523
        } else {
524
            return $persons;
525
        }
526
    }
527
528
    /**
529
     * @return bool
530
     */
531
    public function getValidation()
532
    {
533
        $validationXpath =  $this->clientConfigurationManager->getValidationXpath();
534
        $validation = $this->getValue($validationXpath);
535
        return (strtolower($validation) === 'true')? true : false;
536
    }
537
538
    /**
539
     * @param bool $validated
540
     */
541
    public function setValidation($validated)
542
    {
543
        $validationXpath =  $this->clientConfigurationManager->getValidationXpath();
544
        $this->setValue($validationXpath, ($validated? 'true' : 'false'));
545
    }
546
547
    /**
548
     * @param string $fisId
549
     */
550
    public function setFisId($fisId)
551
    {
552
        $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 getFileXpath()? ( Ignorable by Annotation )

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

552
        /** @scrutinizer ignore-call */ 
553
        $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...
553
        $this->setValue($fisIdXpath, $fisId);
554
    }
555
556
    /**
557
     * @param string $xpathString
558
     * @return string
559
     */
560
    protected function getValue($xpathString)
561
    {
562
        $xpath = $this->getXpath();
563
        $nodeList = $xpath->query(self::rootNode . $xpathString);
564
        if ($nodeList->length > 0) {
565
            return $nodeList->item(0)->nodeValue;
566
        }
567
        return '';
568
    }
569
570
    /**
571
     * @param string $xpathString
572
     * @param string $value
573
     */
574
    protected function setValue($xpathString, $value)
575
    {
576
        $xpath = $this->getXpath();
577
        $nodes = $xpath->query(self::rootNode . $xpathString);
578
        if ($nodes->length > 0) {
579
            $nodes->item(0)->nodeValue = $value;
580
        } elseif(!empty($value)) {
581
            $parserGenerator = new ParserGenerator($this->clientPid);
582
            $parserGenerator->setXml($this->xml->saveXML());
583
            $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

583
            $parserGenerator->customXPath(/** @scrutinizer ignore-type */ $xpathString,true, $value);
Loading history...
584
            $this->xml = new \DOMDocument();
585
            $this->xml->loadXML($parserGenerator->getXMLData());
586
        }
587
    }
588
589
}
590