Passed
Push — master ( 14e493...9c2d3e )
by Marcel
02:30
created

JsonService::getTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
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 JsonService
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
    public function getName(): string
32
    {
33
        return $this->l10n->t('JSON');
34
    }
35
36
    /**
37
     * Get the items for the selected category
38
     *
39
     * @NoAdminRequired
40
     * @param array $option
41
     * @return array
42
     */
43
    public function read($option)
44
    {
45
        $string = $option['url'];
46
        $path = $option['path'];
47
        $auth = $option['auth'];
48
        $data = array();
49
50
        $ch = curl_init();
51
        if ($ch !== false) {
52
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
53
            curl_setopt($ch, CURLOPT_URL, $string);
54
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
55
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
56
                'OCS-APIRequest: true'
57
            ));
58
            curl_setopt($ch, CURLOPT_USERPWD, $auth);
59
            curl_setopt($ch, CURLOPT_VERBOSE, true);
60
            $curlResult = curl_exec($ch);
61
            curl_close($ch);
62
        } else {
63
            $curlResult = '';
64
        }
65
66
        //$this->logger->debug($curlResult);
67
        $json = json_decode($curlResult, true);
68
        $array = $this->get_nested_array_value($json, $path);
69
70
        if (is_array($array)) {
71
            foreach ($array as $key => $value) {
72
                $group = end(explode('/', $path));
73
                array_push($data, [$group, $key, $value]);
74
            }
75
        } else {
76
            $key = end(explode('/', $path));
77
            array_push($data, ['', $key, $array]);
78
        }
79
80
        $header = array();
81
        $header[0] = '';
82
        $header[1] = 'Key';
83
        $header[2] = 'Value';
84
85
        return [
86
            'header' => $header,
87
            'data' => $data,
88
            'error' => 0,
89
        ];
90
    }
91
92
    /**
93
     * get array object from string
94
     *
95
     * @NoAdminRequired
96
     * @param $array
97
     * @param $path
98
     * @param string $delimiter
99
     * @return array|string
100
     */
101
    private function get_nested_array_value(&$array, $path, $delimiter = '/')
102
    {
103
        $pathParts = explode($delimiter, $path);
104
105
        $current = &$array;
106
        foreach ($pathParts as $key) {
107
            $current = &$current[$key];
108
        }
109
        return $current;
110
    }
111
112
    /**
113
     * template for options & settings
114
     *
115
     * @NoAdminRequired
116
     * @return array
117
     */
118
    public function getTemplate()
119
    {
120
        $template = array();
121
        array_push($template, ['id' => 'url', 'name' => 'JSON Url', 'placeholder' => 'url']);
122
        array_push($template, ['id' => 'auth', 'name' => 'Authentication', 'placeholder' => 'User:Password']);
123
        array_push($template, ['id' => 'path', 'name' => 'JSON path', 'placeholder' => 'x/y/z']);
124
        array_push($template, ['id' => 'timestamp', 'name' => 'Timestamp of dataload', 'placeholder' => 'true/false']);
125
        array_push($template, ['id' => 'delete', 'name' => 'Delete all data before load', 'placeholder' => 'true/false']);
126
        return $template;
127
    }
128
}