| Total Complexity | 43 |
| Total Lines | 409 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like DataloadController 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 DataloadController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class DataloadController extends Controller |
||
| 25 | { |
||
| 26 | private $logger; |
||
| 27 | private $StorageController; |
||
| 28 | private $DataSourceController; |
||
| 29 | private $userId; |
||
| 30 | private $ActivityManager; |
||
| 31 | private $DatasetController; |
||
| 32 | private $l10n; |
||
| 33 | private $DataloadMapper; |
||
| 34 | |||
| 35 | public function __construct( |
||
| 36 | string $AppName, |
||
| 37 | IRequest $request, |
||
| 38 | IL10N $l10n, |
||
| 39 | $userId, |
||
| 40 | ILogger $logger, |
||
| 41 | ActivityManager $ActivityManager, |
||
| 42 | DataSourceController $DataSourceController, |
||
| 43 | DatasetController $DatasetController, |
||
| 44 | StorageController $StorageController, |
||
| 45 | DataloadMapper $DataloadMapper |
||
| 46 | ) |
||
| 47 | { |
||
| 48 | parent::__construct($AppName, $request); |
||
| 49 | $this->l10n = $l10n; |
||
| 50 | $this->userId = $userId; |
||
| 51 | $this->logger = $logger; |
||
| 52 | $this->StorageController = $StorageController; |
||
| 53 | $this->ActivityManager = $ActivityManager; |
||
| 54 | $this->DataSourceController = $DataSourceController; |
||
| 55 | $this->DatasetController = $DatasetController; |
||
| 56 | $this->DataloadMapper = $DataloadMapper; |
||
| 57 | } |
||
| 58 | |||
| 59 | // Dataloads |
||
| 60 | // Dataloads |
||
| 61 | // Dataloads |
||
| 62 | |||
| 63 | /** |
||
| 64 | * create a new dataload |
||
| 65 | * |
||
| 66 | * @NoAdminRequired |
||
| 67 | * @param int $datasetId |
||
| 68 | * @param int $datasourceId |
||
| 69 | * @return DataResponse |
||
| 70 | */ |
||
| 71 | public function create(int $datasetId, int $datasourceId) |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * get all dataloads for a dataset |
||
| 78 | * |
||
| 79 | * @NoAdminRequired |
||
| 80 | * @param int $datasetId |
||
| 81 | * @return DataResponse |
||
| 82 | */ |
||
| 83 | public function read(int $datasetId) |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * update dataload |
||
| 92 | * |
||
| 93 | * @NoAdminRequired |
||
| 94 | * @param int $dataloadId |
||
| 95 | * @param $name |
||
| 96 | * @param $option |
||
| 97 | * @param $schedule |
||
| 98 | * @return DataResponse |
||
| 99 | */ |
||
| 100 | public function update(int $dataloadId, $name, $option, $schedule) |
||
| 101 | { |
||
| 102 | return new DataResponse($this->DataloadMapper->update($dataloadId, $name, $option, $schedule)); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * delete a dataload |
||
| 107 | * |
||
| 108 | * @NoAdminRequired |
||
| 109 | * @param int $dataloadId |
||
| 110 | * @return bool |
||
| 111 | */ |
||
| 112 | public function delete(int $dataloadId) |
||
| 113 | { |
||
| 114 | return $this->DataloadMapper->delete($dataloadId); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * simulate a dataload and output its data |
||
| 119 | * |
||
| 120 | * @NoAdminRequired |
||
| 121 | * @param int $dataloadId |
||
| 122 | * @return DataResponse |
||
| 123 | * @throws NotFoundException |
||
| 124 | */ |
||
| 125 | public function simulate(int $dataloadId) |
||
| 126 | { |
||
| 127 | $result = $this->getDataFromDatasource($dataloadId); |
||
| 128 | return new DataResponse($result); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * execute all dataloads depending on their schedule |
||
| 133 | * daily or hourly |
||
| 134 | * |
||
| 135 | * @NoAdminRequired |
||
| 136 | * @param $schedule |
||
| 137 | * @return void |
||
| 138 | * @throws \Exception |
||
| 139 | */ |
||
| 140 | public function executeBySchedule($schedule) |
||
| 141 | { |
||
| 142 | $schedules = $this->DataloadMapper->getDataloadBySchedule($schedule); |
||
| 143 | //$this->logger->debug('DataLoadController 143: execute schedule '.$schedule); |
||
| 144 | foreach ($schedules as $dataload) { |
||
| 145 | //$this->logger->debug('DataLoadController 145: execute dataload '.$dataload['id']); |
||
| 146 | $this->execute($dataload['id']); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * execute a dataload from datasource and store into dataset |
||
| 152 | * |
||
| 153 | * @NoAdminRequired |
||
| 154 | * @param int $dataloadId |
||
| 155 | * @return DataResponse |
||
| 156 | * @throws \Exception |
||
| 157 | */ |
||
| 158 | public function execute(int $dataloadId) |
||
| 159 | { |
||
| 160 | //$this->logger->debug('DataLoadController 143:'.$dataloadId); |
||
| 161 | $dataloadMetadata = $this->DataloadMapper->getDataloadById($dataloadId); |
||
| 162 | $result = $this->getDataFromDatasource($dataloadId); |
||
| 163 | $insert = $update = 0; |
||
| 164 | $datasetId = $result['datasetId']; |
||
| 165 | //$this->logger->debug('DataLoadController 146: loading into dataset '.$datasetId); |
||
| 166 | |||
| 167 | if ($result['error'] === 0) { |
||
| 168 | //$this->logger->debug('DataLoadController 149: OK'); |
||
| 169 | foreach ($result['data'] as &$row) { |
||
| 170 | $action = $this->StorageController->update($datasetId, $row['dimension1'], $row['dimension2'], $row['dimension3'], $dataloadMetadata['user_id']); |
||
| 171 | $insert = $insert + $action['insert']; |
||
| 172 | $update = $update + $action['update']; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | $result = [ |
||
| 177 | 'insert' => $insert, |
||
| 178 | 'update' => $update, |
||
| 179 | 'error' => $result['error'], |
||
| 180 | ]; |
||
| 181 | |||
| 182 | if ($result['error'] === 0) $this->ActivityManager->triggerEvent($datasetId, ActivityManager::OBJECT_DATA, ActivityManager::SUBJECT_DATA_ADD_DATALOAD, $dataloadMetadata['user_id']); |
||
| 183 | |||
| 184 | return new DataResponse($result); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * get the data from datasource |
||
| 189 | * to be used in simulation or execution |
||
| 190 | * |
||
| 191 | * @NoAdminRequired |
||
| 192 | * @param int $dataloadId |
||
| 193 | * @return array |
||
| 194 | * @throws NotFoundException |
||
| 195 | */ |
||
| 196 | private function getDataFromDatasource(int $dataloadId) |
||
| 197 | { |
||
| 198 | $dataloadMetadata = $this->DataloadMapper->getDataloadById($dataloadId); |
||
| 199 | $datasetMetadata = $this->DatasetController->getOwnDataset($dataloadMetadata['dataset'], $dataloadMetadata['user_id']); |
||
| 200 | |||
| 201 | if (!empty($datasetMetadata)) { |
||
| 202 | $option = json_decode($dataloadMetadata['option'], true); |
||
| 203 | $option['user_id'] = $dataloadMetadata['user_id']; |
||
| 204 | |||
| 205 | //$this->logger->debug('DataLoadController 187: ' . $dataloadMetadata['option'] . '---' . json_encode($option)); |
||
| 206 | $result = $this->DataSourceController->read((int)$dataloadMetadata['datasource'], $option); |
||
| 207 | $result['datasetId'] = $dataloadMetadata['dataset']; |
||
| 208 | |||
| 209 | if (isset($option['timestamp']) and $option['timestamp'] === 'true') { |
||
| 210 | // if datasource should be timestamped/snapshoted |
||
| 211 | // shift values by one dimension |
||
| 212 | $result['data'] = array_map(function ($tag) { |
||
| 213 | return array( |
||
| 214 | 'dimension1' => $tag['dimension2'], |
||
| 215 | 'dimension2' => $tag['dimension2'], |
||
| 216 | 'dimension3' => $tag['dimension3'] |
||
| 217 | ); |
||
| 218 | }, $result['data']); |
||
| 219 | $result['data'] = $this->replaceDimension($result['data'], 'dimension2', date("Y-m-d H:i:s")); |
||
| 220 | } |
||
| 221 | |||
| 222 | return $result; |
||
| 223 | } else { |
||
| 224 | return new NotFoundResponse(); |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * replace all values of one dimension |
||
| 230 | * |
||
| 231 | * @NoAdminRequired |
||
| 232 | * @param $Array |
||
| 233 | * @param $Find |
||
| 234 | * @param $Replace |
||
| 235 | * @return array |
||
| 236 | */ |
||
| 237 | private function replaceDimension($Array, $Find, $Replace) |
||
| 251 | } |
||
| 252 | |||
| 253 | // Data Manipulation |
||
| 254 | // Data Manipulation |
||
| 255 | // Data Manipulation |
||
| 256 | |||
| 257 | /** |
||
| 258 | * update data from input form |
||
| 259 | * |
||
| 260 | * @NoAdminRequired |
||
| 261 | * @param int $datasetId |
||
| 262 | * @param $dimension1 |
||
| 263 | * @param $dimension2 |
||
| 264 | * @param $dimension3 |
||
| 265 | * @return DataResponse|NotFoundResponse |
||
| 266 | * @throws \Exception |
||
| 267 | */ |
||
| 268 | public function updateData(int $datasetId, $dimension1, $dimension2, $dimension3) |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * update data from input form |
||
| 300 | * |
||
| 301 | * @NoAdminRequired |
||
| 302 | * @param int $datasetId |
||
| 303 | * @param $dimension1 |
||
| 304 | * @param $dimension2 |
||
| 305 | * @return DataResponse|NotFoundResponse |
||
| 306 | */ |
||
| 307 | public function deleteData(int $datasetId, $dimension1, $dimension2) |
||
| 308 | { |
||
| 309 | $datasetMetadata = $this->DatasetController->getOwnDataset($datasetId); |
||
| 310 | if (!empty($datasetMetadata)) { |
||
| 311 | $result = $this->StorageController->delete($datasetId, $dimension1, $dimension2); |
||
| 312 | return new DataResponse($result); |
||
| 313 | } else { |
||
| 314 | return new NotFoundResponse(); |
||
| 315 | } |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Import clipboard data |
||
| 320 | * |
||
| 321 | * @NoAdminRequired |
||
| 322 | * @param int $datasetId |
||
| 323 | * @param $import |
||
| 324 | * @return DataResponse|NotFoundResponse |
||
| 325 | * @throws \Exception |
||
| 326 | */ |
||
| 327 | public function importClipboard($datasetId, $import) |
||
| 363 | } |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Import data into dataset from an internal or external file |
||
| 368 | * |
||
| 369 | * @NoAdminRequired |
||
| 370 | * @param int $datasetId |
||
| 371 | * @param $path |
||
| 372 | * @return DataResponse|NotFoundResponse |
||
| 373 | * @throws NotFoundException |
||
| 374 | * @throws \Exception |
||
| 375 | */ |
||
| 376 | public function importFile(int $datasetId, $path) |
||
| 377 | { |
||
| 378 | //$this->logger->error('DataLoadController 100:'.$datasetId. $path); |
||
| 379 | $datasetMetadata = $this->DatasetController->getOwnDataset($datasetId); |
||
| 380 | if (!empty($datasetMetadata)) { |
||
| 381 | $insert = $update = 0; |
||
| 382 | $option['user_id'] = $datasetMetadata['user_id']; |
||
| 383 | $option['path'] = $path; |
||
| 384 | $option['link'] = $datasetMetadata['link']; |
||
| 385 | $result = $this->DataSourceController->read(DataSourceController::DATASET_TYPE_INTERNAL_FILE, $option); |
||
| 386 | |||
| 387 | if ($result['error'] === 0) { |
||
| 388 | foreach ($result['data'] as &$row) { |
||
| 389 | $action = $this->StorageController->update($datasetId, $row['dimension1'], $row['dimension2'], $row['dimension3']); |
||
| 390 | $insert = $insert + $action['insert']; |
||
| 391 | $update = $update + $action['update']; |
||
| 392 | } |
||
| 393 | } |
||
| 394 | |||
| 395 | $result = [ |
||
| 396 | 'insert' => $insert, |
||
| 397 | 'update' => $update, |
||
| 398 | 'error' => $result['error'], |
||
| 399 | ]; |
||
| 400 | |||
| 401 | if ($result['error'] === 0) $this->ActivityManager->triggerEvent($datasetId, ActivityManager::OBJECT_DATA, ActivityManager::SUBJECT_DATA_ADD_IMPORT); |
||
| 402 | return new DataResponse($result); |
||
| 403 | } else { |
||
| 404 | return new NotFoundResponse(); |
||
| 405 | } |
||
| 406 | } |
||
| 407 | |||
| 408 | private function detectDelimiter($data) |
||
| 409 | { |
||
| 410 | $delimiters = ["\t", ";", "|", ","]; |
||
| 411 | $data_2 = null; |
||
| 412 | $delimiter = $delimiters[0]; |
||
| 413 | foreach ($delimiters as $d) { |
||
| 414 | $firstRow = str_getcsv($data, "\n")[0]; |
||
| 415 | $data_1 = str_getcsv($firstRow, $d); |
||
| 416 | if (sizeof($data_1) > sizeof($data_2)) { |
||
| 417 | $delimiter = $d; |
||
| 418 | $data_2 = $data_1; |
||
| 419 | } |
||
| 420 | } |
||
| 421 | return $delimiter; |
||
| 422 | } |
||
| 423 | |||
| 424 | private function floatvalue($val) |
||
| 433 | } |
||
| 434 | } |
||
| 435 | } |