Extractor::extract()   A
last analyzed

Complexity

Conditions 3
Paths 10

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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