Passed
Push — master ( f320c5...dad242 )
by Xavier
04:41
created

EutilsEsearch::getDataFrom()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 4
nop 1
1
<?php
2
3
namespace PubPeerFoundation\PublicationDataExtractor\Resources;
4
5
use PubPeerFoundation\PublicationDataExtractor\Identifiers\Identifier;
6
7
class EutilsEsearch implements Resource
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $url = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi';
13
14
    /**
15
     * @var array
16
     */
17
    protected $queryStringParameters = [
18
        'query' => [
19
            'db' => 'pubmed',
20
            'version' => '2.0',
21
            'retmode' => 'json',
22
            'term' => '',
23
            'field' => 'AID',
24
        ],
25
    ];
26
27
    /**
28
     * EutilsEsearch constructor.
29
     *
30
     * @param Identifier $identifier
31
     */
32
    public function __construct(Identifier $identifier)
33
    {
34
        $this->queryStringParameters['query']['term'] = $identifier->getQueryString();
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    public function getApiUrl(): string
41
    {
42
        return $this->url;
43
    }
44
45
    /**
46
     * @return array
47
     */
48
    public function getRequestOptions(): array
49
    {
50
        return $this->queryStringParameters;
51
    }
52
53
    /**
54
     * @param string $document
55
     *
56
     * @return array
57
     */
58
    public function getDataFrom(string $document): array
59
    {
60
        try {
61
            $baseTree = json_decode($document, true);
62
            $extractor = new Extractors\EutilsEsearch($baseTree);
63
64
            return $extractor->extract();
65
        } catch (\Exception $e) {
66
            return [];
67
        }
68
    }
69
}
70