Passed
Push — master ( 8360a5...feb138 )
by Xavier
01:22
created

Crossref::extractUpdatesData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
ccs 6
cts 6
cp 1
cc 2
eloc 6
nc 2
nop 0
crap 2
1
<?php
2
3
namespace PubPeerFoundation\PublicationDataExtractor\Resources\Extractors;
4
5
use PubPeerFoundation\PublicationDataExtractor\Exceptions\UnparseableApiException;
6
use PubPeerFoundation\PublicationDataExtractor\Exceptions\JournalTitleNotFoundException;
7
8
class Crossref extends Extractor implements ProvidesPublicationData, ProvidesIdentifiersData, ProvidesJournalData, ProvidesAuthorsData, ProvidesTagsData
9
{
10
    /**
11
     * @throws UnparseableApiException
12
     */
13
    protected function fillSearchTree(): void
14
    {
15
        if ('ok' !== $this->document['status']) {
16
            throw new UnparseableApiException();
17 24
        }
18
19 24
        $this->searchTree = $this->document['message'];
20 24
    }
21
22 11
    /**
23
     * Extract and format data needed for the Publication Model.
24 11
     */
25
    public function extractPublicationData(): void
26 9
    {
27 9
        $date = $this->extractDateFrom(['published-print', 'published-online', 'issued']);
28 9
29 9
        $this->resourceOutput['publication'] = [
30 9
            'title' => get_string($this->searchTree, 'title'),
31 9
            'abstract' => get_string($this->searchTree, 'abstract'),
32 9
            'url' => $this->searchTree['URL'] ?? null,
33
            'published_at' => $date,
34 9
        ];
35
    }
36
37 7
    /**
38
     * Extract and format data needed for the Identifiers Relationship
39 7
     * on the Publication Model.
40 2
     */
41
    public function extractIdentifiersData(): void
42
    {
43 5
        if (! empty($this->searchTree['DOI'])) {
44 5
            $this->resourceOutput['identifiers'][] = [
45
                'value' => $this->searchTree['DOI'],
46
                'type' => 'doi',
47
            ];
48
        }
49 11
50
        foreach (get_array($this->searchTree, 'ISSN') as $issn) {
51 11
            $this->resourceOutput['identifiers'][] = [
52
                'value' => $issn,
53 11
                'type' => 'issn',
54 11
            ];
55 11
        }
56 11
    }
57 11
58
    /**
59 11
     * Extract and format data needed for the Journal Relationship
60
     * on the Publication Model.
61
     *
62
     * @throws JournalTitleNotFoundException
63
     */
64 11
    public function extractJournalData(): void
65
    {
66 11
        $this->resourceOutput['journal'] = [
67
            'title' => get_string($this->searchTree, 'container-title'),
68
            'issn' => get_array($this->searchTree, 'ISSN'),
69
            'publisher' => get_string($this->searchTree, 'publisher'),
70
        ];
71
72
        if (empty($this->searchTree['container-title']) && empty($this->searchTree['ISSN'])) {
73 17
            throw new JournalTitleNotFoundException();
74
        }
75 17
    }
76 9
77 9
    /**
78 9
     * Extract and format data needed for the Authors Relationship
79
     * on the Publication Model.
80
     */
81
    public function extractAuthorsData(): void
82 17
    {
83 12
        foreach (get_array($this->searchTree, 'author') as $author) {
84 11
            if (isset($author['family'])) {
85 11
                $this->resourceOutput['authors'][] = [
86 11
                    'first_name' => get_string($author, 'given'),
87 11
                    'last_name' => get_string($author, 'family'),
88
                    'orcid' => get_string($author, 'ORCID'),
89
                    'affiliation' => get_array($author, 'affiliation'),
90
                ];
91 1
            }
92 1
        }
93 1
    }
94
95
    /**
96
     * Extract and format data needed for the Types Relationship
97 17
     * on the Publication Model.
98
     */
99
    public function extractTypesData()
100
    {
101
        $this->resourceOutput['types'][] = [
102
            'name' => get_string($this->searchTree, 'type'),
103 12
        ];
104
    }
105 12
106 1
    /**
107
     * Extract and format data needed for the tags Relationship
108
     * on the Publication Model.
109 12
     */
110 12
    public function extractTagsData(): void
111 12
    {
112 12
        foreach (get_array($this->searchTree, 'subject') as $tag) {
113
            $this->resourceOutput['tags'][] = [
114 12
                'name' => $tag,
115
            ];
116
        }
117
    }
118
119
    /**
120 9
     * @param $array
121
     * @return mixed
122 9
     */
123 9
    protected function extractDateFrom($array)
124 9
    {
125 9
        $datePartsContainer = array_values(array_filter($array, function ($string) {
126 9
            return isset($this->searchTree[$string]);
127 9
        }))[0];
128 9
129 9
        return date_from_parts($this->searchTree[$datePartsContainer]['date-parts'][0]);
130
    }
131
}
132