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

EutilsEsearch::extract()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace PubPeerFoundation\PublicationDataExtractor\Resources\Extractors;
4
5
use PubPeerFoundation\PublicationDataExtractor\Exceptions\UnparseableApiException;
6
7
class EutilsEsearch implements Extractor, ProvidesIdentifiersData
8
{
9
    /**
10
     * @var
11
     */
12
    protected $document;
13
14
    /**
15
     * @var
16
     */
17
    protected $searchTree;
18
19
    /**
20
     * @var
21
     */
22
    protected $output;
23
24
    /**
25
     * EutilsEsearch constructor.
26
     *
27
     * @param $document
28
     */
29
    public function __construct($document)
30
    {
31
        $this->document = $document;
32
    }
33
34
    /**
35
     * @throws UnparseableApiException
36
     * @return array
37
     */
38
    public function extract(): array
39
    {
40
        $this->getDataFromDocument();
41
        $this->extractIdentifiersData();
42
43
        return $this->output;
44
    }
45
46
    /**
47
     * Extract search tree from document.
48
     *
49
     * @throws UnparseableApiException
50
     */
51
    protected function getDataFromDocument()
52
    {
53
        if ($this->document['esearchresult']['count'] !== '1') {
54
            throw new UnparseableApiException();
55
        }
56
57
        $this->searchTree = $this->document['esearchresult'];
58
    }
59
60
    /**
61
     * Extract and format data needed for the Identifiers Relationship
62
     * on the Publication Model.
63
     */
64
    public function extractIdentifiersData()
65
    {
66
        foreach ($this->searchTree['idlist'] as $identifier) {
67
            $this->output['identifiers'][] = [
68
                'value' => stringify($identifier),
69
                'type' => 'pubmed',
70
            ];
71
        }
72
    }
73
}
74