Passed
Pull Request — master (#195)
by
unknown
07:19
created

InternalFormat::setProcessNumber()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
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
    public function __construct($xml)
40
    {
41
        $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(ObjectManager::class);
42
        $this->clientConfigurationManager = $objectManager->get(ClientConfigurationManager::class);
43
44
        $this->setXml($xml);
45
    }
46
47
    public function setXml($xml)
48
    {
49
        $dom = new \DOMDocument();
50
        $dom->loadXML($xml);
51
        $this->xml = $dom;
52
    }
53
54
    public function getXml()
55
    {
56
        return $this->xml->saveXML();
57
    }
58
59
    public function getDocument() {
60
        return $this->xml;
61
    }
62
63
    public function getXpath()
64
    {
65
        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...
66
    }
67
68
    public function getDocumentType()
69
    {
70
        $typeXpath = $this->clientConfigurationManager->getTypeXpath();
71
        return $this->getValue($typeXpath);
72
    }
73
74
    public function setDocumentType($type)
75
    {
76
        $typeXpath = $this->clientConfigurationManager->getTypeXpath();
77
        return $this->setValue($typeXpath, $type);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->setValue($typeXpath, $type) targeting EWW\Dpf\Helper\InternalFormat::setValue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
78
    }
79
80
    public function getRepositoryState()
81
    {
82
        $stateXpath = $this->clientConfigurationManager->getStateXpath();
83
        return $this->getValue($stateXpath);
84
    }
85
86
    public function setRepositoryState($state)
87
    {
88
        $stateXpath = $this->clientConfigurationManager->getStateXpath();
89
        $this->setValue($stateXpath,$state);
90
    }
91
92
    public function getProcessNumber()
93
    {
94
        $processNumberXpath = $this->clientConfigurationManager->getProcessNumberXpath();
95
        if ($processNumberXpath) {
96
            return $this->getValue($processNumberXpath);
97
        } else {
98
            return "";
99
        }
100
    }
101
102
    public function setProcessNumber($processNumber)
103
    {
104
        $processNumberXpath = $this->clientConfigurationManager->getProcessNumberXpath();
105
        $this->getValue($processNumberXpath, $processNumber);
0 ignored issues
show
Unused Code introduced by
The call to EWW\Dpf\Helper\InternalFormat::getValue() has too many arguments starting with $processNumber. ( Ignorable by Annotation )

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

105
        $this->/** @scrutinizer ignore-call */ 
106
               getValue($processNumberXpath, $processNumber);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
106
    }
107
108
    public function getTitle()
109
    {
110
        $titleXpath = $this->clientConfigurationManager->getTitleXpath();
111
        $xpath = $this->getXpath();
112
113
        if (!$titleXpath) {
114
            $titleXpath = "titleInfo/title";
115
        }
116
117
        $stateList = $xpath->query(self::rootNode . $titleXpath);
118
        return $stateList->item(0)->nodeValue;
119
    }
120
121
    /**
122
     * @param string $title
123
     */
124
    public function setTitle($title)
125
    {
126
        $titleXpath = $this->clientConfigurationManager->getTitleXpath();
127
        $this->setValue($titleXpath, $title);
128
    }
129
130
    public function getFiles()
131
    {
132
        $xpath = $this->getXpath();
133
134
        $fileXpath = $this->clientConfigurationManager->getFileXpath();
135
136
        $fileNodes = $xpath->query(self::rootNode . $fileXpath);
137
        $files = [];
138
139
        foreach ($fileNodes as $file) {
140
            $fileAttrArray = [];
141
            foreach ($file->childNodes as $fileAttributes) {
142
                $fileAttrArray[$fileAttributes->tagName] = $fileAttributes->nodeValue;
143
            }
144
            $files[] = $fileAttrArray;
145
        }
146
147
        return $files;
148
149
    }
150
151
    public function setDateIssued($date) {
152
        $dateXpath = $this->clientConfigurationManager->getDateXpath();
153
        $this->setValue($dateXpath, $date);
154
    }
155
156
    public function getDateIssued() {
157
        $dateXpath = $this->clientConfigurationManager->getDateXpath();
158
        return $this->getValue($dateXpath);
159
    }
160
161
    public function removeDateIssued()
162
    {
163
        $xpath = $this->getXpath();
164
        $dateXpath = $this->clientConfigurationManager->getDateXpath();
165
166
        $dateNodes = $xpath->query(self::rootNode . $dateXpath);
167
        if ($dateNodes->length > 0) {
168
            $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

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

317
        $sourceDetailsXpathList = explode(";", trim(/** @scrutinizer ignore-type */ $sourceDetailsXpaths," ;"));
Loading history...
318
        $dataNodes = [];
319
320
        foreach ($sourceDetailsXpathList as $sourceDetailsXpathItem) {
321
            $dataNodes[] = $xpath->query(self::rootNode . trim($sourceDetailsXpathItem));
322
        }
323
324
        foreach ($dataNodes as $dataNode) {
325
            foreach ($dataNode as $node) {
326
                if ($node->hasChildNodes()) {
327
                    foreach ($node->childNodes as $n) {
328
                        $data[] = preg_replace('/\s+/', ' ', $n->textContent);
329
                    }
330
                } else {
331
                    $data[] = preg_replace('/\s+/', ' ', $node->textContent);
332
                }
333
            }
334
        }
335
336
        $output = trim(implode(' ', $data));
337
        $output = preg_replace('/\s+/ ', ' ', $output);
338
        return $output;
339
    }
340
341
    /**
342
     * Get all related FOB-IDs
343
     *
344
     * @return array
345
     */
346
    public function getPersonFisIdentifiers(): array
347
    {
348
        $xpath = $this->getXpath();
349
        $personXpath = $this->clientConfigurationManager->getPersonXpath();
350
        $fisIdentifierXpath =  $this->clientConfigurationManager->getPersonFisIdentifierXpath();
351
        $personNodes = $xpath->query(self::rootNode . $personXpath);
352
        $identifiers = [];
353
        foreach ($personNodes as $key => $node) {
354
            $identifierNodes = $xpath->query($fisIdentifierXpath, $node);
355
            if ($identifierNodes->length > 0) {
356
                $identifiers[] = $identifierNodes->item(0)->nodeValue;
357
            }
358
        }
359
360
        return $identifiers;
361
    }
362
363
    /**
364
     * @return string
365
     */
366
    public function getDepositLicense()
367
    {
368
        $depositLicenseXpath = $this->clientConfigurationManager->getDepositLicenseXpath();
369
        return $this->getValue($depositLicenseXpath);
370
    }
371
372
    /**
373
     * @return array
374
     */
375
    public function getNotes()
376
    {
377
        $notesXpath = $this->clientConfigurationManager->getAllNotesXpath();
378
379
        $xpath = $this->getXpath();
380
        $notesNodes = $xpath->query(self::rootNode . $notesXpath);
381
382
        $notes = array();
383
384
        for ($i=0; $i < $notesNodes->length; $i++)
385
        {
386
            $notes[] = $notesNodes->item($i)->nodeValue;
387
        }
388
389
        return $notes;
390
    }
391
392
    public function addNote($noteContent)
393
    {
394
        $notesXpath = $this->clientConfigurationManager->getPrivateNotesXpath();
395
396
        $parserGenerator = new ParserGenerator();
397
        $parserGenerator->setXml($this->xml->saveXML());
398
        $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

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

526
        /** @scrutinizer ignore-call */ 
527
        $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...
527
        $this->setValue($fisIdXpath, $fisId);
528
    }
529
530
    /**
531
     * @param string $xpathString
532
     * @return string
533
     */
534
    protected function getValue($xpathString)
535
    {
536
        $xpath = $this->getXpath();
537
        $nodeList = $xpath->query(self::rootNode . $xpathString);
538
        return $nodeList->item(0)->nodeValue;
539
    }
540
541
    /**
542
     * @param string $xpathString
543
     * @param string $value
544
     */
545
    protected function setValue($xpathString, $value)
546
    {
547
        $xpath = $this->getXpath();
548
        $nodes = $xpath->query(self::rootNode . $xpathString);
549
        if ($nodes->length > 0) {
550
            $nodes->item(0)->nodeValue = $value;
551
        } elseif(!empty($value)) {
552
            $parserGenerator = new ParserGenerator();
553
            $parserGenerator->setXml($this->xml->saveXML());
554
            $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

554
            $parserGenerator->customXPath(/** @scrutinizer ignore-type */ $xpathString,true, $value);
Loading history...
555
            $this->xml = new \DOMDocument();
556
            $this->xml->loadXML($parserGenerator->getXMLData());
557
        }
558
    }
559
560
}
561