Passed
Pull Request — master (#195)
by
unknown
06:45
created

PubMedMetadata   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 85
rs 10
wmc 16

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getYear() 0 13 2
A getTitle() 0 15 5
A getPersons() 0 22 3
A getPublicationType() 0 23 6
1
<?php
2
namespace EWW\Dpf\Domain\Model;
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\Services\ImportExternalMetadata\PubMedImporter;
18
19
/**
20
 * PubMedMetadata
21
 */
22
class PubMedMetadata extends ExternalMetadata
23
{
24
    public function getTitle(): string
25
    {
26
        $node = $this->getDataXpath()->query('/eSummaryResult/DocumentSummarySet/DocumentSummary/Title');
27
28
        if ($node->length == 1) {
29
            if ($node->item(0)->nodeValue) return $node->item(0)->nodeValue;
30
        }
31
32
        $node = $this->getDataXpath()->query('/eSummaryResult/DocumentSummarySet/DocumentSummary/BookTitle');
33
34
        if ($node->length == 1) {
35
            if ($node->item(0)->nodeValue) return $node->item(0)->nodeValue;
36
        }
37
38
        return '';
39
    }
40
41
    public function getPersons(): array
42
    {
43
        $xpath = $this->getDataXpath();
44
45
        $personList = [];
46
47
        $nodes = $xpath->query('/eSummaryResult/DocumentSummarySet/DocumentSummary/Authors/Author');
48
49
        foreach ($nodes as $person) {
50
51
            $name = ['family' => '', 'given' => ''];
52
53
            $namePartNodes =  $xpath->query('Name', $person);
54
55
            if ($namePartNodes->length > 0) {
56
                $name['family'] = $namePartNodes->item(0)->nodeValue;
57
            }
58
59
            $personList[] = $name;
60
        }
61
62
        return $personList;
63
    }
64
65
    public function getYear(): string
66
    {
67
        $xpath = $this->getDataXpath();
68
69
        $node = $xpath->query('/eSummaryResult/DocumentSummarySet/DocumentSummary/PubDate');
70
71
        if ($node->length > 0) {
72
            $year = $node->item(0)->nodeValue;
73
74
            return substr($year, 0, 4);
75
        }
76
77
        return '';
78
    }
79
80
    /**
81
     * @return string
82
     * @throws \Exception
83
     */
84
    public function getPublicationType(): string
85
    {
86
        $node = $this->getDataXpath()->query('/eSummaryResult/DocumentSummarySet/DocumentSummary/PubType/flag');
87
88
        // In PubMed a document can have more than one publication type.
89
        $types = [];
90
        if ($node->length == 1) {
91
            $types[] = $node->item(0)->nodeValue;
92
        } elseif ($node->length > 1) {
93
            foreach ($node as $typeNode) {
94
                $types[] = $typeNode->nodeValue;
95
            }
96
        }
97
98
        foreach ($types as $type) {
99
            // We are only interested in the first appearance of one of the defined PubMed types.
100
            // This is following the requirement description of the Ticket #716
101
            if (in_array($type, PubMedImporter::types())) {
102
                return $type;
103
            };
104
        }
105
106
        return '';
107
    }
108
109
}
110