Passed
Push — master ( 580f3f...4c2e3f )
by Xavier
01:18
created

EutilsEfetch::getIssns()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 0
crap 3
1
<?php
2
3
namespace PubPeerFoundation\PublicationDataExtractor\Resources\Extractors;
4
5
use PubPeerFoundation\PublicationDataExtractor\Helpers\DateHelper;
6
7
class EutilsEfetch implements Extractor, ProvidesPublicationData, ProvidesIdentifiersData, ProvidesAuthorsData, ProvidesJournalData
8
{
9
    private $document;
10
11
    private $searchTree;
12
13
    private $output = [];
14
    private $identifier;
15
16 8
    public function __construct($document, $identifier)
17
    {
18 8
        $this->document = $document;
19 8
        $this->identifier = $identifier;
20 8
    }
21
22 4
    public function extract(): array
23
    {
24 4
        $this->getDataFromDocument();
25
26 4
        $this->extractAuthorsData();
27 4
        $this->extractIdentifiersData();
28 4
        $this->extractJournalData();
29 4
        $this->extractPublicationData();
30
31 4
        return $this->output;
32
    }
33
34 4
    protected function getDataFromDocument()
35
    {
36 4
        $this->searchTree = $this->document->{'PubmedArticle'};
37 4
    }
38
39
    /**
40
     * Extract and format data needed for the Publication Model.
41
     */
42 4
    public function extractPublicationData()
43
    {
44 4
        $this->output['publication'] = [
45 4
            'title' => (string) $this->searchTree->MedlineCitation->Article->ArticleTitle ?? null,
46 4
            'url' => (string) 'http://www.ncbi.nlm.nih.gov/pubmed/'.$this->searchTree->MedlineCitation->PMID,
47 4
            'published_at' => (new DateHelper)->dateFromPubDate($this->searchTree->MedlineCitation->Article->Journal->JournalIssue->PubDate),
48 4
            'abstract' => (string) $this->searchTree->MedlineCitation->Article->Abstract->AbstractText ?? null,
49
        ];
50 4
    }
51
52
    /**
53
     * Extract and format data needed for the Identifiers Relationship
54
     * on the Publication Model.
55
     */
56 4
    public function extractIdentifiersData()
57
    {
58 4
        foreach ($this->searchTree->PubmedData->ArticleIdList->ArticleId as $identifier) {
59 4
            $this->output['identifiers'][] = [
60 4
                'value' => (string) $identifier,
61 4
                'type' => (string) $identifier['IdType'],
62
            ];
63
        }
64
65 4
        if ($this->searchTree->MedlineCitation->Article->Journal->ISSN) {
66 4
            $this->output['identifiers'][] = [
67 4
                'value' => (string) $this->searchTree->MedlineCitation->Article->Journal->ISSN,
68 4
                'type' => 'issn',
69
            ];
70
        }
71 4
    }
72
73
    /**
74
     * Extract and format data needed for the Journals Relationship
75
     * on the Publication Model.
76
     */
77 4
    public function extractJournalData()
78
    {
79 4
        $this->output['journals'] = [
80
            'title' => (string) $this->searchTree->MedlineCitation->Article->Journal->Title ?? null,
81 4
            'issn' => $this->getIssns(),
82 4
        ];
83
    }
84
85 4
    /**
86 4
     * Extract and format data needed for the Authors Relationship
87
     * on the Publication Model.
88
     */
89 4
    public function extractAuthorsData()
90 4
    {
91 4
        try {
92
            foreach ($this->searchTree->MedlineCitation->Article->AuthorList->Author as $author) {
93 4
                $this->output['authors'][] = [
94
                    'first_name' => (string) $author->ForeName,
95
                    'last_name' => (string) $author->LastName,
96
                    'affiliation' => (string) $author->AffiliationInfo->Affiliation,
97
                ];
98
            }
99 8
        } catch (\Exception $e) {
100
            // If can't find authors, just don't do anything.
101
        }
102 8
    }
103 7
104 7
    protected function getIssns()
105 7
    {
106 7
        $issn = [];
107
108
        if ($this->searchTree->MedlineCitation->Article->Journal->ISSN) {
109 1
            $issn[] = (string) $this->searchTree->MedlineCitation->Article->Journal->ISSN;
110
        }
111 8
112
        if ($this->searchTree->MedlineCitation->MedlineJournalInfo->ISSNLinking) {
113
            $issn[] = (string) $this->searchTree->MedlineCitation->MedlineJournalInfo->ISSNLinking;
114
        }
115
116
        return $issn;
117
    }
118
}
119