Passed
Push — master ( dfddba...5403dd )
by Marcel
03:03
created

DatasourceController::getDatasources()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\Controller;
13
14
use OCA\Analytics\Datasource\DatasourceEvent;
15
use OCA\Analytics\Datasource\ExternalFile;
16
use OCA\Analytics\Datasource\File;
17
use OCA\Analytics\Datasource\Github;
18
use OCA\Analytics\Datasource\Json;
19
use OCA\Analytics\Datasource\Regex;
20
use OCP\AppFramework\Controller;
21
use OCP\EventDispatcher\IEventDispatcher;
22
use OCP\Files\NotFoundException;
23
use OCP\IL10N;
24
use OCP\ILogger;
25
use OCP\IRequest;
26
27
class DatasourceController extends Controller
28
{
29
    private $logger;
30
    private $GithubService;
31
    private $FileService;
32
    private $ExternalFileService;
33
    private $RegexService;
34
    private $JsonService;
35
    /** @var IEventDispatcher */
36
    private $dispatcher;
37
    private $l10n;
38
39
    const DATASET_TYPE_GROUP = 0;
40
    const DATASET_TYPE_INTERNAL_FILE = 1;
41
    const DATASET_TYPE_INTERNAL_DB = 2;
42
    const DATASET_TYPE_GIT = 3;
43
    const DATASET_TYPE_EXTERNAL_FILE = 4;
44
    const DATASET_TYPE_REGEX = 5;
45
    const DATASET_TYPE_JSON = 6;
46
47
    public function __construct(
48
        string $AppName,
49
        IRequest $request,
50
        ILogger $logger,
51
        Github $GithubService,
52
        File $FileService,
53
        Regex $RegexService,
54
        Json $JsonService,
55
        ExternalFile $ExternalFileService,
56
        IL10N $l10n,
57
        IEventDispatcher $dispatcher
58
    )
59
    {
60
        parent::__construct($AppName, $request);
61
        $this->logger = $logger;
62
        $this->ExternalFileService = $ExternalFileService;
63
        $this->GithubService = $GithubService;
64
        $this->RegexService = $RegexService;
65
        $this->FileService = $FileService;
66
        $this->JsonService = $JsonService;
67
        $this->dispatcher = $dispatcher;
68
        $this->l10n = $l10n;
69
    }
70
71
    /**
72
     * get all datasource ids + names
73
     *
74
     * @NoAdminRequired
75
     */
76
    public function index()
77
    {
78
        $datasources = array();
79
        $result = array();
80
        foreach ($this->getDatasources() as $key => $class) {
81
            $datasources[$key] = $class->getName();
82
        }
83
        $result['datasources'] = $datasources;
84
85
        $options = array();
86
        foreach ($this->getDatasources() as $key => $class) {
87
            $options[$key] = $class->getTemplate();
88
        }
89
        $result['options'] = $options;
90
91
        return $result;
92
    }
93
94
    /**
95
     * get all datasource templates
96
     *
97
     * @NoAdminRequired
98
     * @return array
99
     */
100
    public function getTemplates()
101
    {
102
        $result = array();
103
        foreach ($this->getDatasources() as $key => $class) {
104
            $result[$key] = $class->getTemplate();
105
        }
106
        return $result;
107
    }
108
109
    /**
110
     * Get the data from a datasource;
111
     *
112
     * @NoAdminRequired
113
     * @param int $datasourceId
114
     * @param $option
115
     * @return array|NotFoundException
116
     */
117
    public function read(int $datasourceId, $option)
118
    {
119
        //$this->logger->debug('DatasourceController 66: Datasource Id: ' . $datasource . ', Option: ' . json_encode($option));
120
        $result = $this->getDatasources()[$datasourceId]->readData($option);
121
122
        if (isset($option['timestamp']) and $option['timestamp'] === 'true') {
123
            // if datasource should be timestamped/snapshoted
124
            // shift values by one dimension and stores date in second column
125
            $result['data'] = array_map(function ($tag) {
126
                $columns = count($tag);
127
                return array($tag[$columns - 2], $tag[$columns - 2], $tag[$columns - 1]);
128
            }, $result['data']);
129
            $result['data'] = $this->replaceDimension($result['data'], 1, date("Y-m-d H:i:s"));
130
        }
131
        return $result;
132
    }
133
134
    /**
135
     * combine internal and registered datasources
136
     * @return array
137
     */
138
    private function getDatasources()
139
    {
140
        return $this->getOwnDatasources() + $this->getRegisteredDatasources();
141
    }
142
143
    /**
144
     * map all internal datasources to their IDs
145
     * @return array
146
     */
147
    private function getOwnDatasources()
148
    {
149
        $datasources = [];
150
        $datasources[self::DATASET_TYPE_INTERNAL_FILE] = $this->FileService;
151
        $datasources[self::DATASET_TYPE_GIT] = $this->GithubService;
152
        $datasources[self::DATASET_TYPE_EXTERNAL_FILE] = $this->ExternalFileService;
153
        $datasources[self::DATASET_TYPE_REGEX] = $this->RegexService;
154
        $datasources[self::DATASET_TYPE_JSON] = $this->JsonService;
155
        return $datasources;
156
    }
157
158
    /**
159
     * map all registered datasources to their IDs
160
     * @return array
161
     */
162
    private function getRegisteredDatasources()
163
    {
164
        $datasources = [];
165
        $event = new DatasourceEvent();
166
        $this->dispatcher->dispatchTyped($event);
167
168
        foreach ($event->getDataSources() as $class) {
169
            $uniqueId = '99' . \OC::$server->get($class)->getId();
170
171
            if (isset($datasources[$uniqueId])) {
172
                $this->logger->logException(new \InvalidArgumentException('Datasource with the same ID already registered: ' . \OC::$server->get($class)->getName()), ['level' => ILogger::INFO]);
0 ignored issues
show
Deprecated Code introduced by
The function OCP\ILogger::logException() has been deprecated: 20.0.0 use the `exception` entry in the context of any method in \Psr\Log\LoggerInterface ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

172
                /** @scrutinizer ignore-deprecated */ $this->logger->logException(new \InvalidArgumentException('Datasource with the same ID already registered: ' . \OC::$server->get($class)->getName()), ['level' => ILogger::INFO]);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Deprecated Code introduced by
The constant OCP\ILogger::INFO has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

172
                $this->logger->logException(new \InvalidArgumentException('Datasource with the same ID already registered: ' . \OC::$server->get($class)->getName()), ['level' => /** @scrutinizer ignore-deprecated */ ILogger::INFO]);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
173
                continue;
174
            }
175
            $datasources[$uniqueId] = \OC::$server->get($class);
176
        }
177
        return $datasources;
178
    }
179
180
    /**
181
     * replace all values of one dimension
182
     *
183
     * @NoAdminRequired
184
     * @param $Array
185
     * @param $Find
186
     * @param $Replace
187
     * @return array
188
     */
189
    private function replaceDimension($Array, $Find, $Replace)
190
    {
191
        if (is_array($Array)) {
192
            foreach ($Array as $Key => $Val) {
193
                if (is_array($Array[$Key])) {
194
                    $Array[$Key] = $this->replaceDimension($Array[$Key], $Find, $Replace);
195
                } else {
196
                    if ($Key === $Find) {
197
                        $Array[$Key] = $Replace;
198
                    }
199
                }
200
            }
201
        }
202
        return $Array;
203
    }
204
}