| Total Complexity | 47 |
| Total Lines | 270 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 0 |
Complex classes like DatasetService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DatasetService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class DatasetService |
||
| 26 | { |
||
| 27 | private $userId; |
||
| 28 | private $logger; |
||
| 29 | private $tagManager; |
||
| 30 | private $ShareService; |
||
| 31 | private $StorageMapper; |
||
| 32 | private $DatasetMapper; |
||
| 33 | private $ThresholdService; |
||
| 34 | private $DataloadMapper; |
||
| 35 | private $ActivityManager; |
||
| 36 | private $rootFolder; |
||
| 37 | private $VariableService; |
||
| 38 | |||
| 39 | public function __construct( |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * get all datasets |
||
| 68 | * |
||
| 69 | * @return DataResponse |
||
| 70 | */ |
||
| 71 | public function index() |
||
| 72 | { |
||
| 73 | $ownDatasets = $this->DatasetMapper->index(); |
||
| 74 | |||
| 75 | // get dataload indicators for icons shown in the advanced screen |
||
| 76 | $dataloads = $this->DataloadMapper->getAllDataloadMetadata(); |
||
| 77 | foreach ($dataloads as $dataload) { |
||
| 78 | $key = array_search($dataload['dataset'], array_column($ownDatasets, 'id')); |
||
| 79 | if ($key !== '') { |
||
| 80 | if ($dataload['schedules'] !== '' and $dataload['schedules'] !== null) { |
||
| 81 | $dataload['schedules'] = 1; |
||
| 82 | } else { |
||
| 83 | $dataload['schedules'] = 0; |
||
| 84 | } |
||
| 85 | $ownDatasets[$key]['dataloads'] = $dataload['dataloads']; |
||
| 86 | $ownDatasets[$key]['schedules'] = $dataload['schedules']; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | foreach ($ownDatasets as &$ownDataset) { |
||
| 91 | $ownDataset = $this->VariableService->replaceTextVariables($ownDataset); |
||
| 92 | } |
||
| 93 | |||
| 94 | return new DataResponse($ownDatasets); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * get own dataset details |
||
| 99 | * |
||
| 100 | * @param int $datasetId |
||
| 101 | * @return array |
||
| 102 | */ |
||
| 103 | public function read(int $datasetId) |
||
| 104 | { |
||
| 105 | $ownDataset = $this->DatasetMapper->read($datasetId); |
||
| 106 | if (!empty($ownDataset)) { |
||
| 107 | $ownDataset['permissions'] = \OCP\Constants::PERMISSION_UPDATE; |
||
| 108 | } |
||
| 109 | return $ownDataset; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * get own dataset details |
||
| 114 | * |
||
| 115 | * @param int $datasetId |
||
| 116 | * @param string|null $user_id |
||
| 117 | * @return array |
||
| 118 | */ |
||
| 119 | public function getOwnDataset(int $datasetId, string $user_id = null) |
||
| 120 | { |
||
| 121 | $ownDataset = $this->DatasetMapper->read($datasetId, $user_id); |
||
| 122 | if (!empty($ownDataset)) { |
||
| 123 | $ownDataset['permissions'] = \OCP\Constants::PERMISSION_UPDATE; |
||
| 124 | $ownDataset = $this->VariableService->replaceTextVariables($ownDataset); |
||
| 125 | } |
||
| 126 | return $ownDataset; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * create new dataset |
||
| 131 | * |
||
| 132 | * @param $name |
||
| 133 | * @param $dimension1 |
||
| 134 | * @param $dimension2 |
||
| 135 | * @param $value |
||
| 136 | * @return int |
||
| 137 | * @throws \OCP\DB\Exception |
||
| 138 | */ |
||
| 139 | public function create($name, $dimension1, $dimension2, $value) |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * get dataset details |
||
| 147 | * |
||
| 148 | * @param int $datasetId |
||
| 149 | * @param $name |
||
| 150 | * @param $subheader |
||
| 151 | * @param int $parent |
||
| 152 | * @param int $type |
||
| 153 | * @param $link |
||
| 154 | * @param $visualization |
||
| 155 | * @param $chart |
||
| 156 | * @param $chartoptions |
||
| 157 | * @param $dataoptions |
||
| 158 | * @param $dimension1 |
||
| 159 | * @param $dimension2 |
||
| 160 | * @param $value |
||
| 161 | * @return bool |
||
| 162 | */ |
||
| 163 | public function update(int $datasetId, $name, $subheader, int $parent, int $type, $link, $visualization, $chart, $chartoptions, $dataoptions, $dimension1 = null, $dimension2 = null, $value = null) |
||
| 164 | { |
||
| 165 | if ($type === DatasourceController::DATASET_TYPE_GROUP) { |
||
| 166 | $parent = 0; |
||
| 167 | } |
||
| 168 | return $this->DatasetMapper->update($datasetId, $name, $subheader, $parent, $type, $link, $visualization, $chart, $chartoptions, $dataoptions, $dimension1, $dimension2, $value); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Import Dataset from File |
||
| 173 | * |
||
| 174 | * @param string|null $path |
||
| 175 | * @param string|null $raw |
||
| 176 | * @return int |
||
| 177 | * @throws \OCP\Files\NotFoundException |
||
| 178 | * @throws \OCP\Files\NotPermittedException |
||
| 179 | */ |
||
| 180 | public function import(string $path = null, string $raw = null) |
||
| 181 | { |
||
| 182 | if ($path !== '') { |
||
| 183 | $file = $this->rootFolder->getUserFolder($this->userId)->get($path); |
||
| 184 | $data = $file->getContent(); |
||
|
|
|||
| 185 | } else if ($raw !== null) { |
||
| 186 | $data = $raw; |
||
| 187 | } else { |
||
| 188 | return false; |
||
| 189 | } |
||
| 190 | $data = json_decode($data, true); |
||
| 191 | |||
| 192 | $dataset = $data['dataset']; |
||
| 193 | isset($dataset['name']) ? $name = $dataset['name'] : $name = ''; |
||
| 194 | isset($dataset['subheader']) ? $subheader = $dataset['subheader'] : $subheader = ''; |
||
| 195 | $parent = 0; |
||
| 196 | isset($dataset['type']) ? $type = $dataset['type'] : $type = null; |
||
| 197 | isset($dataset['link']) ? $link = $dataset['link'] : $link = null; |
||
| 198 | isset($dataset['visualization']) ? $visualization = $dataset['visualization'] : $visualization = null; |
||
| 199 | isset($dataset['chart']) ? $chart = $dataset['chart'] : $chart = null; |
||
| 200 | isset($dataset['chartoptions']) ? $chartoptions = $dataset['chartoptions'] : $chartoptions = null; |
||
| 201 | isset($dataset['dataoptions']) ? $dataoptions = $dataset['dataoptions'] : $dataoptions = null; |
||
| 202 | isset($dataset['filteroptions']) ? $filteroptions = $dataset['filteroptions'] : $filteroptions = null; |
||
| 203 | isset($dataset['dimension1']) ? $dimension1 = $dataset['dimension1'] : $dimension1 = null; |
||
| 204 | isset($dataset['dimension2']) ? $dimension2 = $dataset['dimension2'] : $dimension2 = null; |
||
| 205 | isset($dataset['value']) ? $value = $dataset['value'] : $value = null; |
||
| 206 | |||
| 207 | $datasetId = $this->DatasetMapper->create(); |
||
| 208 | $this->DatasetMapper->update($datasetId, $name, $subheader, $parent, $type, $link, $visualization, $chart, $chartoptions, $dataoptions, $dimension1, $dimension2, $value, $filteroptions); |
||
| 209 | |||
| 210 | foreach ($data['dataload'] as $dataload) { |
||
| 211 | isset($dataload['datasource']) ? $datasource = $dataload['datasource'] : $datasource = null; |
||
| 212 | isset($dataload['name']) ? $name = $dataload['name'] : $name = null; |
||
| 213 | isset($dataload['option']) ? $option = $dataload['option'] : $option = null; |
||
| 214 | $schedule = null; |
||
| 215 | |||
| 216 | $dataloadId = $this->DataloadMapper->create($datasetId, $datasource); |
||
| 217 | $this->DataloadMapper->update($dataloadId, $name, $option, $schedule); |
||
| 218 | } |
||
| 219 | |||
| 220 | foreach ($data['threshold'] as $threshold) { |
||
| 221 | isset($threshold['dimension1']) ? $dimension1 = $threshold['dimension1'] : $dimension1 = null; |
||
| 222 | isset($threshold['value']) ? $value = $threshold['value'] : $value = null; |
||
| 223 | isset($threshold['option']) ? $option = $threshold['option'] : $option = null; |
||
| 224 | isset($threshold['severity']) ? $severity = $threshold['severity'] : $severity = null; |
||
| 225 | $this->ThresholdService->create($datasetId, $dimension1, $option, $value, $severity); |
||
| 226 | } |
||
| 227 | |||
| 228 | foreach ($data['data'] as $dData) { |
||
| 229 | isset($dData[0]) ? $dimension1 = $dData[0] : $dimension1 = null; |
||
| 230 | isset($dData[1]) ? $dimension2 = $dData[1] : $dimension2 = null; |
||
| 231 | isset($dData[2]) ? $value = $dData[2] : $value = null; |
||
| 232 | $this->StorageMapper->create($datasetId, $dimension1, $dimension2, $value); |
||
| 233 | } |
||
| 234 | |||
| 235 | if (isset($data['favorite'])) { |
||
| 236 | $this->setFavorite($datasetId, $data['favorite']); |
||
| 237 | } |
||
| 238 | |||
| 239 | return $datasetId; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Export Dataset |
||
| 244 | * |
||
| 245 | * @param int $datasetId |
||
| 246 | * @return DataDownloadResponse |
||
| 247 | */ |
||
| 248 | public function export(int $datasetId) |
||
| 249 | { |
||
| 250 | $result = array(); |
||
| 251 | $result['dataset'] = $this->DatasetMapper->read($datasetId); |
||
| 252 | $result['dataload'] = $this->DataloadMapper->read($datasetId); |
||
| 253 | $result['threshold'] = $this->ThresholdService->read($datasetId); |
||
| 254 | $result['favorite'] = ''; |
||
| 255 | |||
| 256 | if ($result['dataset']['type'] === DatasourceController::DATASET_TYPE_INTERNAL_DB) { |
||
| 257 | $result['data'] = $this->StorageMapper->read($datasetId); |
||
| 258 | } |
||
| 259 | |||
| 260 | unset($result['dataset']['id'], $result['dataset']['user_id'], $result['dataset']['user_id'], $result['dataset']['parent']); |
||
| 261 | $data = json_encode($result); |
||
| 262 | return new DataDownloadResponse($data, $result['dataset']['name'] . '.export.txt', 'text/plain; charset=utf-8'); |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Delete Dataset and all depending objects |
||
| 267 | * |
||
| 268 | * @param int $datasetId |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | public function delete(int $datasetId) |
||
| 272 | { |
||
| 273 | $this->ShareService->deleteShareByReport($datasetId); |
||
| 274 | $this->StorageMapper->deleteByDataset($datasetId); |
||
| 275 | $this->DatasetMapper->delete($datasetId); |
||
| 276 | $this->ThresholdService->deleteThresholdByReport($datasetId); |
||
| 277 | $this->DataloadMapper->deleteDataloadByDataset($datasetId); |
||
| 278 | $this->ActivityManager->triggerEvent(0, ActivityManager::OBJECT_DATASET, ActivityManager::SUBJECT_DATASET_DELETE); |
||
| 279 | $this->setFavorite($datasetId, 'false'); |
||
| 280 | return true; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * get dataset details |
||
| 285 | * |
||
| 286 | * @param int $datasetId |
||
| 287 | * @param $chartoptions |
||
| 288 | * @param $dataoptions |
||
| 289 | * @param $filteroptions |
||
| 290 | * @return bool |
||
| 291 | */ |
||
| 292 | public function updateOptions(int $datasetId, $chartoptions, $dataoptions, $filteroptions) |
||
| 295 | } |
||
| 296 | } |