Passed
Push — master ( 0d32dd...265c4a )
by Marcel
02:32
created

DatasetService::index()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 15
c 1
b 0
f 0
nc 8
nop 0
dl 0
loc 25
rs 9.2222
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\Files\IRootFolder;
23
use OCP\Files\NotFoundException;
24
use OCP\ITagManager;
25
use Psr\Log\LoggerInterface;
26
27
class DatasetService
28
{
29
    private $userId;
30
    private $logger;
31
    private $tagManager;
32
    private $ShareService;
33
    private $StorageMapper;
34
    private $DatasetMapper;
35
    private $ThresholdService;
36
    private $DataloadMapper;
37
    private $ActivityManager;
38
    private $rootFolder;
39
    private $VariableService;
40
    private $ReportMapper;
41
42
    public function __construct(
43
        $userId,
44
        LoggerInterface $logger,
45
        ITagManager $tagManager,
46
        ShareService $ShareService,
47
        StorageMapper $StorageMapper,
48
        DatasetMapper $DatasetMapper,
49
        ThresholdService $ThresholdService,
50
        DataloadMapper $DataloadMapper,
51
        ActivityManager $ActivityManager,
52
        IRootFolder $rootFolder,
53
        VariableService $VariableService,
54
        ReportMapper $ReportMapper
55
    )
56
    {
57
        $this->userId = $userId;
58
        $this->logger = $logger;
59
        $this->tagManager = $tagManager;
60
        $this->ShareService = $ShareService;
61
        $this->ThresholdService = $ThresholdService;
62
        $this->StorageMapper = $StorageMapper;
63
        $this->DatasetMapper = $DatasetMapper;
64
        $this->DataloadMapper = $DataloadMapper;
65
        $this->ActivityManager = $ActivityManager;
66
        $this->rootFolder = $rootFolder;
67
        $this->VariableService = $VariableService;
68
        $this->ReportMapper = $ReportMapper;
69
    }
70
71
    /**
72
     * get all datasets
73
     *
74
     * @return DataResponse
75
     */
76
    public function index()
77
    {
78
        $ownDatasets = $this->DatasetMapper->index();
79
80
        // get dataload indicators for icons shown in the advanced screen
81
        $dataloads = $this->DataloadMapper->getAllDataloadMetadata();
82
        foreach ($dataloads as $dataload) {
83
            $key = array_search($dataload['dataset'], array_column($ownDatasets, 'id'));
84
            if ($key !== '') {
85
                if ($dataload['schedules'] !== '' and $dataload['schedules'] !== null) {
86
                    $dataload['schedules'] = 1;
87
                } else {
88
                    $dataload['schedules'] = 0;
89
                }
90
                $ownDatasets[$key]['dataloads'] = $dataload['dataloads'];
91
                $ownDatasets[$key]['schedules'] = $dataload['schedules'];
92
            }
93
        }
94
95
        foreach ($ownDatasets as &$ownDataset) {
96
            $ownDataset['type'] = DatasourceController::DATASET_TYPE_INTERNAL_DB;
97
            $ownDataset = $this->VariableService->replaceTextVariables($ownDataset);
98
        }
99
100
        return new DataResponse($ownDatasets);
101
    }
102
103
    /**
104
     * get own dataset details
105
     *
106
     * @param int $datasetId
107
     * @param string|null $user_id
108
     * @return array|bool
109
     * @throws \OCP\DB\Exception
110
     */
111
    public function read(int $datasetId, string $user_id = null): array
112
    {
113
        $ownDataset = $this->DatasetMapper->read($datasetId, $user_id);
114
        if ($ownDataset) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $ownDataset of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
115
            $ownDataset['permissions'] = \OCP\Constants::PERMISSION_UPDATE;
116
        }
117
        return $ownDataset;
118
    }
119
120
    /**
121
     * get dataset status
122
     *
123
     * @param int $datasetId
124
     * @return array|bool
125
     * @throws \OCP\DB\Exception
126
     */
127
    public function status(int $datasetId): array
128
    {
129
        $status['reports'] = $this->ReportMapper->reportsForDataset($datasetId);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$status was never initialized. Although not strictly required by PHP, it is generally a good practice to add $status = array(); before regardless.
Loading history...
130
        $status['data'] = $this->StorageMapper->getRecordCount($datasetId);
131
        return $status;
132
    }
133
134
    /**
135
     * create new dataset
136
     *
137
     * @param $name
138
     * @param $dimension1
139
     * @param $dimension2
140
     * @param $value
141
     * @return int
142
     * @throws \OCP\DB\Exception
143
     */
144
    public function create($name, $dimension1, $dimension2, $value)
145
    {
146
        //$this->ActivityManager->triggerEvent(0, ActivityManager::OBJECT_DATASET, ActivityManager::SUBJECT_DATASET_ADD);
147
        return $this->DatasetMapper->create($name, $dimension1, $dimension2, $value);
148
    }
149
150
    /**
151
     * get dataset details
152
     *
153
     * @param int $datasetId
154
     * @param $name
155
     * @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...
156
     * @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...
157
     * @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...
158
     * @return bool
159
     * @throws \OCP\DB\Exception
160
     */
161
    public function update(int $datasetId, $name,$dimension1 = null, $dimension2 = null, $value = null)
162
    {
163
        return $this->DatasetMapper->update($datasetId, $name, $dimension1, $dimension2, $value);
164
    }
165
166
    /**
167
     * Export Dataset
168
     *
169
     * @param int $datasetId
170
     * @return DataDownloadResponse
171
     * @throws \OCP\DB\Exception
172
     */
173
    public function export(int $datasetId)
174
    {
175
        $result = array();
176
        $result['dataset'] = $this->DatasetMapper->read($datasetId);
177
        $result['dataload'] = $this->DataloadMapper->read($datasetId);
178
        $result['threshold'] = $this->ThresholdService->read($datasetId);
179
        $result['favorite'] = '';
180
181
        if ($result['dataset']['type'] === DatasourceController::DATASET_TYPE_INTERNAL_DB) {
182
            $result['data'] = $this->StorageMapper->read($datasetId);
183
        }
184
185
        unset($result['dataset']['id'], $result['dataset']['user_id'], $result['dataset']['user_id'], $result['dataset']['parent']);
186
        $data = json_encode($result);
187
        return new DataDownloadResponse($data, $result['dataset']['name'] . '.export.txt', 'text/plain; charset=utf-8');
188
    }
189
190
    /**
191
     * Delete Dataset and all depending objects
192
     *
193
     * @param int $datasetId
194
     * @return bool
195
     * @throws \OCP\DB\Exception
196
     */
197
    public function delete(int $datasetId)
198
    {
199
        $this->DatasetMapper->delete($datasetId);
200
        $this->DataloadMapper->deleteDataloadByDataset($datasetId);
201
        $this->StorageMapper->deleteByDataset($datasetId);
202
        return true;
203
    }
204
}