Passed
Push — master ( 6ba3d6...287c0c )
by Xavier
05:28
created

IdConverter::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 IdConverter extends Extractor implements ProvidesIdentifiersData
8
{
9
    /**
10
     * @var array
11
     */
12
    protected $typeMap = [
13
        'pmcid' => 'pmc',
14
        'pmid' => 'pubmed',
15
        'doi' => 'doi',
16
    ];
17
18
    /**
19
     * Extract search tree from document.
20
     *
21
     * @throws UnparseableApiException
22
     */
23
    protected function getDataFromDocument()
24
    {
25
        if ('ok' !== $this->document['status']) {
26
            throw new UnparseableApiException();
27
        }
28
29
        if (isset($this->document['records'][0]['status']) && 'error' === $this->document['records'][0]['status']) {
30
            throw new UnparseableApiException();
31
        }
32
33
        $this->searchTree = $this->document['records'][0];
34
    }
35
36
    /**
37
     * Extract and format data needed for the Identifiers Relationship
38
     * on the Publication Model.
39
     */
40
    public function extractIdentifiersData()
41
    {
42
        foreach ($this->searchTree as $type => $identifier) {
43
            if (array_key_exists($type, $this->typeMap)) {
44
                $this->output['identifiers'][] = [
45
                    'value' => $identifier,
46
                    'type' => $this->typeMap[$type],
47
                ];
48
            }
49
        }
50
    }
51
}
52