Total Complexity | 111 |
Total Lines | 686 |
Duplicated Lines | 0 % |
Changes | 12 | ||
Bugs | 1 | Features | 1 |
Complex classes like ExcelImportExport 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 ExcelImportExport, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class ExcelImportExport |
||
31 | { |
||
32 | use Configurable; |
||
33 | |||
34 | /** |
||
35 | * Setting this to false improve performance but may lead to skipped cells |
||
36 | * @var bool |
||
37 | */ |
||
38 | public static $iterate_only_existing_cells = false; |
||
39 | |||
40 | /** |
||
41 | * Useful if importing only one sheet or if computation fails |
||
42 | * @var bool |
||
43 | */ |
||
44 | public static $use_old_calculated_value = false; |
||
45 | |||
46 | /** |
||
47 | * Some excel sheets need extra processing |
||
48 | * @var boolean |
||
49 | */ |
||
50 | public static $process_headers = false; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | public static $default_tmp_reader = 'Xlsx'; |
||
56 | |||
57 | /** |
||
58 | * @var integer |
||
59 | */ |
||
60 | public static $limit_exports = 1000; |
||
61 | |||
62 | /** |
||
63 | * You can override the static var with yml config if needed |
||
64 | * @return int |
||
65 | */ |
||
66 | public static function getExportLimit() |
||
67 | { |
||
68 | $v = self::config()->export_limit; |
||
69 | if ($v) { |
||
70 | return $v; |
||
71 | } |
||
72 | return self::$limit_exports; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Get all db fields for a given dataobject class |
||
77 | * |
||
78 | * @param string $class |
||
79 | * @return array<string,string> |
||
80 | */ |
||
81 | public static function allFieldsForClass($class) |
||
82 | { |
||
83 | $dataClasses = ClassInfo::dataClassesFor($class); |
||
84 | $fields = []; |
||
85 | $dataObjectSchema = DataObject::getSchema(); |
||
86 | foreach ($dataClasses as $dataClass) { |
||
87 | $dataFields = $dataObjectSchema->databaseFields($dataClass); |
||
88 | $fields = array_merge($fields, array_keys($dataFields)); |
||
89 | } |
||
90 | return array_combine($fields, $fields); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Get fields that should be exported by default for a class |
||
95 | * |
||
96 | * @param string $class |
||
97 | * @return array<int|string,mixed> |
||
98 | */ |
||
99 | public static function exportFieldsForClass($class) |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Get fields that can be imported by default for a class |
||
130 | * |
||
131 | * @param string $class |
||
132 | * @return array<string,string> |
||
133 | */ |
||
134 | public static function importFieldsForClass($class) |
||
152 | } |
||
153 | |||
154 | public static function createDownloadSampleLink($importer = '') |
||
155 | { |
||
156 | /** @var \SilverStripe\Admin\ModelAdmin $owner */ |
||
157 | $owner = Controller::curr(); |
||
158 | $class = $owner->getModelClass(); |
||
159 | $downloadSampleLink = $owner->Link(str_replace('\\', '-', $class) . '/downloadsample?importer=' . urlencode($importer)); |
||
160 | $downloadSample = '<a href="' . $downloadSampleLink . '" class="no-ajax" target="_blank">' . _t( |
||
161 | 'ExcelImportExport.DownloadSample', |
||
162 | 'Download sample file' |
||
163 | ) . '</a>'; |
||
164 | return $downloadSample; |
||
165 | } |
||
166 | |||
167 | public static function createSampleFile($data, $fileName) |
||
168 | { |
||
169 | $ext = self::getDefaultExtension(); |
||
170 | $fileNameExtension = pathinfo($fileName, PATHINFO_EXTENSION); |
||
171 | if (!$fileNameExtension) { |
||
172 | $fileName .= ".$ext"; |
||
173 | } |
||
174 | $options = new Options(); |
||
175 | $options->creator = ExcelImportExport::config()->default_creator; |
||
176 | SpreadCompat::output($data, $fileName, $options); |
||
177 | exit(); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Output a sample file for a class |
||
182 | * |
||
183 | * A custom file can be provided with a custom sampleExcelFile method |
||
184 | * either as a file path or as a Excel instance |
||
185 | * |
||
186 | * @param string $class |
||
187 | * @return void |
||
188 | */ |
||
189 | public static function sampleFileForClass($class) |
||
190 | { |
||
191 | $ext = self::getDefaultExtension(); |
||
192 | $filter = new FileNameFilter(); |
||
193 | $fileName = $filter->filter("sample-file-for-$class.$ext"); |
||
194 | $spreadsheet = null; |
||
195 | |||
196 | $sng = singleton($class); |
||
197 | |||
198 | // Deprecated |
||
199 | if ($sng->hasMethod('sampleExcelFile')) { |
||
200 | $spreadsheet = $sng->sampleExcelFile(); |
||
201 | // PHPSpreadsheet is required for this |
||
202 | $writer = self::getDefaultWriter($spreadsheet); |
||
203 | self::outputHeaders($fileName); |
||
204 | $writer->save('php://output'); |
||
205 | exit(); |
||
206 | } |
||
207 | |||
208 | if ($sng->hasMethod('sampleImportData')) { |
||
209 | $data = $sng->sampleImportData(); |
||
210 | } else { |
||
211 | // Simply output the default headers |
||
212 | $data = [ExcelImportExport::importFieldsForClass($class)]; |
||
213 | } |
||
214 | |||
215 | if (!is_iterable($data)) { |
||
216 | throw new Exception("`sampleImportData` must return an iterable"); |
||
217 | } |
||
218 | |||
219 | self::createSampleFile($data, $fileName); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * @param \SilverStripe\Admin\ModelAdmin $controller |
||
224 | * @return bool |
||
225 | */ |
||
226 | public static function checkImportForm($controller) |
||
227 | { |
||
228 | if (!$controller->showImportForm) { |
||
229 | return false; |
||
230 | } |
||
231 | $modelClass = $controller->getModelClass(); |
||
232 | if (is_array($controller->showImportForm)) { |
||
233 | /** @var array<string> $valid */ |
||
234 | $valid = $controller->showImportForm; |
||
235 | if (!in_array($modelClass, $valid)) { |
||
236 | return false; |
||
237 | } |
||
238 | } |
||
239 | return true; |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * @param string $handler |
||
244 | * @param Form $form |
||
245 | * @param Controller $controller |
||
246 | * @return HTTPResponse |
||
247 | */ |
||
248 | public static function useCustomHandler($handler, Form $form, Controller $controller) |
||
249 | { |
||
250 | if (!$handler || !method_exists($handler, "load")) { |
||
251 | $form->sessionMessage("Invalid handler: $handler", 'bad'); |
||
252 | return $controller->redirectBack(); |
||
253 | } |
||
254 | $file = $_FILES['_CsvFile']['tmp_name']; |
||
255 | $name = $_FILES['_CsvFile']['name']; |
||
256 | $inst = new $handler(); |
||
257 | |||
258 | if (!empty($_POST['OnlyUpdateRecords']) && method_exists($inst, 'setOnlyUpdate')) { |
||
259 | $inst->setOnlyUpdate(true); |
||
260 | } |
||
261 | /** @var ExcelLoaderInterface $inst */ |
||
262 | try { |
||
263 | $results = $inst->load($file, $name); |
||
264 | } catch (Exception $e) { |
||
265 | $form->sessionMessage($e->getMessage(), 'bad'); |
||
266 | return $controller->redirectBack(); |
||
267 | } |
||
268 | |||
269 | $message = ''; |
||
270 | if ($results instanceof BulkLoader_Result) { |
||
271 | if ($results->CreatedCount()) { |
||
272 | $message .= _t( |
||
273 | 'ModelAdmin.IMPORTEDRECORDS', |
||
274 | "Imported {count} records.", |
||
275 | ['count' => $results->CreatedCount()] |
||
276 | ); |
||
277 | } |
||
278 | if ($results->UpdatedCount()) { |
||
279 | $message .= _t( |
||
280 | 'ModelAdmin.UPDATEDRECORDS', |
||
281 | "Updated {count} records.", |
||
282 | ['count' => $results->UpdatedCount()] |
||
283 | ); |
||
284 | } |
||
285 | if ($results->DeletedCount()) { |
||
286 | $message .= _t( |
||
287 | 'ModelAdmin.DELETEDRECORDS', |
||
288 | "Deleted {count} records.", |
||
289 | ['count' => $results->DeletedCount()] |
||
290 | ); |
||
291 | } |
||
292 | if (!$results->CreatedCount() && !$results->UpdatedCount()) { |
||
293 | $message .= _t('ModelAdmin.NOIMPORT', "Nothing to import"); |
||
294 | } |
||
295 | } else { |
||
296 | // Or we have a simple result |
||
297 | $message = $results; |
||
298 | } |
||
299 | |||
300 | $form->sessionMessage($message, 'good'); |
||
301 | return $controller->redirectBack(); |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * @return string |
||
306 | */ |
||
307 | public static function getDefaultExtension() |
||
308 | { |
||
309 | return self::config()->default_extension ?? 'xlsx'; |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * Get default writer for PHPSpreadsheet if installed |
||
314 | * |
||
315 | * @param Spreadsheet $spreadsheet |
||
316 | * @return IWriter |
||
317 | */ |
||
318 | public static function getDefaultWriter(Spreadsheet $spreadsheet): IWriter |
||
319 | { |
||
320 | if (!self::isPhpSpreadsheetAvailable()) { |
||
321 | throw new Exception("PHPSpreadsheet is not installed"); |
||
322 | } |
||
323 | $writer = ucfirst(self::getDefaultExtension()); |
||
324 | return IOFactory::createWriter($spreadsheet, $writer); |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * Get default reader for PHPSpreadsheet if installed |
||
329 | * |
||
330 | * @return IReader |
||
331 | */ |
||
332 | public static function getDefaultReader(): IReader |
||
333 | { |
||
334 | if (!self::isPhpSpreadsheetAvailable()) { |
||
335 | throw new Exception("PHPSpreadsheet is not installed"); |
||
336 | } |
||
337 | $writer = ucfirst(self::getDefaultExtension()); |
||
338 | return IOFactory::createReader($writer); |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * Output excel headers |
||
343 | * |
||
344 | * @param string $fileName |
||
345 | * @return void |
||
346 | */ |
||
347 | public static function outputHeaders($fileName) |
||
348 | { |
||
349 | $ext = pathinfo($fileName, PATHINFO_EXTENSION); |
||
350 | switch ($ext) { |
||
351 | case 'csv': |
||
352 | header('Content-Type: text/csv'); |
||
353 | break; |
||
354 | case 'xlsx': |
||
355 | header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); |
||
356 | break; |
||
357 | default: |
||
358 | header('Content-type: application/vnd.ms-excel'); |
||
359 | break; |
||
360 | } |
||
361 | |||
362 | header('Content-Disposition: attachment; filename="' . $fileName . '"'); |
||
363 | header('Cache-Control: max-age=0'); |
||
364 | ob_clean(); |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * Generate a default import file with all field name |
||
369 | * @deprecated |
||
370 | * @param string $class |
||
371 | * @return string |
||
372 | */ |
||
373 | public static function generateDefaultSampleFile($class) |
||
374 | { |
||
375 | $opts = [ |
||
376 | 'creator' => "SilverStripe" |
||
377 | ]; |
||
378 | $allFields = ExcelImportExport::importFieldsForClass($class); |
||
379 | $tmpname = SpreadCompat::getTempFilename(); |
||
380 | SpreadCompat::write([ |
||
381 | $allFields |
||
382 | ], $tmpname, ...$opts); |
||
383 | return $tmpname; |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * Show valid extensions helper (for uploaders) |
||
388 | * |
||
389 | * @return string |
||
390 | */ |
||
391 | public static function getValidExtensionsText() |
||
392 | { |
||
393 | return _t( |
||
394 | 'ExcelImportExport.VALIDEXTENSIONS', |
||
395 | "Allowed extensions: {extensions}", |
||
396 | array('extensions' => implode(', ', self::getValidExtensions())) |
||
397 | ); |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * Extracted from PHPSpreadsheet |
||
402 | * |
||
403 | * @param string $ext |
||
404 | * @return string |
||
405 | */ |
||
406 | public static function getReaderForExtension($ext) |
||
407 | { |
||
408 | switch (strtolower($ext)) { |
||
409 | case 'xlsx': // Excel (OfficeOpenXML) Spreadsheet |
||
410 | case 'xlsm': // Excel (OfficeOpenXML) Macro Spreadsheet (macros will be discarded) |
||
411 | case 'xltx': // Excel (OfficeOpenXML) Template |
||
412 | case 'xltm': // Excel (OfficeOpenXML) Macro Template (macros will be discarded) |
||
413 | return 'Xlsx'; |
||
414 | case 'xls': // Excel (BIFF) Spreadsheet |
||
415 | case 'xlt': // Excel (BIFF) Template |
||
416 | return 'Xls'; |
||
417 | case 'ods': // Open/Libre Offic Calc |
||
418 | case 'ots': // Open/Libre Offic Calc Template |
||
419 | return 'Ods'; |
||
420 | case 'slk': |
||
421 | return 'Slk'; |
||
422 | case 'xml': // Excel 2003 SpreadSheetML |
||
423 | return 'Xml'; |
||
424 | case 'gnumeric': |
||
425 | return 'Gnumeric'; |
||
426 | case 'htm': |
||
427 | case 'html': |
||
428 | return 'Html'; |
||
429 | case 'csv': |
||
430 | return 'Csv'; |
||
431 | case 'tmp': // Useful when importing uploaded files |
||
432 | return self::$default_tmp_reader; |
||
433 | default: |
||
434 | throw new Exception("Unsupported file type : $ext"); |
||
435 | } |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * @return boolean |
||
440 | */ |
||
441 | public static function isPhpSpreadsheetAvailable() |
||
444 | } |
||
445 | |||
446 | /** |
||
447 | * If you exported separated files, you can merge them in one big file |
||
448 | * Requires PHPSpreadsheet |
||
449 | * @param array<string> $files |
||
450 | * @return Spreadsheet |
||
451 | */ |
||
452 | public static function mergeExcelFiles($files) |
||
453 | { |
||
454 | if (!self::isPhpSpreadsheetAvailable()) { |
||
455 | throw new Exception("PHPSpreadsheet is not installed"); |
||
456 | } |
||
457 | $merged = new Spreadsheet; |
||
458 | $merged->removeSheetByIndex(0); |
||
459 | foreach ($files as $filename) { |
||
460 | $remoteExcel = IOFactory::load($filename); |
||
461 | $merged->addExternalSheet($remoteExcel->getActiveSheet()); |
||
462 | } |
||
463 | return $merged; |
||
464 | } |
||
465 | |||
466 | /** |
||
467 | * Get valid extensions |
||
468 | * |
||
469 | * @return array<string> |
||
470 | */ |
||
471 | public static function getValidExtensions() |
||
472 | { |
||
473 | $v = self::config()->allowed_extensions; |
||
474 | if (!$v || !is_array($v)) { |
||
475 | return []; |
||
476 | } |
||
477 | return $v; |
||
478 | } |
||
479 | |||
480 | /** |
||
481 | * Save content of an array to a file |
||
482 | * |
||
483 | * @param iterable<array<mixed>> $data |
||
484 | * @param string $filepath |
||
485 | * @return void |
||
486 | */ |
||
487 | public static function arrayToFile($data, $filepath) |
||
488 | { |
||
489 | SpreadCompat::write($data, $filepath); |
||
490 | } |
||
491 | |||
492 | /** |
||
493 | * Fast saving to csv |
||
494 | * |
||
495 | * @param array<mixed> $data |
||
496 | * @param string $filepath |
||
497 | * @param string $delimiter |
||
498 | * @param string $enclosure |
||
499 | * @param string $escapeChar |
||
500 | * @return void |
||
501 | */ |
||
502 | public static function arrayToCsv($data, $filepath, $delimiter = ',', $enclosure = '"', $escapeChar = '\\') |
||
503 | { |
||
504 | if (is_file($filepath)) { |
||
505 | unlink($filepath); |
||
506 | } |
||
507 | $options = new Options(); |
||
508 | $options->separator = $delimiter; |
||
509 | $options->enclosure = $enclosure; |
||
510 | $options->escape = $escapeChar; |
||
511 | SpreadCompat::write($data, $filepath, $options); |
||
512 | } |
||
513 | |||
514 | public static function excelColumnRange(string $lower = 'A', string $upper = 'ZZ'): Generator |
||
515 | { |
||
516 | ++$upper; |
||
517 | for ($i = $lower; $i !== $upper; ++$i) { |
||
518 | yield $i; |
||
519 | } |
||
520 | } |
||
521 | |||
522 | /** |
||
523 | * String from column index. |
||
524 | * |
||
525 | * @param int $index Column index (1 = A) |
||
526 | * @param string $fallback |
||
527 | * @return string |
||
528 | */ |
||
529 | public static function getLetter($index, $fallback = 'A') |
||
538 | } |
||
539 | |||
540 | /** |
||
541 | * Convert a file to an array |
||
542 | * |
||
543 | * @param string $filepath |
||
544 | * @param string $delimiter (csv only) |
||
545 | * @param string $enclosure (csv only) |
||
546 | * @param string $ext if extension cannot be deducted from filepath (eg temp files) |
||
547 | * @return array<mixed> |
||
548 | */ |
||
549 | public static function fileToArray($filepath, $delimiter = ';', $enclosure = '', $ext = null) |
||
564 | } |
||
565 | |||
566 | /** |
||
567 | * Convert an excel file to an array |
||
568 | * |
||
569 | * @param string $filepath |
||
570 | * @param string $sheetname Load a specific worksheet by name |
||
571 | * @param bool $onlyExisting Avoid reading empty columns |
||
572 | * @param string $ext if extension cannot be deducted from filepath (eg temp files) |
||
573 | * @return array<mixed> |
||
574 | */ |
||
575 | public static function excelToArray($filepath, $sheetname = null, $onlyExisting = true, $ext = null) |
||
576 | { |
||
577 | if (!self::isPhpSpreadsheetAvailable()) { |
||
578 | throw new Exception("PHPSpreadsheet is not installed"); |
||
579 | } |
||
580 | if ($ext === null) { |
||
581 | $ext = pathinfo($filepath, PATHINFO_EXTENSION); |
||
582 | } |
||
583 | $ext = pathinfo($filepath, PATHINFO_EXTENSION); |
||
584 | $readerType = self::getReaderForExtension($ext); |
||
585 | $reader = IOFactory::createReader($readerType); |
||
586 | $reader->setReadDataOnly(true); |
||
587 | if ($sheetname) { |
||
588 | $reader->setLoadSheetsOnly($sheetname); |
||
589 | } |
||
590 | $data = []; |
||
591 | if ($reader->canRead($filepath)) { |
||
592 | $excel = $reader->load($filepath); |
||
593 | if ($onlyExisting) { |
||
594 | $data = []; |
||
595 | foreach ($excel->getActiveSheet()->getRowIterator() as $row) { |
||
596 | $cellIterator = $row->getCellIterator(); |
||
597 | if (self::$iterate_only_existing_cells) { |
||
598 | $cellIterator->setIterateOnlyExistingCells(true); |
||
599 | } |
||
600 | $cells = []; |
||
601 | foreach ($cellIterator as $cell) { |
||
602 | if (self::$use_old_calculated_value) { |
||
603 | $cells[] = $cell->getOldCalculatedValue(); |
||
604 | } else { |
||
605 | $cells[] = $cell->getFormattedValue(); |
||
606 | } |
||
607 | } |
||
608 | $data[] = $cells; |
||
609 | } |
||
610 | } else { |
||
611 | $data = $excel->getActiveSheet()->toArray(null, true, false, false); |
||
612 | } |
||
613 | } else { |
||
614 | throw new Exception("Cannot read $filepath"); |
||
615 | } |
||
616 | return $data; |
||
617 | } |
||
618 | |||
619 | /** |
||
620 | * @link https://stackoverflow.com/questions/44304795/how-to-retrieve-date-from-table-cell-using-phpspreadsheet#44304796 |
||
621 | * @param int|string|null|float $v |
||
622 | * @return string |
||
623 | */ |
||
624 | public static function convertExcelDate($v) |
||
636 | } |
||
637 | |||
638 | /** |
||
639 | * Convert an excel file to an associative array |
||
640 | * |
||
641 | * Suppose the first line are the headers of the file |
||
642 | * Headers are trimmed in case you have crappy whitespace in your files |
||
643 | * |
||
644 | * @param string $filepath |
||
645 | * @param string $sheetname Load a specific worksheet by name |
||
646 | * @param string $ext if extension cannot be deducted from filepath (eg temp files) |
||
647 | * @return array<mixed> |
||
648 | */ |
||
649 | public static function excelToAssocArray($filepath, $sheetname = null, $ext = null) |
||
716 | } |
||
717 | } |
||
718 |
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