Passed
Push — master ( 6ba3d6...287c0c )
by Xavier
05:28
created

EutilsEfetch::extractAuthorsData()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
ccs 8
cts 8
cp 1
cc 5
eloc 12
nc 9
nop 0
crap 5
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
            foreach ($this->searchTree->MedlineCitation->Article->AuthorList->Author as $author) {
70
                if (! empty($lastName = get_string($author, 'LastName'))) {
71 4
                    $affiliations = [];
72
                    foreach ($author->AffiliationInfo as $affiliation) {
73
                        $affiliations[]['name'] = get_string($affiliation, 'Affiliation');
74
                    }
75
76
                    $this->output['authors'][] = [
77 4
                        'first_name' => get_string($author, 'ForeName'),
78
                        'last_name' => $lastName,
79 4
                        'email' => get_string(find_emails_in_array(Arr::pluck($affiliations, 'name')), 0),
80
                        'affiliation' => $affiliations,
81 4
                    ];
82 4
                }
83
            }
84
        } catch (\Exception $e) {
85 4
            // Empty catch block, don't want anything to happen in case of exception.
86 4
        }
87
    }
88
89 4
    /**
90 4
     * Get all available ISSNs values from the tree.
91 4
     *
92
     * @return array
93 4
     */
94
    protected function getIssns()
95
    {
96
        $issn = [];
97
98
        if ($number = get_string($this->searchTree, 'MedlineCitation.Article.Journal.ISSN')) {
99 8
            $issn[] = $number;
100
        }
101
102 8
        if ($number = get_string($this->searchTree, 'MedlineCitation.MedlineJournalInfo.ISSNLinking')) {
103 7
            $issn[] = $number;
104 7
        }
105 7
106 7
        return $issn;
107
    }
108
}
109