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

GithubService::read()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 45
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 32
nc 4
nop 1
dl 0
loc 45
rs 8.4746
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 GithubService
17
{
18
    private $logger;
19
20
    public function __construct(
21
        ILogger $logger
22
    )
23
    {
24
        $this->logger = $logger;
25
    }
26
27
    /**
28
     * Get the items for the selected category
29
     *
30
     * @NoAdminRequired
31
     * @param array $option
32
     * @return array
33
     */
34
    public function read($option)
35
    {
36
        if (isset($option['link'])) $string = 'https://api.github.com/repos/' . $option['link'] . '/releases';
37
        else $string = 'https://api.github.com/repos/' . $option['user'] . '/' . $option['repository'] . '/releases';
38
39
        $ch = curl_init();
40
        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

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

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

48
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
49
        #echo $result;
50
51
        $jason_a = json_decode($result, true);
52
        $i = 0;
53
54
        $data = array();
55
        foreach ($jason_a as $item) {
56
            if (isset($option['limit'])) {
57
                if ($i === (int)$option['limit']) break;
58
            }
59
            $nc_value = 0;
60
            foreach ($item['assets'] as $asset) {
61
                if (substr($asset['name'], -2) == 'gz') $nc_value = $asset['download_count'];
62
            }
63
            array_push($data, ['dimension1' => '', 'dimension2' => $item['tag_name'], 'dimension3' => $nc_value]);
64
            $i++;
65
        }
66
67
        $header = array();
68
        $header['dimension1'] = '';
69
        $header['dimension2'] = 'Version';
70
        $header['dimension3'] = 'Count';
71
72
        $result = [
73
            'header' => $header,
74
            'data' => $data,
75
            'error' => 0,
76
        ];
77
78
        return $result;
79
    }
80
81
    /**
82
     * template for options & settings
83
     *
84
     * @NoAdminRequired
85
     * @return array
86
     */
87
    public function getTemplate()
88
    {
89
        $template = array();
90
        array_push($template, ['id' => 'user', 'name' => 'GitHub Username', 'placeholder' => 'GitHub user']);
91
        array_push($template, ['id' => 'repository', 'name' => 'Repository', 'placeholder' => 'GitHub repository']);
92
        array_push($template, ['id' => 'limit', 'name' => 'Limit', 'placeholder' => 'Number of records']);
93
        array_push($template, ['id' => 'timestamp', 'name' => 'Timestamp of dataload', 'placeholder' => 'true/false']);
94
        return $template;
95
    }
96
}