Passed
Push — master ( 9c2d3e...b67124 )
by Marcel
02:34
created

ExternalFile::readData()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 37
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 28
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 37
rs 9.472
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' => 'delete', 'name' => 'Delete all data before load', 'placeholder' => 'true/false']);
58
        return $template;
59
    }
60
61
    /**
62
     * Read the Data
63
     * @param $option
64
     * @return array available options of the datasoure
65
     */
66
    public function readData($option): array
67
    {
68
        //$this->logger->error('dataset path: ' . $datasetMetadata['link']);
69
70
        $ch = curl_init();
71
        if ($ch !== false) {
72
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
73
            curl_setopt($ch, CURLOPT_HEADER, false);
74
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
75
            curl_setopt($ch, CURLOPT_URL, $option['link']);
76
            curl_setopt($ch, CURLOPT_REFERER, $option['link']);
77
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
78
            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');
79
            $curlResult = curl_exec($ch);
80
            curl_close($ch);
81
        } else {
82
            $curlResult = '';
83
        }
84
85
        $delimiter = $this->detectDelimiter($curlResult);
86
        $rows = str_getcsv($curlResult, "\n");
87
        $data = array();
88
        $header = array();
89
        $headerrow = 0;
90
91
        foreach ($rows as &$row) {
92
            $row = str_getcsv($row, $delimiter);
93
            if ($headerrow === 0) {
94
                $header = $row;
95
                $headerrow = 1;
96
            } else {
97
                array_push($data, $row);
98
            }
99
        }
100
        return [
101
            'header' => $header,
102
            'data' => $data
103
        ];
104
    }
105
106
    private function detectDelimiter($data)
107
    {
108
        $delimiters = ["\t", ";", "|", ","];
109
        $data_2 = array();
110
        $delimiter = $delimiters[0];
111
        foreach ($delimiters as $d) {
112
            $firstRow = str_getcsv($data, "\n")[0];
113
            $data_1 = str_getcsv($firstRow, $d);
114
            if (sizeof($data_1) > sizeof($data_2)) {
115
                $delimiter = $d;
116
                $data_2 = $data_1;
117
            }
118
        }
119
        return $delimiter;
120
    }
121
}