Total Complexity | 44 |
Total Lines | 281 |
Duplicated Lines | 0 % |
Changes | 4 | ||
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 |
||
26 | class DatasourceController extends Controller { |
||
27 | private $logger; |
||
28 | private $GithubService; |
||
29 | private $ExternalCsvService; |
||
30 | private $RegexService; |
||
31 | private $ExternalJsonService; |
||
32 | private $LocalJsonService; |
||
33 | private $LocalCsvService; |
||
34 | private $LocalExcelService; |
||
35 | /** @var IEventDispatcher */ |
||
36 | private $dispatcher; |
||
37 | private $l10n; |
||
38 | |||
39 | const DATASET_TYPE_GROUP = 0; |
||
40 | const DATASET_TYPE_LOCAL_CSV = 1; |
||
41 | const DATASET_TYPE_INTERNAL_DB = 2; |
||
42 | const DATASET_TYPE_GIT = 3; |
||
43 | const DATASET_TYPE_EXTERNAL_CSV = 4; |
||
44 | const DATASET_TYPE_REGEX = 5; |
||
45 | const DATASET_TYPE_EXTERNAL_JSON = 6; |
||
46 | const DATASET_TYPE_LOCAL_EXCEL = 7; |
||
47 | const DATASET_TYPE_LOCAL_JSON = 8; |
||
48 | |||
49 | public function __construct( |
||
50 | string $appName, |
||
51 | IRequest $request, |
||
52 | LoggerInterface $logger, |
||
53 | Github $GithubService, |
||
54 | LocalCsv $LocalCsvService, |
||
55 | Regex $RegexService, |
||
56 | ExternalJson $ExternalJsonService, |
||
57 | LocalJson $LocalJsonService, |
||
58 | ExternalCsv $ExternalCsvService, |
||
59 | LocalExcel $LocalExcelService, |
||
60 | IL10N $l10n, |
||
61 | IEventDispatcher $dispatcher |
||
62 | ) { |
||
63 | parent::__construct($appName, $request); |
||
64 | $this->logger = $logger; |
||
65 | $this->ExternalCsvService = $ExternalCsvService; |
||
66 | $this->GithubService = $GithubService; |
||
67 | $this->RegexService = $RegexService; |
||
68 | $this->LocalCsvService = $LocalCsvService; |
||
69 | $this->ExternalJsonService = $ExternalJsonService; |
||
70 | $this->LocalJsonService = $LocalJsonService; |
||
71 | $this->LocalExcelService = $LocalExcelService; |
||
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 | $datasources = array(); |
||
83 | $result = array(); |
||
84 | foreach ($this->getDatasources() as $key => $class) { |
||
85 | $datasources[$key] = $class->getName(); |
||
86 | } |
||
87 | $result['datasources'] = $datasources; |
||
88 | |||
89 | $options = array(); |
||
90 | foreach ($this->getDatasources() as $key => $class) { |
||
91 | $options[$key] = $class->getTemplate(); |
||
92 | } |
||
93 | $result['options'] = $options; |
||
94 | |||
95 | return $result; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * get all data source templates |
||
100 | * |
||
101 | * @NoAdminRequired |
||
102 | * @return array |
||
103 | */ |
||
104 | public function getTemplates() { |
||
105 | $result = array(); |
||
106 | foreach ($this->getDatasources() as $key => $class) { |
||
107 | $result[$key] = $class->getTemplate(); |
||
108 | } |
||
109 | return $result; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Get the data from a datasource; |
||
114 | * |
||
115 | * @NoAdminRequired |
||
116 | * @param int $datasourceId |
||
117 | * @param $datasetMetadata |
||
118 | * @return array|NotFoundException |
||
119 | */ |
||
120 | public function read(int $datasourceId, $datasetMetadata) { |
||
121 | if (!$this->getDatasources()[$datasourceId]) { |
||
122 | $result['error'] = $this->l10n->t('Data source not available anymore'); |
||
123 | return $result; |
||
124 | } |
||
125 | |||
126 | $option = array(); |
||
127 | // before 3.1.0, the options were in another format. as of 3.1.0 the standard option array is used |
||
128 | if ($datasetMetadata['link'][0] !== '{') { |
||
129 | $option['link'] = $datasetMetadata['link']; |
||
130 | } else { |
||
131 | $option = json_decode($datasetMetadata['link'], true); |
||
132 | } |
||
133 | $option['user_id'] = $datasetMetadata['user_id']; |
||
134 | |||
135 | try { |
||
136 | // read the data from the source |
||
137 | $result = $this->getDatasources()[$datasourceId]->readData($option); |
||
138 | |||
139 | // if data source should be timestamped/snapshoted |
||
140 | if (isset($option['timestamp']) and $option['timestamp'] === 'true') { |
||
141 | date_default_timezone_set('UTC'); |
||
142 | $result['data'] = array_map(function ($tag) { |
||
143 | $columns = count($tag); |
||
144 | if ($columns > 1) { |
||
145 | // shift values by one dimension and stores date in second column |
||
146 | return array($tag[$columns - 2], date("Y-m-d H:i:s") . 'Z', $tag[$columns - 1]); |
||
147 | } else { |
||
148 | // just return 2 columns if the original data only has one column |
||
149 | return array(date("Y-m-d H:i:s") . 'Z', $tag[$columns - 1]); |
||
150 | } |
||
151 | }, $result['data']); |
||
152 | } |
||
153 | |||
154 | if (isset($datasetMetadata['filteroptions']) && strlen($datasetMetadata['filteroptions']) >> 2) { |
||
155 | // filter data |
||
156 | $result = $this->filterData($result, $datasetMetadata['filteroptions']); |
||
157 | // remove columns and aggregate data |
||
158 | $result = $this->aggregateData($result, $datasetMetadata['filteroptions']); |
||
159 | } |
||
160 | |||
161 | |||
162 | } catch (\Error $e) { |
||
163 | $result['error'] = $e->getMessage(); |
||
164 | } |
||
165 | |||
166 | if (empty($result['data'])) { |
||
167 | $result['status'] = 'nodata'; |
||
168 | } |
||
169 | return $result; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * combine internal and registered datasources |
||
174 | * @return array |
||
175 | */ |
||
176 | private function getDatasources() { |
||
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() { |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * map all registered data sources to their IDs |
||
199 | * @return array |
||
200 | */ |
||
201 | private function getRegisteredDatasources() { |
||
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) |
||
212 | ->getName())); |
||
213 | continue; |
||
214 | } |
||
215 | $dataSources[$uniqueId] = \OC::$server->get($class); |
||
216 | } catch (\Error $e) { |
||
217 | $this->logger->error('Can not initialize data source: ' . json_encode($class)); |
||
218 | $this->logger->error($e->getMessage()); |
||
219 | } |
||
220 | } |
||
221 | return $dataSources; |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * apply the fiven filters to the hole result set |
||
226 | * |
||
227 | * @NoAdminRequired |
||
228 | * @param $data |
||
229 | * @param $filter |
||
230 | * @return array |
||
231 | */ |
||
232 | private function filterData($data, $filter) { |
||
233 | $options = json_decode($filter, true); |
||
234 | if (isset($options['filter'])) { |
||
235 | foreach ($options['filter'] as $key => $value) { |
||
236 | $filterValue = $value['value']; |
||
237 | $filterOption = $value['option']; |
||
238 | $filtered = array(); |
||
239 | |||
240 | foreach ($data['data'] as $record) { |
||
241 | if (($filterOption === 'EQ' && $record[$key] === $filterValue) || ($filterOption === 'GT' && $record[$key] > $filterValue) || ($filterOption === 'LT' && $record[$key] < $filterValue) || ($filterOption === 'LIKE' && strpos($record[$key], $filterValue) !== false)) { |
||
242 | array_push($filtered, $record); |
||
243 | } else if ($filterOption === 'IN') { |
||
244 | $filterValues = explode(',', $filterValue); |
||
245 | if (in_array($record[$key], $filterValues)) { |
||
246 | array_push($filtered, $record); |
||
247 | } |
||
248 | } |
||
249 | } |
||
250 | $data['data'] = $filtered; |
||
251 | } |
||
252 | } |
||
253 | return $data; |
||
254 | } |
||
255 | |||
256 | private function aggregateData($data, $filter) { |
||
307 | } |
||
308 | } |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths