| Total Complexity | 48 |
| Total Lines | 311 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like DatasourceController 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 DatasourceController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class DatasourceController extends Controller |
||
| 29 | { |
||
| 30 | private $logger; |
||
| 31 | private $GithubService; |
||
| 32 | private $FileService; |
||
| 33 | private $ExternalFileService; |
||
| 34 | private $RegexService; |
||
| 35 | private $JsonService; |
||
| 36 | private $ExcelService; |
||
| 37 | /** @var IEventDispatcher */ |
||
| 38 | private $dispatcher; |
||
| 39 | private $l10n; |
||
| 40 | |||
| 41 | const DATASET_TYPE_GROUP = 0; |
||
| 42 | const DATASET_TYPE_FILE = 1; |
||
| 43 | const DATASET_TYPE_INTERNAL_DB = 2; |
||
| 44 | const DATASET_TYPE_GIT = 3; |
||
| 45 | const DATASET_TYPE_EXTERNAL_FILE = 4; |
||
| 46 | const DATASET_TYPE_REGEX = 5; |
||
| 47 | const DATASET_TYPE_JSON = 6; |
||
| 48 | const DATASET_TYPE_EXCEL = 7; |
||
| 49 | |||
| 50 | public function __construct( |
||
| 51 | string $AppName, |
||
| 52 | IRequest $request, |
||
| 53 | LoggerInterface $logger, |
||
| 54 | Github $GithubService, |
||
| 55 | File $FileService, |
||
| 56 | Regex $RegexService, |
||
| 57 | Json $JsonService, |
||
| 58 | ExternalFile $ExternalFileService, |
||
| 59 | Excel $ExcelService, |
||
| 60 | IL10N $l10n, |
||
| 61 | IEventDispatcher $dispatcher |
||
| 62 | ) |
||
| 63 | { |
||
| 64 | parent::__construct($AppName, $request); |
||
| 65 | $this->logger = $logger; |
||
| 66 | $this->ExternalFileService = $ExternalFileService; |
||
| 67 | $this->GithubService = $GithubService; |
||
| 68 | $this->RegexService = $RegexService; |
||
| 69 | $this->FileService = $FileService; |
||
| 70 | $this->JsonService = $JsonService; |
||
| 71 | $this->ExcelService = $ExcelService; |
||
| 72 | $this->dispatcher = $dispatcher; |
||
| 73 | $this->l10n = $l10n; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * get all data source ids + names |
||
| 78 | * |
||
| 79 | * @NoAdminRequired |
||
| 80 | */ |
||
| 81 | public function index() |
||
| 82 | { |
||
| 83 | $datasources = array(); |
||
| 84 | $result = array(); |
||
| 85 | foreach ($this->getDatasources() as $key => $class) { |
||
| 86 | $datasources[$key] = $class->getName(); |
||
| 87 | } |
||
| 88 | $result['datasources'] = $datasources; |
||
| 89 | |||
| 90 | $options = array(); |
||
| 91 | foreach ($this->getDatasources() as $key => $class) { |
||
| 92 | $options[$key] = $class->getTemplate(); |
||
| 93 | } |
||
| 94 | $result['options'] = $options; |
||
| 95 | |||
| 96 | return $result; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * get all data source templates |
||
| 101 | * |
||
| 102 | * @NoAdminRequired |
||
| 103 | * @return array |
||
| 104 | */ |
||
| 105 | public function getTemplates() |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Get the data from a datasource; |
||
| 116 | * |
||
| 117 | * @NoAdminRequired |
||
| 118 | * @param int $datasourceId |
||
| 119 | * @param $datasetMetadata |
||
| 120 | * @return array|NotFoundException |
||
| 121 | */ |
||
| 122 | public function read(int $datasourceId, $datasetMetadata) |
||
| 123 | { |
||
| 124 | if (!$this->getDatasources()[$datasourceId]) { |
||
| 125 | $result['error'] = $this->l10n->t('Data source not available anymore'); |
||
|
|
|||
| 126 | return $result; |
||
| 127 | } |
||
| 128 | |||
| 129 | $option = array(); |
||
| 130 | // before 3.1.0, the options were in another format. as of 3.1.0 the standard option array is used |
||
| 131 | if ($datasetMetadata['link'][0] !== '{') { |
||
| 132 | $option['link'] = $datasetMetadata['link']; |
||
| 133 | } else { |
||
| 134 | $option = json_decode($datasetMetadata['link'], true); |
||
| 135 | } |
||
| 136 | $option['user_id'] = $datasetMetadata['user_id']; |
||
| 137 | |||
| 138 | try { |
||
| 139 | // read the data from the source |
||
| 140 | $result = $this->getDatasources()[$datasourceId]->readData($option); |
||
| 141 | |||
| 142 | // if data source should be timestamped/snapshoted |
||
| 143 | if (isset($option['timestamp']) and $option['timestamp'] === 'true') { |
||
| 144 | $result['data'] = array_map(function ($tag) { |
||
| 145 | $columns = count($tag); |
||
| 146 | return array($tag[$columns - 2], $tag[$columns - 2], $tag[$columns - 1]); |
||
| 147 | }, $result['data']); |
||
| 148 | date_default_timezone_set('UTC'); |
||
| 149 | // shift values by one dimension and stores date in second column |
||
| 150 | $result['data'] = $this->replaceDimension($result['data'], 1, date("Y-m-d H:i:s") . 'Z'); |
||
| 151 | } |
||
| 152 | |||
| 153 | if (isset($datasetMetadata['filteroptions']) && strlen($datasetMetadata['filteroptions']) >> 2) { |
||
| 154 | // filter data |
||
| 155 | $result = $this->filterData($result, $datasetMetadata['filteroptions']); |
||
| 156 | // remove columns and aggregate data |
||
| 157 | $result = $this->aggregateData($result, $datasetMetadata['filteroptions']); |
||
| 158 | } |
||
| 159 | |||
| 160 | |||
| 161 | } catch (\Error $e) { |
||
| 162 | $result['error'] = $e->getMessage(); |
||
| 163 | } |
||
| 164 | |||
| 165 | if (empty($result['data'])) { |
||
| 166 | $result['status'] = 'nodata'; |
||
| 167 | } |
||
| 168 | return $result; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * combine internal and registered datasources |
||
| 173 | * @return array |
||
| 174 | */ |
||
| 175 | private function getDatasources() |
||
| 176 | { |
||
| 177 | return $this->getOwnDatasources() + $this->getRegisteredDatasources(); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * map all internal data sources to their IDs |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | private function getOwnDatasources() |
||
| 185 | { |
||
| 186 | $dataSources = []; |
||
| 187 | $dataSources[self::DATASET_TYPE_FILE] = $this->FileService; |
||
| 188 | $dataSources[self::DATASET_TYPE_EXCEL] = $this->ExcelService; |
||
| 189 | $dataSources[self::DATASET_TYPE_GIT] = $this->GithubService; |
||
| 190 | $dataSources[self::DATASET_TYPE_EXTERNAL_FILE] = $this->ExternalFileService; |
||
| 191 | $dataSources[self::DATASET_TYPE_REGEX] = $this->RegexService; |
||
| 192 | $dataSources[self::DATASET_TYPE_JSON] = $this->JsonService; |
||
| 193 | return $dataSources; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * map all registered data sources to their IDs |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | private function getRegisteredDatasources() |
||
| 201 | { |
||
| 202 | $dataSources = []; |
||
| 203 | $event = new DatasourceEvent(); |
||
| 204 | $this->dispatcher->dispatchTyped($event); |
||
| 205 | |||
| 206 | foreach ($event->getDataSources() as $class) { |
||
| 207 | try { |
||
| 208 | $uniqueId = '99' . \OC::$server->get($class)->getId(); |
||
| 209 | |||
| 210 | if (isset($dataSources[$uniqueId])) { |
||
| 211 | $this->logger->error(new \InvalidArgumentException('Data source with the same ID already registered: ' . \OC::$server->get($class)->getName())); |
||
| 212 | continue; |
||
| 213 | } |
||
| 214 | $dataSources[$uniqueId] = \OC::$server->get($class); |
||
| 215 | } catch (\Error $e) { |
||
| 216 | $this->logger->error('Can not initialize data source: ' . json_encode($class)); |
||
| 217 | $this->logger->error($e->getMessage()); |
||
| 218 | } |
||
| 219 | } |
||
| 220 | return $dataSources; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * replace all values of one dimension |
||
| 225 | * |
||
| 226 | * @NoAdminRequired |
||
| 227 | * @param $Array |
||
| 228 | * @param $Find |
||
| 229 | * @param $Replace |
||
| 230 | * @return array |
||
| 231 | */ |
||
| 232 | private function replaceDimension($Array, $Find, $Replace) |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * apply the fiven filters to the hole result set |
||
| 250 | * |
||
| 251 | * @NoAdminRequired |
||
| 252 | * @param $data |
||
| 253 | * @param $filter |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | private function filterData($data, $filter) |
||
| 257 | { |
||
| 258 | $options = json_decode($filter, true); |
||
| 259 | if (isset($options['filter'])) { |
||
| 260 | foreach ($options['filter'] as $key => $value) { |
||
| 261 | $filterValue = $value['value']; |
||
| 262 | $filterOption = $value['option']; |
||
| 263 | $filtered = array(); |
||
| 264 | |||
| 265 | foreach ($data['data'] as $record) { |
||
| 266 | if ( |
||
| 267 | ($filterOption === 'EQ' && $record[$key] === $filterValue) |
||
| 268 | || ($filterOption === 'GT' && $record[$key] > $filterValue) |
||
| 269 | || ($filterOption === 'LT' && $record[$key] < $filterValue) |
||
| 270 | || ($filterOption === 'LIKE' && strpos($record[$key], $filterValue) !== FALSE) |
||
| 271 | ) { |
||
| 272 | array_push($filtered, $record); |
||
| 273 | } else if ($filterOption === 'IN') { |
||
| 274 | $filterValues = explode(',', $filterValue); |
||
| 275 | if (in_array($record[$key], $filterValues)) { |
||
| 276 | array_push($filtered, $record); |
||
| 277 | } |
||
| 278 | } |
||
| 279 | } |
||
| 280 | $data['data'] = $filtered; |
||
| 281 | } |
||
| 282 | } |
||
| 283 | return $data; |
||
| 284 | } |
||
| 285 | |||
| 286 | private function aggregateData($data, $filter) |
||
| 339 | } |
||
| 340 | } |