Passed
Push — master ( c3bafb...29fa5f )
by Xavier
02:01
created

Extractor::extract()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 47
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 8.5125
c 0
b 0
f 0
cc 6
eloc 15
nc 12
nop 0
1
<?php
2
3
namespace PubPeerFoundation\PublicationDataExtractor\Resources\Extractors;
4
5
use PubPeerFoundation\PublicationDataExtractor\Models\Output;
6
use PubPeerFoundation\PublicationDataExtractor\ApiDataChecker;
7
use PubPeerFoundation\PublicationDataExtractor\Exceptions\JournalTitleNotFoundException;
8
9
abstract class Extractor
10
{
11
    /**
12
     * @var mixed
13
     */
14
    protected $document;
15
16
    /**
17
     * @var array
18
     */
19
    protected $searchTree;
20
21
    /**
22
     * @var array
23
     */
24
    protected $output = [];
25
26
    /**
27
     * Extractor constructor.
28
     *
29
     * @param $document
30
     */
31
    public function __construct($document)
32
    {
33
        $this->document = $document;
34
    }
35
36
    /**
37
     * Dynamically choose what methods to call on each Resource.
38
     *
39
     * @return array
40
     */
41
    public function extract(): array
42
    {
43
        $this->getDataFromDocument();
44
45
        foreach (ApiDataChecker::getDataTypes() as $type) {
46
            $class = __NAMESPACE__.'\\Provides'.ucfirst($type).'Data';
47
48
            if ($this instanceof $class) {
49
                $method = 'extract'.ucfirst($type).'Data';
50
                try {
51
                    $this->$method();
52
                } catch (JournalTitleNotFoundException $e) {
53
                    $this->output['journal']['title'] = $this->output['journal']['publisher'];
54
                }
55
            }
56
        }
57
//        if (isset($this->output['identifiers'])) {
58
//            Output::getInstance()->addIdentifiers($this->output['identifiers']);
59
//        }
60
//        if (isset($this->output['publication'])) {
61
//            Output::getInstance()->addPublication($this->output['publication']);
62
//        }
63
//        if (isset($this->output['journal'])) {
64
//            Output::getInstance()->addJournal($this->output['journal']);
65
//        }
66
//        if (isset($this->output['affiliations'])) {
67
//            Output::getInstance()->addAffiliations($this->output['affiliations']);
68
//        }
69
//        if (isset($this->output['types'])) {
70
//            Output::getInstance()->addTypes($this->output['types']);
71
//        }
72
//        if (isset($this->output['tags'])) {
73
//            Output::getInstance()->addTags($this->output['tags']);
74
//        }
75
//        if (isset($this->output['authors'])) {
76
//            Output::getInstance()->addAuthors($this->output['authors']);
77
//        }
78
        foreach (ApiDataChecker::getDataTypes() as $type) {
79
            if (isset($this->output[$type])) {
80
                $method = 'add'.ucfirst($type);
81
                Output::getInstance()->$method($this->output[$type]);
82
            }
83
        }
84
85
        $this->addOutputSource();
86
87
        return $this->output;
88
    }
89
90
    /**
91
     * Prepare each data document.
92
     *
93
     * @return mixed
94
     */
95
    abstract protected function getDataFromDocument();
96
97
    /**
98
     *  Set the source of the data on the data.
99
     */
100
    protected function addOutputSource(): void
101
    {
102
        if (! empty($this->output)) {
103
            $this->output['_source'] = get_class_name($this);
104
        }
105
    }
106
}
107