Passed
Push — master ( 14e493...9c2d3e )
by Marcel
02:30
created

GithubService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 7
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 GithubService
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
    public function getName(): string
32
    {
33
        return $this->l10n->t('GitHub');
34
    }
35
36
    /**
37
     * Get the items for the selected category
38
     *
39
     * @NoAdminRequired
40
     * @param array $option
41
     * @return array
42
     */
43
    public function read($option)
44
    {
45
        if (isset($option['link'])) $string = 'https://api.github.com/repos/' . $option['link'] . '/releases';
46
        else $string = 'https://api.github.com/repos/' . $option['user'] . '/' . $option['repository'] . '/releases';
47
48
        $ch = curl_init();
49
        if ($ch !== false) {
50
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
51
            curl_setopt($ch, CURLOPT_HEADER, false);
52
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
53
            curl_setopt($ch, CURLOPT_URL, $string);
54
            curl_setopt($ch, CURLOPT_REFERER, $string);
55
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
56
            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');
57
            $curlResult = curl_exec($ch);
58
            curl_close($ch);
59
        } else {
60
            $curlResult = '';
61
        }
62
63
        $json = json_decode($curlResult, true);
64
        $i = 0;
65
        $data = array();
66
        foreach ($json as $item) {
67
            if (isset($option['limit']) and $option['limit'] !== '') {
68
                if ($i === (int)$option['limit']) break;
69
            }
70
            $nc_value = 0;
71
            foreach ($item['assets'] as $asset) {
72
                if (substr($asset['name'], -2) === 'gz') $nc_value = $asset['download_count'];
73
            }
74
            array_push($data, [$item['tag_name'], $this->floatvalue($nc_value)]);
75
            $i++;
76
        }
77
78
        $header = array();
79
        $header[0] = 'Version';
80
        $header[1] = 'Count';
81
82
        usort($data, function ($a, $b) {
83
            return strnatcmp($a[0], $b[0]);
84
        });
85
86
        return [
87
            'header' => $header,
88
            'data' => $data,
89
            'error' => 0,
90
        ];
91
    }
92
93
    /**
94
     * template for options & settings
95
     *
96
     * @NoAdminRequired
97
     * @return array
98
     */
99
    public function getTemplate()
100
    {
101
        $template = array();
102
        array_push($template, ['id' => 'user', 'name' => 'GitHub Username', 'placeholder' => 'GitHub user']);
103
        array_push($template, ['id' => 'repository', 'name' => 'Repository', 'placeholder' => 'GitHub repository']);
104
        array_push($template, ['id' => 'limit', 'name' => 'Limit', 'placeholder' => 'Number of records']);
105
        array_push($template, ['id' => 'timestamp', 'name' => 'Timestamp of dataload', 'placeholder' => 'true/false']);
106
        array_push($template, ['id' => 'delete', 'name' => 'Delete all data before load', 'placeholder' => 'true/false']);
107
        return $template;
108
    }
109
110
    private function floatvalue($val)
111
    {
112
        $val = str_replace(",", ".", $val);
113
        $val = preg_replace('/\.(?=.*\.)/', '', $val);
114
        $val = preg_replace('/[^0-9-.]+/', '', $val);
115
        if (is_numeric($val)) {
116
            return floatval($val);
117
        } else {
118
            return false;
119
        }
120
    }
121
}