Passed
Branch master (7b1276)
by Marcel
06:24
created

GithubService::read()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 47
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 29
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 47
rs 9.1448
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 OCA\Analytics\Controller\DbController;
15
use OCP\ILogger;
16
17
class GithubService
18
{
19
    private $logger;
20
    private $DBController;
21
22
    public function __construct(
23
        ILogger $logger,
24
        DbController $DBController
25
    )
26
    {
27
        $this->logger = $logger;
28
        $this->DBController = $DBController;
29
    }
30
31
    /**
32
     * Get the items for the selected category
33
     *
34
     * @NoAdminRequired
35
     * @param $datasetMetadata
36
     * @return array
37
     */
38
    public function read($datasetMetadata)
39
    {
40
        $string = 'https://api.github.com/repos/' . $datasetMetadata['link'] . '/releases';
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
        $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

50
        $result = 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
        #echo $result;
53
54
        $jason_a = json_decode($result, true);
55
        $i = 0;
56
57
        //$tagName = $jason_a[0]['tag_name'];
58
        //$tagVariable = 'tag_name';
59
        //$tagName = $jason_a[0][$tagVariable];
60
        //$count = $jason_a[0] . assets . download_cont;
61
        //$this->logger->error($tagName . $count);
62
63
        $data = array();
64
        foreach ($jason_a as $item) {
65
            if ($i === 15) break;
66
            $nc_value = 0;
67
            foreach ($item['assets'] as $asset) {
68
                if (substr($asset['name'], -2) == 'gz') $nc_value = $asset['download_count'];
69
            }
70
            array_push($data, ['dimension2' => $item['tag_name'], 'dimension3' => $nc_value]);
71
            $i++;
72
        }
73
74
        $header = array();
75
        $header['dimension1'] = '';
76
        $header['dimension2'] = 'Version';
77
        $header['dimension3'] = 'Count';
78
79
        $result = [
80
            'header' => $header,
81
            'data' => $data
82
        ];
83
84
        return $result;
85
    }
86
}