Passed
Push — master ( 287c0c...c3bafb )
by Xavier
07:46
created

EutilsEfetch   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
dl 0
loc 142
ccs 49
cts 49
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A extractIdentifiersData() 0 12 3
A extractPublicationData() 0 7 1
A getDataFromDocument() 0 3 1
A extractJournalData() 0 5 1
A getIssns() 0 13 3
A getEmailsFromAffiliations() 0 3 1
A loopOverAuthors() 0 4 2
A loopOverAffiliations() 0 8 2
A createAuthorEntry() 0 10 2
A extractAuthorsData() 0 5 2
1
<?php
2
3
namespace PubPeerFoundation\PublicationDataExtractor\Resources\Extractors;
4
5
use Tightenco\Collect\Support\Arr;
6
7
class EutilsEfetch extends Extractor implements ProvidesPublicationData, ProvidesIdentifiersData, ProvidesAuthorsData, ProvidesJournalData
8
{
9
    /**
10
     * Create search tree.
11
     */
12
    protected function getDataFromDocument()
13
    {
14
        $this->searchTree = $this->document->{'PubmedArticle'};
15
    }
16 8
17
    /**
18 8
     * Extract and format data needed for the Publication Model.
19 8
     */
20 8
    public function extractPublicationData()
21
    {
22 4
        $this->output['publication'] = [
23
            'title' => get_string($this->searchTree, 'MedlineCitation.Article.ArticleTitle'),
24 4
            'url' => (string) 'http://www.ncbi.nlm.nih.gov/pubmed/'.get_string($this->searchTree, 'MedlineCitation.PMID'),
25
            'published_at' => date_from_pub_date(data_get($this->searchTree, 'MedlineCitation.Article.Journal.JournalIssue.PubDate')),
26 4
            'abstract' => get_string($this->searchTree, 'MedlineCitation.Article.Abstract.AbstractText'),
27 4
        ];
28 4
    }
29 4
30
    /**
31 4
     * Extract and format data needed for the Identifiers Relationship
32
     * on the Publication Model.
33
     */
34 4
    public function extractIdentifiersData()
35
    {
36 4
        foreach ($this->searchTree->PubmedData->ArticleIdList->ArticleId as $identifier) {
37 4
            $this->output['identifiers'][] = [
38
                'value' => (string) $identifier,
39
                'type' => (string) $identifier['IdType'],
40
            ];
41
        }
42 4
        if ($value = get_string($this->searchTree, 'MedlineCitation.Article.Journal.ISSN')) {
43
            $this->output['identifiers'][] = [
44 4
                'value' => $value,
45 4
                'type' => 'issn',
46 4
            ];
47 4
        }
48 4
    }
49
50 4
    /**
51
     * Extract and format data needed for the Journals Relationship
52
     * on the Publication Model.
53
     */
54
    public function extractJournalData()
55
    {
56 4
        $this->output['journal'] = [
57
            'title' => get_string($this->searchTree, 'MedlineCitation.Article.Journal.Title'),
58 4
            'issn' => $this->getIssns(),
59 4
        ];
60 4
    }
61 4
62
    /**
63
     * Extract and format data needed for the Authors Relationship
64
     * on the Publication Model.
65 4
     */
66 4
    public function extractAuthorsData()
67 4
    {
68 4
        try {
69
            $this->loopOverAuthors();
70
        } catch (\Exception $e) {
71 4
            // Empty catch block, don't want anything to happen in case of exception.
72
        }
73
    }
74
75
    /**
76
     * Get all available ISSNs values from the tree.
77 4
     *
78
     * @return array
79 4
     */
80
    protected function getIssns()
81 4
    {
82 4
        $issn = [];
83
84
        if ($number = get_string($this->searchTree, 'MedlineCitation.Article.Journal.ISSN')) {
85 4
            $issn[] = $number;
86 4
        }
87
88
        if ($number = get_string($this->searchTree, 'MedlineCitation.MedlineJournalInfo.ISSNLinking')) {
89 4
            $issn[] = $number;
90 4
        }
91 4
92
        return $issn;
93 4
    }
94
95
    /**
96
     * Loop over authors array.
97
     */
98
    protected function loopOverAuthors(): void
99 8
    {
100
        foreach ($this->searchTree->MedlineCitation->Article->AuthorList->Author as $author) {
101
            $this->createAuthorEntry($author);
102 8
        }
103 7
    }
104 7
105 7
    /**
106 7
     * Create an author entry in output.
107
     *
108
     * @param $author
109 1
     */
110
    protected function createAuthorEntry($author): void
111 8
    {
112
        if (! empty($lastName = get_string($author, 'LastName'))) {
113
            $affiliations = $this->loopOverAffiliations($author);
114
115
            $this->output['authors'][] = [
116
                'first_name' => get_string($author, 'ForeName'),
117
                'last_name' => $lastName,
118
                'email' => $this->getEmailsFromAffiliations($affiliations),
119
                'affiliation' => $affiliations,
120
            ];
121
        }
122
    }
123
124
    /**
125
     * Loop over affiliations.
126
     *
127
     * @param  array $author
128
     * @return array
129
     */
130
    protected function loopOverAffiliations($author): array
131
    {
132
        $affiliations = [];
133
        foreach ($author->AffiliationInfo as $affiliation) {
134
            $affiliations[]['name'] = get_string($affiliation, 'Affiliation');
135
        }
136
137
        return $affiliations;
138
    }
139
140
    /**
141
     * Get emails from affiliations array.
142
     *
143
     * @param  array  $affiliations
144
     * @return string
145
     */
146
    protected function getEmailsFromAffiliations($affiliations): string
147
    {
148
        return get_string(find_emails_in_array(Arr::pluck($affiliations, 'name')), 0);
149
    }
150
}
151