Passed
Push — master ( ffe1e4...544106 )
by Marcel
05:27
created

ExternalFileService::getTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Data 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 2019 Marcel Scherello
10
 */
11
12
namespace OCA\Analytics\Datasource;
13
14
use OCP\ILogger;
15
16
class ExternalFileService
17
{
18
    private $logger;
19
    private $userId;
20
21
    public function __construct(
22
        $userId,
23
        ILogger $logger
24
    )
25
    {
26
        $this->userId = $userId;
27
        $this->logger = $logger;
28
    }
29
30
    /**
31
     * Get the content from an external url
32
     *
33
     * @NoAdminRequired
34
     * @param array $option
35
     * @return array
36
     */
37
    public function read($option)
38
    {
39
        //$this->logger->error('dataset path: ' . $datasetMetadata['link']);
40
41
        $ch = curl_init();
42
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, 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

42
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_SSL_VERIFYPEER, FALSE);
Loading history...
43
        curl_setopt($ch, CURLOPT_HEADER, false);
44
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
45
        curl_setopt($ch, CURLOPT_URL, $option['link']);
46
        curl_setopt($ch, CURLOPT_REFERER, $option['link']);
47
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
48
        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');
49
        $data = curl_exec($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, 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

49
        $data = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
50
        curl_close($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, 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

50
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
51
52
        $delimiter = $this->detectDelimiter($data);
53
        $rows = str_getcsv($data, "\n");
54
        $data = array();
55
        $header = array();
56
        $headerrow = 0;
57
58
        foreach ($rows as &$row) {
59
            $row = str_getcsv($row, $delimiter);
60
            if ($headerrow === 0) {
61
                $header['dimension1'] = $row[0];
62
                $header['dimension2'] = $row[1];
63
                $header['dimension3'] = $row[2];
64
                $headerrow = 1;
65
            } else {
66
                array_push($data, ['dimension1' => $row[0], 'dimension2' => $row[1], 'dimension3' => $row[2]]);
67
            }
68
        }
69
        $result = [
70
            'header' => $header,
71
            'data' => $data
72
        ];
73
        return $result;
74
    }
75
76
    /**
77
     * template for options & settings
78
     *
79
     * @NoAdminRequired
80
     * @return array
81
     */
82
    public function getTemplate()
83
    {
84
        $template = array();
85
        array_push($template, ['id' => 'link', 'name' => 'External URL', 'placeholder' => 'url']);
86
        return $template;
87
    }
88
89
    private function detectDelimiter($data)
90
    {
91
        $delimiters = ["\t", ";", "|", ","];
92
        $data_2 = array();
93
        $delimiter = $delimiters[0];
94
        foreach ($delimiters as $d) {
95
            $firstRow = str_getcsv($data, "\n")[0];
96
            $data_1 = str_getcsv($firstRow, $d);
97
            if (sizeof($data_1) > sizeof($data_2)) {
98
                $delimiter = $d;
99
                $data_2 = $data_1;
100
            }
101
        }
102
        return $delimiter;
103
    }
104
}