Passed
Push — master ( b14300...20328b )
by Marcel
06:03
created

JsonService::get_nested_array_value()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 9
rs 10
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 2020 Marcel Scherello
10
 */
11
12
namespace OCA\Analytics\Datasource;
13
14
use OCP\ILogger;
15
16
class JsonService
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
        $string = $option['url'];
37
        $path = $option['path'];
38
        $auth = $option['auth'];
39
        $data = array();
40
41
        $ch = curl_init();
42
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
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

42
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_SSL_VERIFYPEER, true);
Loading history...
43
        curl_setopt($ch, CURLOPT_URL, $string);
44
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
45
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
46
            'OCS-APIRequest: true'
47
        ));
48
        curl_setopt($ch, CURLOPT_USERPWD, $auth);
49
        curl_setopt($ch, CURLOPT_VERBOSE, true);
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
53
        $json = json_decode($result, true);
54
        $array = $this->get_nested_array_value($json, $path);
55
56
        if (is_array($array)) {
0 ignored issues
show
introduced by
The condition is_array($array) is always true.
Loading history...
57
            foreach ($array as $key => $value) {
58
                array_push($data, ['dimension1' => '', 'dimension2' => $key, 'dimension3' => $value]);
59
            }
60
        } else {
61
            $key = end(explode('/', $path));
62
            array_push($data, ['dimension1' => '', 'dimension2' => $key, 'dimension3' => $array]);
63
        }
64
65
        $header = array();
66
        $header['dimension1'] = '';
67
        $header['dimension2'] = 'Key';
68
        $header['dimension3'] = 'Value';
69
70
        $result = [
71
            'header' => $header,
72
            'data' => $data,
73
            'error' => 0,
74
        ];
75
76
        return $result;
77
    }
78
79
    /**
80
     * get array object from string
81
     *
82
     * @NoAdminRequired
83
     * @param $array
84
     * @param $path
85
     * @param string $delimiter
86
     * @return array
87
     */
88
    private function get_nested_array_value(&$array, $path, $delimiter = '/')
89
    {
90
        $pathParts = explode($delimiter, $path);
91
92
        $current = &$array;
93
        foreach ($pathParts as $key) {
94
            $current = &$current[$key];
95
        }
96
        return $current;
97
    }
98
99
    /**
100
     * template for options & settings
101
     *
102
     * @NoAdminRequired
103
     * @return array
104
     */
105
    public function getTemplate()
106
    {
107
        $template = array();
108
        array_push($template, ['id' => 'url', 'name' => 'JSON Url', 'placeholder' => 'url']);
109
        array_push($template, ['id' => 'auth', 'name' => 'Authentication', 'placeholder' => 'User:Password']);
110
        array_push($template, ['id' => 'path', 'name' => 'JSON path', 'placeholder' => 'x/y/z']);
111
        array_push($template, ['id' => 'timestamp', 'name' => 'Timestamp of dataload', 'placeholder' => 'true/false']);
112
        return $template;
113
    }
114
}