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

Extractor::extract()   A

Complexity

Conditions 3
Paths 10

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 10
nop 0
1
<?php
2
3
namespace PubPeerFoundation\PublicationDataExtractor\Resources\Extractors;
4
5
use PubPeerFoundation\PublicationDataExtractor\Output;
6
use PubPeerFoundation\PublicationDataExtractor\Schema;
7
use PubPeerFoundation\PublicationDataExtractor\Exceptions\UnparseableApiException;
8
use PubPeerFoundation\PublicationDataExtractor\Exceptions\JournalTitleNotFoundException;
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
     * @param Output $output
41
     */
42
    public function __construct($document, Output $output)
43
    {
44
        $this->document = $document;
45
        $this->output = $output;
46
    }
47
48
    /**
49
     * Dynamically choose what methods to call on each Resource.
50
     *
51
     * @return array
52
     */
53
    public function extract(): array
54
    {
55
        try {
56
            $this->fillSearchTree();
57
58
            foreach (Schema::getDataTypes() as $type) {
59
                $this->extractData($type);
60
61
                $this->buildOutput($type);
62
            }
63
64
            $this->addOutputSource();
65
66
            return $this->resourceOutput;
67
        } catch (UnparseableApiException $e) {
68
            return [];
69
        }
70
    }
71
72
    /**
73
     * Prepare each data document.
74
     *
75
     * @throws UnparseableApiException
76
     */
77
    abstract protected function fillSearchTree(): void;
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->resourceOutput['_source'] = get_class_name($this);
86
        }
87
    }
88
89
    /**
90
     * Extract data from each type of the Resource.
91
     *
92
     * @param $type
93
     */
94
    protected function extractData($type): void
95
    {
96
        $class = __NAMESPACE__.'\\Provides'.ucfirst($type).'Data';
97
98
        if ($this instanceof $class) {
99
            $method = 'extract'.ucfirst($type).'Data';
100
            try {
101
                $this->$method();
102
            } catch (JournalTitleNotFoundException $e) {
103
                $this->resourceOutput['journal']['title'] = $this->resourceOutput['journal']['publisher'];
104
            }
105
        }
106
    }
107
108
    /**
109
     * Build data output for each Resource type.
110
     *
111
     * @param $type
112
     */
113
    protected function buildOutput($type): void
114
    {
115
        if (isset($this->resourceOutput[$type])) {
116
            $method = 'add'.ucfirst($type);
117
            $this->output->$method($this->resourceOutput[$type]);
118
        }
119
    }
120
}
121