Total Complexity | 44 |
Total Lines | 288 |
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 |
||
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 | date_default_timezone_set('UTC'); |
||
145 | $result['data'] = array_map(function ($tag) { |
||
146 | $columns = count($tag); |
||
147 | if ($columns > 1) { |
||
148 | // shift values by one dimension and stores date in second column |
||
149 | return array($tag[$columns - 2], date("Y-m-d H:i:s") . 'Z', $tag[$columns - 1]); |
||
150 | } else { |
||
151 | // just return 2 columns if the original data only has one column |
||
152 | return array(date("Y-m-d H:i:s") . 'Z', $tag[$columns - 1]); |
||
153 | }}, $result['data']); |
||
154 | } |
||
155 | |||
156 | if (isset($datasetMetadata['filteroptions']) && strlen($datasetMetadata['filteroptions']) >> 2) { |
||
157 | // filter data |
||
158 | $result = $this->filterData($result, $datasetMetadata['filteroptions']); |
||
159 | // remove columns and aggregate data |
||
160 | $result = $this->aggregateData($result, $datasetMetadata['filteroptions']); |
||
161 | } |
||
162 | |||
163 | |||
164 | } catch (\Error $e) { |
||
165 | $result['error'] = $e->getMessage(); |
||
166 | } |
||
167 | |||
168 | if (empty($result['data'])) { |
||
169 | $result['status'] = 'nodata'; |
||
170 | } |
||
171 | return $result; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * combine internal and registered datasources |
||
176 | * @return array |
||
177 | */ |
||
178 | private function getDatasources() |
||
179 | { |
||
180 | return $this->getOwnDatasources() + $this->getRegisteredDatasources(); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * map all internal data sources to their IDs |
||
185 | * @return array |
||
186 | */ |
||
187 | private function getOwnDatasources() |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * map all registered data sources to their IDs |
||
201 | * @return array |
||
202 | */ |
||
203 | private function getRegisteredDatasources() |
||
204 | { |
||
205 | $dataSources = []; |
||
206 | $event = new DatasourceEvent(); |
||
207 | $this->dispatcher->dispatchTyped($event); |
||
208 | |||
209 | foreach ($event->getDataSources() as $class) { |
||
210 | try { |
||
211 | $uniqueId = '99' . \OC::$server->get($class)->getId(); |
||
212 | |||
213 | if (isset($dataSources[$uniqueId])) { |
||
214 | $this->logger->error(new \InvalidArgumentException('Data source with the same ID already registered: ' . \OC::$server->get($class)->getName())); |
||
215 | continue; |
||
216 | } |
||
217 | $dataSources[$uniqueId] = \OC::$server->get($class); |
||
218 | } catch (\Error $e) { |
||
219 | $this->logger->error('Can not initialize data source: ' . json_encode($class)); |
||
220 | $this->logger->error($e->getMessage()); |
||
221 | } |
||
222 | } |
||
223 | return $dataSources; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * apply the fiven filters to the hole result set |
||
228 | * |
||
229 | * @NoAdminRequired |
||
230 | * @param $data |
||
231 | * @param $filter |
||
232 | * @return array |
||
233 | */ |
||
234 | private function filterData($data, $filter) |
||
235 | { |
||
236 | $options = json_decode($filter, true); |
||
237 | if (isset($options['filter'])) { |
||
238 | foreach ($options['filter'] as $key => $value) { |
||
239 | $filterValue = $value['value']; |
||
240 | $filterOption = $value['option']; |
||
241 | $filtered = array(); |
||
242 | |||
243 | foreach ($data['data'] as $record) { |
||
244 | if ( |
||
245 | ($filterOption === 'EQ' && $record[$key] === $filterValue) |
||
246 | || ($filterOption === 'GT' && $record[$key] > $filterValue) |
||
247 | || ($filterOption === 'LT' && $record[$key] < $filterValue) |
||
248 | || ($filterOption === 'LIKE' && strpos($record[$key], $filterValue) !== FALSE) |
||
249 | ) { |
||
250 | array_push($filtered, $record); |
||
251 | } else if ($filterOption === 'IN') { |
||
252 | $filterValues = explode(',', $filterValue); |
||
253 | if (in_array($record[$key], $filterValues)) { |
||
254 | array_push($filtered, $record); |
||
255 | } |
||
256 | } |
||
257 | } |
||
258 | $data['data'] = $filtered; |
||
259 | } |
||
260 | } |
||
261 | return $data; |
||
262 | } |
||
263 | |||
264 | private function aggregateData($data, $filter) |
||
316 | } |
||
317 | } |
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