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

Extractor::addOutputSource()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
3
namespace PubPeerFoundation\PublicationDataExtractor\Resources\Extractors;
4
5
use PubPeerFoundation\PublicationDataExtractor\ApiDataChecker;
6
use PubPeerFoundation\PublicationDataExtractor\Exceptions\JournalTitleNotFoundException;
7
8
abstract class Extractor
9
{
10
    /**
11
     * @var mixed
12
     */
13
    protected $document;
14
15
    /**
16
     * @var array
17
     */
18
    protected $searchTree;
19
20
    /**
21
     * @var array
22
     */
23
    protected $output = [];
24
25
    /**
26
     * Extractor constructor.
27
     *
28
     * @param $document
29
     */
30
    public function __construct($document)
31
    {
32
        $this->document = $document;
33
    }
34
35
    /**
36
     * Dynamically choose what methods to call on each Resource.
37
     *
38
     * @return array
39
     */
40
    public function extract(): array
41
    {
42
        $this->getDataFromDocument();
43
44
        foreach ($this->dataTypes() as $type) {
45
            $class = __NAMESPACE__.'\\Provides'.ucfirst($type).'Data';
46
47
            if ($this instanceof $class) {
48
                $method = 'extract'.ucfirst($type).'Data';
49
                try {
50
                    $this->$method();
51
                } catch (JournalTitleNotFoundException $e) {
52
                    $this->output['journal']['title'] = $this->output['journal']['publisher'];
53
                }
54
            }
55
        }
56
57
        $this->addOutputSource();
58
59
        return $this->output;
60
    }
61
62
    /**
63
     * Get a list of data Types from Schema.
64
     *
65
     * @return array
66
     */
67
    protected function dataTypes(): array
68
    {
69
        return array_slice(array_keys(ApiDataChecker::SCHEMA['root']), 1);
70
    }
71
72
    /**
73
     * Prepare each data document.
74
     *
75
     * @return mixed
76
     */
77
    abstract protected function getDataFromDocument();
78
79
    /**
80
     *  Set the source of the data on the data.
81
     */
82
    protected function addOutputSource(): void
83
    {
84
        if (! empty($this->output)) {
85
            $this->output['_source'] = get_class_name($this);
86
        }
87
    }
88
}
89