Passed
Push — master ( 0b1a33...6372a5 )
by Marcel
06:49
created

DatasetService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 2
b 0
f 0
nc 1
nop 11
dl 0
loc 25
rs 9.9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\Service;
13
14
use OCA\Analytics\Activity\ActivityManager;
15
use OCA\Analytics\Controller\DatasourceController;
16
use OCA\Analytics\Db\DataloadMapper;
17
use OCA\Analytics\Db\DatasetMapper;
18
use OCA\Analytics\Db\StorageMapper;
19
use OCP\AppFramework\Http\DataDownloadResponse;
20
use OCP\AppFramework\Http\DataResponse;
21
use OCP\Files\IRootFolder;
22
use OCP\Files\NotFoundException;
23
use OCP\ITagManager;
24
use Psr\Log\LoggerInterface;
25
26
class DatasetService
27
{
28
    private $userId;
29
    private $logger;
30
    private $tagManager;
31
    private $ShareService;
32
    private $StorageMapper;
33
    private $DatasetMapper;
34
    private $ThresholdService;
35
    private $DataloadMapper;
36
    private $ActivityManager;
37
    private $rootFolder;
38
    private $VariableService;
39
40
    public function __construct(
41
        $userId,
42
        LoggerInterface $logger,
43
        ITagManager $tagManager,
44
        ShareService $ShareService,
45
        StorageMapper $StorageMapper,
46
        DatasetMapper $DatasetMapper,
47
        ThresholdService $ThresholdService,
48
        DataloadMapper $DataloadMapper,
49
        ActivityManager $ActivityManager,
50
        IRootFolder $rootFolder,
51
        VariableService $VariableService
52
    )
53
    {
54
        $this->userId = $userId;
55
        $this->logger = $logger;
56
        $this->tagManager = $tagManager;
57
        $this->ShareService = $ShareService;
58
        $this->ThresholdService = $ThresholdService;
59
        $this->StorageMapper = $StorageMapper;
60
        $this->DatasetMapper = $DatasetMapper;
61
        $this->DataloadMapper = $DataloadMapper;
62
        $this->ActivityManager = $ActivityManager;
63
        $this->rootFolder = $rootFolder;
64
        $this->VariableService = $VariableService;
65
    }
66
67
    /**
68
     * get all datasets
69
     *
70
     * @return DataResponse
71
     */
72
    public function index()
73
    {
74
        $ownDatasets = $this->DatasetMapper->index();
75
76
        // get dataload indicators for icons shown in the advanced screen
77
        $dataloads = $this->DataloadMapper->getAllDataloadMetadata();
78
        foreach ($dataloads as $dataload) {
79
            $key = array_search($dataload['dataset'], array_column($ownDatasets, 'id'));
80
            if ($key !== '') {
81
                if ($dataload['schedules'] !== '' and $dataload['schedules'] !== null) {
82
                    $dataload['schedules'] = 1;
83
                } else {
84
                    $dataload['schedules'] = 0;
85
                }
86
                $ownDatasets[$key]['dataloads'] = $dataload['dataloads'];
87
                $ownDatasets[$key]['schedules'] = $dataload['schedules'];
88
            }
89
        }
90
91
        foreach ($ownDatasets as &$ownDataset) {
92
            $ownDataset = $this->VariableService->replaceTextVariables($ownDataset);
93
        }
94
95
        return new DataResponse($ownDatasets);
96
    }
97
98
    /**
99
     * get own dataset details
100
     *
101
     * @param int $datasetId
102
     * @param string|null $user_id
103
     * @return array
104
     * @throws \OCP\DB\Exception
105
     */
106
    public function read(int $datasetId, string $user_id = null): array
107
    {
108
        $ownDataset = $this->DatasetMapper->read($datasetId, $user_id);
109
        if (!empty($ownDataset)) {
110
            $ownDataset['permissions'] = \OCP\Constants::PERMISSION_UPDATE;
111
        }
112
        return $ownDataset;
113
    }
114
115
    /**
116
     * create new dataset
117
     *
118
     * @param $name
119
     * @param $dimension1
120
     * @param $dimension2
121
     * @param $value
122
     * @return int
123
     * @throws \OCP\DB\Exception
124
     */
125
    public function create($name, $dimension1, $dimension2, $value)
126
    {
127
        //$this->ActivityManager->triggerEvent(0, ActivityManager::OBJECT_DATASET, ActivityManager::SUBJECT_DATASET_ADD);
128
        return $this->DatasetMapper->create($name, $dimension1, $dimension2, $value);
129
    }
130
131
    /**
132
     * get dataset details
133
     *
134
     * @param int $datasetId
135
     * @param $name
136
     * @param null $dimension1
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $dimension1 is correct as it would always require null to be passed?
Loading history...
137
     * @param null $dimension2
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $dimension2 is correct as it would always require null to be passed?
Loading history...
138
     * @param null $value
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $value is correct as it would always require null to be passed?
Loading history...
139
     * @return bool
140
     * @throws \OCP\DB\Exception
141
     */
142
    public function update(int $datasetId, $name,$dimension1 = null, $dimension2 = null, $value = null)
143
    {
144
        return $this->DatasetMapper->update($datasetId, $name, $dimension1, $dimension2, $value);
145
    }
146
147
    /**
148
     * Import Dataset from File
149
     *
150
     * @param string|null $path
151
     * @param string|null $raw
152
     * @return int
153
     * @throws NotFoundException
154
     * @throws \OCP\DB\Exception
155
     * @throws \OCP\Files\NotPermittedException
156
     */
157
    public function import(string $path = null, string $raw = null)
158
    {
159
        if ($path !== '') {
160
            $file = $this->rootFolder->getUserFolder($this->userId)->get($path);
161
            $data = $file->getContent();
0 ignored issues
show
Bug introduced by
The method getContent() does not exist on OCP\Files\Node. It seems like you code against a sub-type of OCP\Files\Node such as OCP\Files\File. ( Ignorable by Annotation )

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

161
            /** @scrutinizer ignore-call */ 
162
            $data = $file->getContent();
Loading history...
162
        } else if ($raw !== null) {
163
            $data = $raw;
164
        } else {
165
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type integer.
Loading history...
166
        }
167
        $data = json_decode($data, true);
168
169
        $dataset = $data['dataset'];
170
        isset($dataset['name']) ? $name = $dataset['name'] : $name = '';
171
        isset($dataset['subheader']) ? $subheader = $dataset['subheader'] : $subheader = '';
172
        $parent = 0;
173
        isset($dataset['type']) ? $type = $dataset['type'] : $type = null;
174
        isset($dataset['link']) ? $link = $dataset['link'] : $link = null;
175
        isset($dataset['visualization']) ? $visualization = $dataset['visualization'] : $visualization = null;
176
        isset($dataset['chart']) ? $chart = $dataset['chart'] : $chart = null;
177
        isset($dataset['chartoptions']) ? $chartoptions = $dataset['chartoptions'] : $chartoptions = null;
178
        isset($dataset['dataoptions']) ? $dataoptions = $dataset['dataoptions'] : $dataoptions = null;
179
        isset($dataset['filteroptions']) ? $filteroptions = $dataset['filteroptions'] : $filteroptions = null;
180
        isset($dataset['dimension1']) ? $dimension1 = $dataset['dimension1'] : $dimension1 = null;
181
        isset($dataset['dimension2']) ? $dimension2 = $dataset['dimension2'] : $dimension2 = null;
182
        isset($dataset['value']) ? $value = $dataset['value'] : $value = null;
183
184
        $datasetId = $this->DatasetMapper->create();
0 ignored issues
show
Bug introduced by
The call to OCA\Analytics\Db\DatasetMapper::create() has too few arguments starting with name. ( Ignorable by Annotation )

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

184
        /** @scrutinizer ignore-call */ 
185
        $datasetId = $this->DatasetMapper->create();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
185
        $this->DatasetMapper->update($datasetId, $name, $subheader, $parent, $type, $link, $visualization, $chart, $chartoptions, $dataoptions, $dimension1, $dimension2, $value, $filteroptions);
0 ignored issues
show
Unused Code introduced by
The call to OCA\Analytics\Db\DatasetMapper::update() has too many arguments starting with $link. ( Ignorable by Annotation )

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

185
        $this->DatasetMapper->/** @scrutinizer ignore-call */ 
186
                              update($datasetId, $name, $subheader, $parent, $type, $link, $visualization, $chart, $chartoptions, $dataoptions, $dimension1, $dimension2, $value, $filteroptions);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
186
187
        foreach ($data['dataload'] as $dataload) {
188
            isset($dataload['datasource']) ? $datasource = $dataload['datasource'] : $datasource = null;
189
            isset($dataload['name']) ? $name = $dataload['name'] : $name = null;
190
            isset($dataload['option']) ? $option = $dataload['option'] : $option = null;
191
            $schedule = null;
192
193
            $dataloadId = $this->DataloadMapper->create($datasetId, $datasource);
194
            $this->DataloadMapper->update($dataloadId, $name, $option, $schedule);
195
        }
196
197
        foreach ($data['threshold'] as $threshold) {
198
            isset($threshold['dimension1']) ? $dimension1 = $threshold['dimension1'] : $dimension1 = null;
199
            isset($threshold['value']) ? $value = $threshold['value'] : $value = null;
200
            isset($threshold['option']) ? $option = $threshold['option'] : $option = null;
201
            isset($threshold['severity']) ? $severity = $threshold['severity'] : $severity = null;
202
            $this->ThresholdService->create($datasetId, $dimension1, $option, $value, $severity);
203
        }
204
205
        foreach ($data['data'] as $dData) {
206
            isset($dData[0]) ? $dimension1 = $dData[0] : $dimension1 = null;
207
            isset($dData[1]) ? $dimension2 = $dData[1] : $dimension2 = null;
208
            isset($dData[2]) ? $value = $dData[2] : $value = null;
209
            $this->StorageMapper->create($datasetId, $dimension1, $dimension2, $value);
210
        }
211
212
        if (isset($data['favorite'])) {
213
            $this->setFavorite($datasetId, $data['favorite']);
0 ignored issues
show
Bug introduced by
The method setFavorite() does not exist on OCA\Analytics\Service\DatasetService. ( Ignorable by Annotation )

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

213
            $this->/** @scrutinizer ignore-call */ 
214
                   setFavorite($datasetId, $data['favorite']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
214
        }
215
216
        return $datasetId;
217
    }
218
219
    /**
220
     * Export Dataset
221
     *
222
     * @param int $datasetId
223
     * @return DataDownloadResponse
224
     * @throws \OCP\DB\Exception
225
     */
226
    public function export(int $datasetId)
227
    {
228
        $result = array();
229
        $result['dataset'] = $this->DatasetMapper->read($datasetId);
230
        $result['dataload'] = $this->DataloadMapper->read($datasetId);
231
        $result['threshold'] = $this->ThresholdService->read($datasetId);
232
        $result['favorite'] = '';
233
234
        if ($result['dataset']['type'] === DatasourceController::DATASET_TYPE_INTERNAL_DB) {
235
            $result['data'] = $this->StorageMapper->read($datasetId);
236
        }
237
238
        unset($result['dataset']['id'], $result['dataset']['user_id'], $result['dataset']['user_id'], $result['dataset']['parent']);
239
        $data = json_encode($result);
240
        return new DataDownloadResponse($data, $result['dataset']['name'] . '.export.txt', 'text/plain; charset=utf-8');
241
    }
242
243
    /**
244
     * Delete Dataset and all depending objects
245
     *
246
     * @param int $datasetId
247
     * @return bool
248
     * @throws \OCP\DB\Exception
249
     */
250
    public function delete(int $datasetId)
251
    {
252
        return $this->DatasetMapper->delete($datasetId);
253
    }
254
255
    /**
256
     * get dataset details
257
     *
258
     * @param int $datasetId
259
     * @param $chartoptions
260
     * @param $dataoptions
261
     * @param $filteroptions
262
     * @return bool
263
     */
264
    public function updateOptions(int $datasetId, $chartoptions, $dataoptions, $filteroptions)
265
    {
266
        return $this->DatasetMapper->updateOptions($datasetId, $chartoptions, $dataoptions, $filteroptions);
0 ignored issues
show
Bug introduced by
The method updateOptions() does not exist on OCA\Analytics\Db\DatasetMapper. Did you maybe mean update()? ( Ignorable by Annotation )

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

266
        return $this->DatasetMapper->/** @scrutinizer ignore-call */ updateOptions($datasetId, $chartoptions, $dataoptions, $filteroptions);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
267
    }
268
}