Passed
Pull Request — master (#195)
by
unknown
08:51
created

InternalFormat::getSubmitterEmail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 9
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
        if (empty($xml)) {
50
            $xml = "<data></data>";
51
        }
52
53
        $dom = new \DOMDocument();
54
        $dom->loadXML($xml);
55
        $this->xml = $dom;
56
    }
57
58
    public function getXml()
59
    {
60
        return $this->xml->saveXML();
61
    }
62
63
    public function getDocument() {
64
        return $this->xml;
65
    }
66
67
    public function getXpath()
68
    {
69
        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...
70
    }
71
72
    public function getDocumentType()
73
    {
74
        $typeXpath = $this->clientConfigurationManager->getTypeXpath();
75
        return $this->getValue($typeXpath);
76
    }
77
78
    public function setDocumentType($type)
79
    {
80
        $typeXpath = $this->clientConfigurationManager->getTypeXpath();
81
        $this->setValue($typeXpath, $type);
82
    }
83
84
    public function getRepositoryState()
85
    {
86
        $stateXpath = $this->clientConfigurationManager->getStateXpath();
87
        return $this->getValue($stateXpath);
88
    }
89
90
    public function setRepositoryState($state)
91
    {
92
        $stateXpath = $this->clientConfigurationManager->getStateXpath();
93
        $this->setValue($stateXpath,$state);
94
    }
95
96
    public function getProcessNumber()
97
    {
98
        $processNumberXpath = $this->clientConfigurationManager->getProcessNumberXpath();
99
        if ($processNumberXpath) {
100
            return $this->getValue($processNumberXpath);
101
        } else {
102
            return "";
103
        }
104
    }
105
106
    public function setProcessNumber($processNumber)
107
    {
108
        $processNumberXpath = $this->clientConfigurationManager->getProcessNumberXpath();
109
        $this->setValue($processNumberXpath, $processNumber);
110
    }
111
112
    public function getTitle()
113
    {
114
        $titleXpath = $this->clientConfigurationManager->getTitleXpath();
115
        $xpath = $this->getXpath();
116
117
        if (!$titleXpath) {
118
            $titleXpath = "titleInfo/title";
119
        }
120
121
        $stateList = $xpath->query(self::rootNode . $titleXpath);
122
        return $stateList->item(0)->nodeValue;
123
    }
124
125
    /**
126
     * @param string $title
127
     */
128
    public function setTitle($title)
129
    {
130
        $titleXpath = $this->clientConfigurationManager->getTitleXpath();
131
        $this->setValue($titleXpath, $title);
132
    }
133
134
    public function getFiles()
135
    {
136
        $xpath = $this->getXpath();
137
138
        $fileXpath = $this->clientConfigurationManager->getFileXpath();
139
140
        $fileNodes = $xpath->query(self::rootNode . $fileXpath);
141
        $files = [];
142
143
        foreach ($fileNodes as $file) {
144
            $fileAttrArray = [];
145
            foreach ($file->childNodes as $fileAttributes) {
146
                $fileAttrArray[$fileAttributes->tagName] = $fileAttributes->nodeValue;
147
            }
148
            $files[] = $fileAttrArray;
149
        }
150
151
        return $files;
152
153
    }
154
155
    public function setDateIssued($date) {
156
        $dateXpath = $this->clientConfigurationManager->getDateXpath();
157
        $this->setValue($dateXpath, $date);
158
    }
159
160
    public function getDateIssued() {
161
        $dateXpath = $this->clientConfigurationManager->getDateXpath();
162
        return $this->getValue($dateXpath);
163
    }
164
165
    public function removeDateIssued()
166
    {
167
        $xpath = $this->getXpath();
168
        $dateXpath = $this->clientConfigurationManager->getDateXpath();
169
170
        $dateNodes = $xpath->query(self::rootNode . $dateXpath);
171
        if ($dateNodes->length > 0) {
172
            $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

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

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

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

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

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