Passed
Push — master ( b84320...92b80c )
by Marcel
02:37
created

Json::readData()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 52
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 39
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 52
rs 8.9848

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 2021 Marcel Scherello
10
 */
11
12
namespace OCA\Analytics\Datasource;
13
14
use OCP\IL10N;
15
use OCP\ILogger;
16
17
class Json 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('JSON');
37
    }
38
39
    /**
40
     * @return int digit unique datasource id
41
     */
42
    public function getId(): int
43
    {
44
        return 6;
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' => 'url', 'name' => 'JSON Url', 'placeholder' => 'url']);
54
        array_push($template, ['id' => 'auth', 'name' => $this->l10n->t('Authentication'), 'placeholder' => 'User:Password']);
55
        array_push($template, ['id' => 'path', 'name' => $this->l10n->t('JSON path'), 'placeholder' => 'x/y/z']);
56
        array_push($template, ['id' => 'timestamp', 'name' => $this->l10n->t('Timestamp of dataload'), 'placeholder' => 'false/true', 'type' => 'tf']);
57
        return $template;
58
    }
59
60
    /**
61
     * Read the Data
62
     * @param $option
63
     * @return array available options of the datasoure
64
     */
65
    public function readData($option): array
66
    {
67
        $string = $option['url'];
68
        $path = $option['path'];
69
        $auth = $option['auth'];
70
        $data = array();
71
72
        $ch = curl_init();
73
        if ($ch !== false) {
74
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
75
            curl_setopt($ch, CURLOPT_URL, $string);
76
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
77
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
78
                'OCS-APIRequest: true'
79
            ));
80
            curl_setopt($ch, CURLOPT_USERPWD, $auth);
81
            curl_setopt($ch, CURLOPT_VERBOSE, true);
82
            $curlResult = curl_exec($ch);
83
            curl_close($ch);
84
        } else {
85
            $curlResult = '';
86
        }
87
88
        $json = json_decode($curlResult, true);
0 ignored issues
show
Bug introduced by
It seems like $curlResult can also be of type true; however, parameter $json of json_decode() does only seem to accept string, 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

88
        $json = json_decode(/** @scrutinizer ignore-type */ $curlResult, true);
Loading history...
89
        $paths = explode(',', $path);
90
91
        foreach ($paths as $singlePath) {
92
            $array = $this->get_nested_array_value($json, $singlePath);
93
94
            if (is_array($array)) {
95
                foreach ($array as $key => $value) {
96
                    $pathArray = explode('/', $singlePath);
97
                    $group = end($pathArray);
98
                    array_push($data, [$group, $key, $value]);
99
                }
100
            } else {
101
                $pathArray = explode('/', $singlePath);
102
                $key = end($pathArray);
103
                array_push($data, ['', $key, $array]);
104
            }
105
        }
106
107
        $header = array();
108
        $header[0] = '';
109
        $header[1] = 'Key';
110
        $header[2] = 'Value';
111
112
        return [
113
            'header' => $header,
114
            'dimensions' => array_slice($header, 0, count($header) - 1),
115
            'data' => $data,
116
            'error' => 0,
117
        ];
118
    }
119
120
    /**
121
     * get array object from string
122
     *
123
     * @NoAdminRequired
124
     * @param $array
125
     * @param $path
126
     * @param string $delimiter
127
     * @return array|string
128
     */
129
    private function get_nested_array_value(&$array, $path, $delimiter = '/')
130
    {
131
        $pathParts = explode($delimiter, $path);
132
133
        $current = &$array;
134
        foreach ($pathParts as $key) {
135
            $current = &$current[$key];
136
        }
137
        return $current;
138
    }
139
140
}