Crossref::extractPublicationData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
c 0
b 0
f 0
rs 10
ccs 7
cts 7
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace PubPeerFoundation\PublicationDataExtractor\Resources\Extractors;
4
5
use PubPeerFoundation\PublicationDataExtractor\Exceptions\JournalTitleNotFoundException;
6
use PubPeerFoundation\PublicationDataExtractor\Exceptions\UnparseableApiException;
7
8
class Crossref extends Extractor implements ProvidesPublicationData, ProvidesIdentifiersData, ProvidesJournalData, ProvidesAuthorsData, ProvidesTagsData, ProvidesLinksData
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
                    'sequence' => get_string($author, 'sequence'),
90
                    'affiliation' => get_array($author, 'affiliation'),
91 1
                ];
92 1
            }
93 1
        }
94
    }
95
96
    /**
97 17
     * Extract and format data needed for the Types Relationship
98
     * on the Publication Model.
99
     */
100
    public function extractTypesData()
101
    {
102
        $this->resourceOutput['types'][] = [
103 12
            'name' => get_string($this->searchTree, 'type'),
104
        ];
105 12
    }
106 1
107
    /**
108
     * Extract and format data needed for the tags Relationship
109 12
     * on the Publication Model.
110 12
     */
111 12
    public function extractTagsData(): void
112 12
    {
113
        foreach (get_array($this->searchTree, 'subject') as $tag) {
114 12
            $this->resourceOutput['tags'][] = [
115
                'name' => $tag,
116
            ];
117
        }
118
    }
119
120 9
    /**
121
     * Extract and format data needed for the links Relationship
122 9
     * on the Publication Model.
123 9
     */
124 9
    public function extractLinksData(): void
125 9
    {
126 9
        if (array_key_exists('relation', $this->searchTree)) {
127 9
            foreach (get_array($this->searchTree['relation'], 'has-preprint') as $preprint) {
128 9
                $this->resourceOutput['links'][] = [
129 9
                    'has_preprint' => $preprint['id'],
130
                ];
131
            }
132
            foreach (get_array($this->searchTree['relation'], 'is-preprint-of') as $preprint) {
133
                $this->resourceOutput['links'][] = [
134 9
                    'is_preprint_of' => $preprint['id'],
135
                ];
136
            }
137
        }
138
    }
139
140 9
    /**
141
     * @param  $array
142 9
     * @return mixed
143 9
     */
144
    protected function extractDateFrom($array)
145 9
    {
146
        $datePartsContainer = array_values(array_filter($array, function ($string) {
147
            return isset($this->searchTree[$string]);
148
        }))[0];
149
150
        return date_from_parts($this->searchTree[$datePartsContainer]['date-parts'][0]);
151 9
    }
152
}
153