Passed
Push — master ( 9179ab...b51dc7 )
by Marcel
02:24
created

DatasetService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 2
b 0
f 0
nc 1
nop 12
dl 0
loc 27
rs 9.8666

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\ReportMapper;
19
use OCA\Analytics\Db\StorageMapper;
20
use OCP\AppFramework\Http\DataDownloadResponse;
21
use OCP\AppFramework\Http\DataResponse;
22
use OCP\DB\Exception;
23
use OCP\Files\IRootFolder;
24
use OCP\Files\NotFoundException;
25
use OCP\ITagManager;
26
use Psr\Log\LoggerInterface;
27
28
class DatasetService
29
{
30
    private $userId;
31
    private $logger;
32
    private $tagManager;
33
    private $ShareService;
34
    private $StorageMapper;
35
    private $DatasetMapper;
36
    private $ThresholdService;
37
    private $DataloadMapper;
38
    private $ActivityManager;
39
    private $rootFolder;
40
    private $VariableService;
41
    private $ReportMapper;
42
43
    public function __construct(
44
        $userId,
45
        LoggerInterface $logger,
46
        ITagManager $tagManager,
47
        ShareService $ShareService,
48
        StorageMapper $StorageMapper,
49
        DatasetMapper $DatasetMapper,
50
        ThresholdService $ThresholdService,
51
        DataloadMapper $DataloadMapper,
52
        ActivityManager $ActivityManager,
53
        IRootFolder $rootFolder,
54
        VariableService $VariableService,
55
        ReportMapper $ReportMapper
56
    )
57
    {
58
        $this->userId = $userId;
59
        $this->logger = $logger;
60
        $this->tagManager = $tagManager;
61
        $this->ShareService = $ShareService;
62
        $this->ThresholdService = $ThresholdService;
63
        $this->StorageMapper = $StorageMapper;
64
        $this->DatasetMapper = $DatasetMapper;
65
        $this->DataloadMapper = $DataloadMapper;
66
        $this->ActivityManager = $ActivityManager;
67
        $this->rootFolder = $rootFolder;
68
        $this->VariableService = $VariableService;
69
        $this->ReportMapper = $ReportMapper;
70
    }
71
72
    /**
73
     * get all datasets
74
     *
75
     * @return array
76
     */
77
    public function index()
78
    {
79
        $ownDatasets = $this->DatasetMapper->index();
80
81
        // get dataload indicators for icons shown in the advanced screen
82
        $dataloads = $this->DataloadMapper->getAllDataloadMetadata();
83
        foreach ($dataloads as $dataload) {
84
            $key = array_search($dataload['dataset'], array_column($ownDatasets, 'id'));
85
            if ($key !== '') {
86
                if ($dataload['schedules'] !== '' and $dataload['schedules'] !== null) {
87
                    $dataload['schedules'] = 1;
88
                } else {
89
                    $dataload['schedules'] = 0;
90
                }
91
                $ownDatasets[$key]['dataloads'] = $dataload['dataloads'];
92
                $ownDatasets[$key]['schedules'] = $dataload['schedules'];
93
            }
94
        }
95
96
        foreach ($ownDatasets as &$ownDataset) {
97
            $ownDataset['type'] = DatasourceController::DATASET_TYPE_INTERNAL_DB;
98
            $ownDataset = $this->VariableService->replaceTextVariables($ownDataset);
99
        }
100
101
        return $ownDatasets;
102
    }
103
104
    /**
105
     * get own dataset details
106
     *
107
     * @param int $datasetId
108
     * @return array|bool
109
     * @throws Exception
110
     */
111
    public function readOwn(int $datasetId)
112
    {
113
        $ownDataset = $this->DatasetMapper->readOwn($datasetId);
114
        if (! empty($ownDataset)) {
115
            $ownDataset['permissions'] = \OCP\Constants::PERMISSION_UPDATE;
116
        }
117
        return $ownDataset;
118
    }
119
120
    /**
121
     * get  dataset details
122
     *
123
     * @param int $datasetId
124
     * @return array|bool
125
     * @throws Exception
126
     */
127
    public function read(int $datasetId)
128
    {
129
        $ownDataset = $this->DatasetMapper->read($datasetId);
130
        return $ownDataset;
131
    }
132
133
    /**
134
     * get dataset status
135
     *
136
     * @param int $datasetId
137
     * @return array|bool
138
     * @throws Exception
139
     */
140
    public function status(int $datasetId): array
141
    {
142
        $status = array();
143
        $status['reports'] = $this->ReportMapper->reportsForDataset($datasetId);
144
        $status['data'] = $this->StorageMapper->getRecordCount($datasetId);
145
        return $status;
146
    }
147
148
    /**
149
     * create new dataset
150
     *
151
     * @param $name
152
     * @param $dimension1
153
     * @param $dimension2
154
     * @param $value
155
     * @return int
156
     * @throws Exception
157
     */
158
    public function create($name, $dimension1, $dimension2, $value)
159
    {
160
        //$this->ActivityManager->triggerEvent(0, ActivityManager::OBJECT_DATASET, ActivityManager::SUBJECT_DATASET_ADD);
161
        return $this->DatasetMapper->create($name, $dimension1, $dimension2, $value);
162
    }
163
164
    /**
165
     * get dataset details
166
     *
167
     * @param int $datasetId
168
     * @param $name
169
     * @param $dimension1
170
     * @param $dimension2
171
     * @param $value
172
     * @return bool
173
     * @throws Exception
174
     */
175
    public function update(int $datasetId, $name,$dimension1 = null, $dimension2 = null, $value = null)
176
    {
177
        return $this->DatasetMapper->update($datasetId, $name, $dimension1, $dimension2, $value);
178
    }
179
180
    /**
181
     * Export Dataset
182
     *
183
     * @param int $datasetId
184
     * @return DataDownloadResponse
185
     * @throws Exception
186
     */
187
    public function export(int $datasetId)
188
    {
189
        $result = array();
190
        $result['dataset'] = $this->DatasetMapper->read($datasetId);
191
        $result['dataload'] = $this->DataloadMapper->read($datasetId);
192
        $result['threshold'] = $this->ThresholdService->read($datasetId);
193
        $result['favorite'] = '';
194
195
        if ($result['dataset']['type'] === DatasourceController::DATASET_TYPE_INTERNAL_DB) {
196
            $result['data'] = $this->StorageMapper->read($datasetId);
197
        }
198
199
        unset($result['dataset']['id'], $result['dataset']['user_id'], $result['dataset']['user_id'], $result['dataset']['parent']);
200
        $data = json_encode($result);
201
        return new DataDownloadResponse($data, $result['dataset']['name'] . '.export.txt', 'text/plain; charset=utf-8');
202
    }
203
204
    /**
205
     * Delete Dataset and all depending objects
206
     *
207
     * @param int $datasetId
208
     * @return bool
209
     * @throws Exception
210
     */
211
    public function delete(int $datasetId)
212
    {
213
        $this->DatasetMapper->delete($datasetId);
214
        $this->DataloadMapper->deleteDataloadByDataset($datasetId);
215
        $this->StorageMapper->deleteByDataset($datasetId);
216
        return true;
217
    }
218
}