Passed
Push — master ( 222752...4454ce )
by Marcel
02:38
created

ExternalFileService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
c 1
b 0
f 0
dl 0
loc 75
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A detectDelimiter() 0 14 3
A read() 0 38 3
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 $datasetMetadata
35
     * @return array
36
     */
37
    public function read($datasetMetadata)
38
    {
39
        //$this->logger->error('dataset path: ' . $datasetMetadata['link']);
40
        $string = $datasetMetadata['link'];
41
42
        $ch = curl_init();
43
        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

43
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_SSL_VERIFYPEER, FALSE);
Loading history...
44
        curl_setopt($ch, CURLOPT_HEADER, false);
45
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
46
        curl_setopt($ch, CURLOPT_URL, $string);
47
        curl_setopt($ch, CURLOPT_REFERER, $string);
48
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
49
        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');
50
        $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

50
        $data = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
51
        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

51
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
52
53
        $delimiter = $this->detectDelimiter($data);
54
        $rows = str_getcsv($data, "\n");
55
        $data = array();
56
        $header = array();
57
        $headerrow = 0;
58
59
        foreach ($rows as &$row) {
60
            $row = str_getcsv($row, $delimiter);
61
            if ($headerrow === 0) {
62
                $header['dimension1'] = $row[0];
63
                $header['dimension2'] = $row[1];
64
                $header['dimension3'] = $row[2];
65
                $headerrow = 1;
66
            } else {
67
                array_push($data, ['dimension1' => $row[0], 'dimension2' => $row[1], 'dimension3' => $row[2]]);
68
            }
69
        }
70
        $result = [
71
            'header' => $header,
72
            'data' => $data
73
        ];
74
        return $result;
75
    }
76
77
    private function detectDelimiter($data)
78
    {
79
        $delimiters = ["\t", ";", "|", ","];
80
        $data_2 = array();
81
        $delimiter = $delimiters[0];
82
        foreach ($delimiters as $d) {
83
            $firstRow = str_getcsv($data, "\n")[0];
84
            $data_1 = str_getcsv($firstRow, $d);
85
            if (sizeof($data_1) > sizeof($data_2)) {
86
                $delimiter = $d;
87
                $data_2 = $data_1;
88
            }
89
        }
90
        return $delimiter;
91
    }
92
}