Crossref::getDataFrom()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 14
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 3
1
<?php
2
3
namespace PubPeerFoundation\PublicationDataExtractor\Resources;
4
5
use PubPeerFoundation\PublicationDataExtractor\Exceptions\UnparseableApiException;
6
7
class Crossref extends Resource
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $url = 'https://api.crossref.org/works/';
13
14
    /**
15
     * @var array
16
     */
17
    protected $queryStringParameters = [
18
        'headers' => [
19
            'User-Agent' => 'PubPeer/2.0 (https://pubpeer.com; mailto:[email protected])',
20
        ],
21
    ];
22
23
    /**
24 3
     * @var string
25
     */
26 3
    protected $extractor = Extractors\Crossref::class;
27 3
28
    /**
29
     * @return string
30
     */
31
    public function getApiUrl(): string
32 1
    {
33
        return $this->url.$this->identifier->getQueryString();
34 1
    }
35
36
    /**
37
     * @return array
38
     */
39
    public function getRequestOptions(): array
40 1
    {
41
        return $this->queryStringParameters;
42 1
    }
43
44
    /**
45
     * @param  string  $document
46
     * @return array
47
     */
48
    public function getDataFrom(string $document): array
49
    {
50 3
        $baseTree = json_decode($document, true);
51
52 3
        if (is_null($baseTree)) {
53
            return [];
54 3
        }
55 1
56
        try {
57
            $extractor = new $this->extractor($baseTree, $this->output);
58
59 2
            return $extractor->extract();
60
        } catch (UnparseableApiException $e) {
61 2
            return [];
62 1
        }
63 1
    }
64
}
65