Passed
Push — master ( 9cfbc1...2e79f5 )
by Marcel
02:35
created

ExternalFile::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * Analytics
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the LICENSE.md file.
7
 *
8
 * @author Marcel Scherello <[email protected]>
9
 * @copyright 2020 Marcel Scherello
10
 */
11
12
namespace OCA\Analytics\Datasource;
13
14
use OCP\IL10N;
15
use OCP\ILogger;
16
17
class ExternalFile implements IDatasource
18
{
19
    private $logger;
20
    private $userId;
21
    private $l10n;
22
23
    public function __construct(
24
        $userId,
25
        IL10N $l10n,
26
        ILogger $logger
27
    )
28
    {
29
        $this->userId = $userId;
30
        $this->l10n = $l10n;
31
        $this->logger = $logger;
32
    }
33
34
    /**
35
     * @return string Display Name of the datasource
36
     */
37
    public function getName(): string
38
    {
39
        return $this->l10n->t('External url (csv)');
40
    }
41
42
    /**
43
     * @return int digit unique datasource id
44
     */
45
    public function getId(): int
46
    {
47
        return 4;
48
    }
49
50
    /**
51
     * @return array available options of the datasoure
52
     */
53
    public function getTemplate(): array
54
    {
55
        $template = array();
56
        array_push($template, ['id' => 'link', 'name' => 'External URL', 'placeholder' => 'url']);
57
        array_push($template, ['id' => 'columns', 'name' => $this->l10n->t('Select columns'), 'placeholder' => $this->l10n->t('e.g. 1,2,4 or leave empty')]);
58
        array_push($template, ['id' => 'offset', 'name' => $this->l10n->t('Ignore first rows'), 'placeholder' => $this->l10n->t('Number of rows')]);
59
        return $template;
60
    }
61
62
    /**
63
     * Read the Data
64
     * @param $option
65
     * @return array available options of the datasoure
66
     */
67
    public function readData($option): array
68
    {
69
        //$this->logger->error('dataset path: ' . $datasetMetadata['link']);
70
71
        $data = array();
72
        $header = array();
73
        $headerrow = 0;
74
        $selectedColumns = array();
75
76
        $ch = curl_init();
77
        if ($ch !== false) {
78
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
79
            curl_setopt($ch, CURLOPT_HEADER, false);
80
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
81
            curl_setopt($ch, CURLOPT_URL, $option['link']);
82
            curl_setopt($ch, CURLOPT_REFERER, $option['link']);
83
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
84
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
85
            $curlResult = curl_exec($ch);
86
            curl_close($ch);
87
        } else {
88
            $curlResult = '';
89
        }
90
91
        $rows = str_getcsv($curlResult, "\n");
0 ignored issues
show
Bug introduced by
It seems like $curlResult can also be of type true; however, parameter $string of str_getcsv() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
        $rows = str_getcsv(/** @scrutinizer ignore-type */ $curlResult, "\n");
Loading history...
92
93
        // remove x number of rows from the beginning
94
        if (isset($option['offset']) and is_numeric($option['offset'])) {
95
            $rows = array_slice($rows, $option['offset']);
96
        }
97
98
        // ensure that all values are integers
99
        if (isset($option['columns'])) {
100
            $new = array();
101
            $selectedColumns = str_getcsv($option['columns'], ',');
102
            foreach ($selectedColumns as $value) {
103
                if (is_numeric($value)) {
104
                    array_push($new, $value);
105
                }
106
            }
107
            $selectedColumns = $new;
108
        }
109
110
        $delimiter = $this->detectDelimiter($rows[0]);
111
112
        foreach ($rows as &$row) {
113
            $row = str_getcsv($row, $delimiter);
114
            $rowMinimized = array();
115
116
            if (count($selectedColumns) !== 0) {
117
                foreach ($selectedColumns as $selectedColumn) {
118
                    array_push($rowMinimized, $row[$selectedColumn - 1]);
119
                }
120
            } else {
121
                $rowMinimized = $row;
122
            }
123
124
            if ($headerrow === 0) {
125
                $header = $rowMinimized;
126
                $headerrow = 1;
127
            } else {
128
                array_push($data, $rowMinimized);
129
            }
130
        }
131
        return [
132
            'header' => $header,
133
            'data' => $data
134
        ];
135
    }
136
137
    private function detectDelimiter($data)
138
    {
139
        $delimiters = ["\t", ";", "|", ","];
140
        $data_2 = array();
141
        $delimiter = $delimiters[0];
142
        foreach ($delimiters as $d) {
143
            //$firstRow = str_getcsv($data, "\n")[0];
144
            $data_1 = str_getcsv($data, $d);
145
            if (sizeof($data_1) > sizeof($data_2)) {
146
                $delimiter = $d;
147
                $data_2 = $data_1;
148
            }
149
        }
150
        return $delimiter;
151
    }
152
}