Passed
Push — master ( 9c2d3e...b67124 )
by Marcel
02:34
created

Json::__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 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' => 'Authentication', 'placeholder' => 'User:Password']);
55
        array_push($template, ['id' => 'path', 'name' => 'JSON path', 'placeholder' => 'x/y/z']);
56
        array_push($template, ['id' => 'timestamp', 'name' => 'Timestamp of dataload', 'placeholder' => 'true/false']);
57
        array_push($template, ['id' => 'delete', 'name' => 'Delete all data before load', 'placeholder' => 'true/false']);
58
        return $template;
59
    }
60
61
    /**
62
     * Read the Data
63
     * @param $option
64
     * @return array available options of the datasoure
65
     */
66
    public function readData($option): array
67
    {
68
        $string = $option['url'];
69
        $path = $option['path'];
70
        $auth = $option['auth'];
71
        $data = array();
72
73
        $ch = curl_init();
74
        if ($ch !== false) {
75
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
76
            curl_setopt($ch, CURLOPT_URL, $string);
77
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
78
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
79
                'OCS-APIRequest: true'
80
            ));
81
            curl_setopt($ch, CURLOPT_USERPWD, $auth);
82
            curl_setopt($ch, CURLOPT_VERBOSE, true);
83
            $curlResult = curl_exec($ch);
84
            curl_close($ch);
85
        } else {
86
            $curlResult = '';
87
        }
88
89
        //$this->logger->debug($curlResult);
90
        $json = json_decode($curlResult, true);
91
        $array = $this->get_nested_array_value($json, $path);
92
93
        if (is_array($array)) {
94
            foreach ($array as $key => $value) {
95
                $pathArray = explode('/', $path);
96
                $group = end($pathArray);
97
                array_push($data, [$group, $key, $value]);
98
            }
99
        } else {
100
            $pathArray = explode('/', $path);
101
            $key = end($pathArray);
102
            array_push($data, ['', $key, $array]);
103
        }
104
105
        $header = array();
106
        $header[0] = '';
107
        $header[1] = 'Key';
108
        $header[2] = 'Value';
109
110
        return [
111
            'header' => $header,
112
            'data' => $data,
113
            'error' => 0,
114
        ];
115
    }
116
117
    /**
118
     * get array object from string
119
     *
120
     * @NoAdminRequired
121
     * @param $array
122
     * @param $path
123
     * @param string $delimiter
124
     * @return array|string
125
     */
126
    private function get_nested_array_value(&$array, $path, $delimiter = '/')
127
    {
128
        $pathParts = explode($delimiter, $path);
129
130
        $current = &$array;
131
        foreach ($pathParts as $key) {
132
            $current = &$current[$key];
133
        }
134
        return $current;
135
    }
136
137
}