Passed
Push — master ( bdffc0...7ee269 )
by Marcel
04:00
created

DatasetService::providerRemoveByUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Analytics
4
 *
5
 * SPDX-FileCopyrightText: 2019-2022 Marcel Scherello
6
 * SPDX-License-Identifier: AGPL-3.0-or-later
7
 */
8
9
namespace OCA\Analytics\Service;
10
11
use OCA\Analytics\Activity\ActivityManager;
12
use OCA\Analytics\Controller\DatasourceController;
13
use OCA\Analytics\Db\DataloadMapper;
14
use OCA\Analytics\Db\DatasetMapper;
15
use OCA\Analytics\Db\ReportMapper;
16
use OCA\Analytics\Db\StorageMapper;
17
use OCP\AppFramework\Http\DataDownloadResponse;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http\DataDownloadResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use OCP\AppFramework\Http\DataResponse;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http\DataResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use OCP\DB\Exception;
0 ignored issues
show
Bug introduced by
The type OCP\DB\Exception was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use OCP\Files\IRootFolder;
0 ignored issues
show
Bug introduced by
The type OCP\Files\IRootFolder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use OCP\Files\NotFoundException;
0 ignored issues
show
Bug introduced by
The type OCP\Files\NotFoundException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use OCP\ITagManager;
0 ignored issues
show
Bug introduced by
The type OCP\ITagManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Psr\Log\LoggerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LoggerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
25
class DatasetService {
26
	private $userId;
27
	private $logger;
28
	private $tagManager;
29
	private $ShareService;
30
	private $StorageMapper;
31
	private $DatasetMapper;
32
	private $ThresholdService;
33
	private $DataloadMapper;
34
	private $ActivityManager;
35
	private $rootFolder;
36
	private $VariableService;
37
	private $ReportMapper;
38
	private $contextChatManager;
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
		ReportMapper $ReportMapper
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
		$this->ReportMapper = $ReportMapper;
66
	}
67
68
	/**
69
	 * get all datasets
70
	 *
71
	 * @return array
72
	 */
73
	public function index() {
74
		$ownDatasets = $this->DatasetMapper->index();
75
76
		// get data load 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['type'] = DatasourceController::DATASET_TYPE_INTERNAL_DB;
93
			$ownDataset['item_type'] = ShareService::SHARE_ITEM_TYPE_DATASET;
94
			$ownDataset = $this->VariableService->replaceTextVariables($ownDataset);
95
		}
96
97
		return $ownDatasets;
98
	}
99
100
	/**
101
	 * get own dataset details; used in external access like dataset controller
102
	 *
103
	 * @param int $datasetId
104
	 * @return array|bool
105
	 * @throws Exception
106
	 */
107
	public function readOwn(int $datasetId) {
108
		$ownDataset = $this->DatasetMapper->readOwn($datasetId);
109
		if (!empty($ownDataset)) {
110
			$ownDataset['permissions'] = \OCP\Constants::PERMISSION_UPDATE;
0 ignored issues
show
Bug introduced by
The type OCP\Constants was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
111
			$ownDataset['dataset'] = $ownDataset['id'];
112
		}
113
		return $ownDataset;
114
	}
115
116
	/**
117
	 * get  dataset details
118
	 *
119
	 * @param int $datasetId
120
	 * @return array|bool
121
	 * @throws Exception
122
	 */
123
	public function read(int $datasetId) {
124
		return $this->DatasetMapper->read($datasetId);
125
	}
126
127
	/**
128
	 * check if own report
129
	 *
130
	 * @param int $reportId
131
	 * @return bool
132
	 */
133
	public function isOwn(int $datasetId) {
134
		$ownDataset = $this->DatasetMapper->readOwn($datasetId);
135
		if (!empty($ownDataset)) {
136
			return true;
137
		} else {
138
			return false;
139
		}
140
	}
141
142
	/**
143
	 * get dataset status
144
	 *
145
	 * @param int $datasetId
146
	 * @return array|bool
147
	 * @throws Exception
148
	 */
149
	public function status(int $datasetId): array {
150
		$status = array();
151
		$status['reports'] = $this->ReportMapper->reportsForDataset($datasetId);
152
		$status['data'] = $this->StorageMapper->getRecordCount($datasetId);
153
		return $status;
154
	}
155
156
	/**
157
	 * create new dataset
158
	 *
159
	 * @param $name
160
	 * @param $dimension1
161
	 * @param $dimension2
162
	 * @param $value
163
	 * @return int
164
	 * @throws Exception
165
	 */
166
	public function create($name, $dimension1, $dimension2, $value) {
167
		$datasetId = $this->DatasetMapper->create($name, $dimension1, $dimension2, $value);
168
		$this->ActivityManager->triggerEvent($datasetId, ActivityManager::OBJECT_DATASET, ActivityManager::SUBJECT_DATASET_ADD);
169
		return $datasetId;
170
	}
