Passed
Push — master ( 2dbb0d...2ad290 )
by Xavier
01:17
created

IdConverter::getDataFromDocument()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
nc 3
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 fillSearchTree(): void
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(): void
41
    {
42
        foreach ($this->searchTree as $type => $identifier) {
43
            if (array_key_exists($type, $this->typeMap)) {
44
                $this->resourceOutput['identifiers'][] = [
45
                    'value' => $identifier,
46
                    'type' => $this->typeMap[$type],
47
                ];
48
            }
49
        }
50
    }
51
}
52