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

Github::readData()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 47
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 35
c 0
b 0
f 0
nc 8
nop 1
dl 0
loc 47
rs 8.0555
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 Github implements IDatasource
18
{
19
    private $logger;
20
    private $l10n;
21
22
    public function __construct(
23
        IL10N $l10n,
24
        ILogger $logger
25
    )
26
    {
27
        $this->l10n = $l10n;
28
        $this->logger = $logger;
29
    }
30
31
    /**
32
     * @return string Display Name of the datasource
33
     */
34
    public function getName(): string
35
    {
36
        return $this->l10n->t('GitHub');
37
    }
38
39
    /**
40
     * @return int digit unique datasource id
41
     */
42
    public function getId(): int
43
    {
44
        return 3;
45
    }
46
47
    /**
48
     * @return array available options of the datasoure
49
     */
50
    public function getTemplate(): array
51
    {
52
        $template = array();
53
        array_push($template, ['id' => 'user', 'name' => 'GitHub Username', 'placeholder' => 'GitHub user']);
54
        array_push($template, ['id' => 'repository', 'name' => 'Repository', 'placeholder' => 'GitHub repository']);
55
        array_push($template, ['id' => 'limit', 'name' => 'Limit', 'placeholder' => 'Number of records']);
56
        array_push($template, ['id' => 'timestamp', 'name' => 'Timestamp of dataload', 'placeholder' => 'true/false']);
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
        if (isset($option['link'])) $string = 'https://api.github.com/repos/' . $option['link'] . '/releases';
69
        else $string = 'https://api.github.com/repos/' . $option['user'] . '/' . $option['repository'] . '/releases';
70
71
        $ch = curl_init();
72
        if ($ch !== false) {
73
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
74
            curl_setopt($ch, CURLOPT_HEADER, false);
75
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
76
            curl_setopt($ch, CURLOPT_URL, $string);
77
            curl_setopt($ch, CURLOPT_REFERER, $string);
78
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
79
            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');
80
            $curlResult = curl_exec($ch);
81
            curl_close($ch);
82
        } else {
83
            $curlResult = '';
84
        }
85
86
        $json = json_decode($curlResult, true);
87
        $i = 0;
88
        $data = array();
89
        foreach ($json as $item) {
90
            if (isset($option['limit']) and $option['limit'] !== '') {
91
                if ($i === (int)$option['limit']) break;
92
            }
93
            $nc_value = 0;
94
            foreach ($item['assets'] as $asset) {
95
                if (substr($asset['name'], -2) === 'gz') $nc_value = $asset['download_count'];
96
            }
97
            array_push($data, [$item['tag_name'], $this->floatvalue($nc_value)]);
98
            $i++;
99
        }
100
101
        $header = array();
102
        $header[0] = 'Version';
103
        $header[1] = 'Count';
104
105
        usort($data, function ($a, $b) {
106
            return strnatcmp($a[0], $b[0]);
107
        });
108
109
        return [
110
            'header' => $header,
111
            'data' => $data,
112
            'error' => 0,
113
        ];
114
    }
115
116
    private function floatvalue($val)
117
    {
118
        $val = str_replace(",", ".", $val);
119
        $val = preg_replace('/\.(?=.*\.)/', '', $val);
120
        $val = preg_replace('/[^0-9-.]+/', '', $val);
121
        if (is_numeric($val)) {
122
            return floatval($val);
123
        } else {
124
            return false;
125
        }
126
    }
127
}