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

CrossrefUpdates::getApiUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace PubPeerFoundation\PublicationDataExtractor\Resources;
4
5
use PubPeerFoundation\PublicationDataExtractor\Exceptions\UnparseableApiException;
6
7
class CrossrefUpdates extends Resource
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $url = 'https://api.crossref.org/works?filter=updates:';
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
     * @var string
25
     */
26
    protected $extractor = Extractors\CrossrefUpdates::class;
27
28
    /**
29
     * @return string
30
     */
31
    public function getApiUrl(): string
32
    {
33
        return $this->url.$this->identifier->getQueryString();
34
    }
35
36
    /**
37
     * @return array
38
     */
39
    public function getRequestOptions(): array
40
    {
41
        return $this->queryStringParameters;
42
    }
43
44
    /**
45
     * @param string $document
46
     *
47
     * @return array
48
     */
49
    public function getDataFrom(string $document): array
50
    {
51
        $baseTree = json_decode($document, true);
52
53
        if (is_null($baseTree)) {
54
            return [];
55
        }
56
57
        try {
58
            $extractor = new $this->extractor($baseTree, $this->output);
59
60
            return $extractor->extract();
61
        } catch (UnparseableApiException $e) {
62
            return [];
63
        }
64
    }
65
}
66