171
172
	/**
173
	 * get dataset details
174
	 *
175
	 * @param int $datasetId
176
	 * @param $name
177
	 * @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...
178
	 * @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...
179
	 * @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...
180
	 * @param null $subheader
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $subheader is correct as it would always require null to be passed?
Loading history...
181
	 * @param null $aiIndex
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $aiIndex is correct as it would always require null to be passed?
Loading history...
182
	 * @return bool
183
	 * @throws Exception
184
	 */
185
	public function update(int $datasetId, $name, $subheader, $dimension1, $dimension2, $value, $aiIndex) {
186
		$dbUpdate = $this->DatasetMapper->update($datasetId, $name, $subheader, $dimension1, $dimension2, $value, $aiIndex);
187
188
		if ($aiIndex === 1) {
189
			$this->provider($datasetId);
190
		} else {
191
			$this->providerRemove($datasetId);
192
		}
193
		return $dbUpdate;
194
	}
195
196
	/**
197
	 * Export Dataset
198
	 *
199
	 * @param int $datasetId
200
	 * @return DataDownloadResponse
201
	 * @throws Exception
202
	 */
203
	public function export(int $datasetId) {
204
		$result = array();
205
		$result['dataset'] = $this->DatasetMapper->read($datasetId);
206
		$result['dataload'] = $this->DataloadMapper->read($datasetId);
207
		$result['threshold'] = $this->ThresholdService->read($datasetId);
208
		$result['favorite'] = '';
209
210
		if ($result['dataset']['type'] === DatasourceController::DATASET_TYPE_INTERNAL_DB) {
211
			$result['data'] = $this->StorageMapper->read($datasetId);
212
		}
213
214
		unset($result['dataset']['id'], $result['dataset']['user_id'], $result['dataset']['user_id'], $result['dataset']['parent']);
215
		$data = json_encode($result);
216
		return new DataDownloadResponse($data, $result['dataset']['name'] . '.export.txt', 'text/plain; charset=utf-8');
217
	}
218
219
	/**
220
	 * Update the context chat provider
221
	 *
222
	 * @NoAdminRequired
223
	 * @param int $datasetId
224
	 * @return bool
225
	 */
226
	public function provider(int $datasetId) {
227
		if (class_exists('OCA\ContextChat\Public\ContentManager')) {
228
			$this->contextChatManager = \OC::$server->query('OCA\Analytics\ContextChat\ContextChatManager');
229
			$this->contextChatManager->submitContent($datasetId);
230
		}
231
		return true;
232
	}
233
234
	/**
235
	 * Remove dataset from context chat
236
	 *
237
	 * @NoAdminRequired
238
	 * @param int $datasetId
239
	 * @return bool
240
	 */
241
	private function providerRemove(int $datasetId) {
242
		if (class_exists('OCA\ContextChat\Public\ContentManager')) {
243
			$this->contextChatManager = \OC::$server->query('OCA\Analytics\ContextChat\ContextChatManager');
244
			$this->contextChatManager->removeContentByDataset($datasetId);
245
		}
246
		return true;
247
	}
248
249
	/**
250
	 * Remove user from context chat
251
	 *
252
	 * @NoAdminRequired
253
	 * @param string $userId
254
	 * @return bool
255
	 */
256
	private function providerRemoveByUser(string $userId) {
257
		if (class_exists('OCA\ContextChat\Public\ContentManager')) {
258
			$this->contextChatManager = \OC::$server->query('OCA\Analytics\ContextChat\ContextChatManager');
259
			$this->contextChatManager->removeContentByUser($userId);
260
		}
261
		return true;
262
	}
263
264
	/**
265
	 * Delete Dataset and all depending objects
266
	 *
267
	 * @param int $datasetId
268
	 * @return bool
269
	 * @throws Exception
270
	 */
271
	public function delete(int $datasetId) {
272
		$this->ActivityManager->triggerEvent($datasetId, ActivityManager::OBJECT_DATASET, ActivityManager::SUBJECT_DATASET_DELETE);
273
		$this->DatasetMapper->delete($datasetId);
274
		$this->DataloadMapper->deleteByDataset($datasetId);
275
		$this->StorageMapper->deleteByDataset($datasetId);
276
		$this->providerRemove($datasetId);
277
		return true;
278
	}
279
280
	/**
281
	 * get dataset by user
282
	 *
283
	 * @param string $userId
284
	 * @return bool
285
	 * @throws Exception
286
	 */
287
	public function deleteByUser(string $userId) {
288
		$datasets = $this->DatasetMapper->indexByUser($userId);
289
		foreach ($datasets as $dataset) {
290
			$this->DatasetMapper->delete($dataset['id']);
291
			$this->DataloadMapper->deleteByDataset($dataset['id']);
292
			$this->StorageMapper->deleteByDataset($dataset['id']);
293
		}
294
		$this->providerRemoveByUser($userId);
295
		return true;
296
	}
297
298
}