ImportBaseService.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Import;
  4. use App\Models\Product;
  5. use Exception;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Storage;
  8. use Illuminate\Support\Str;
  9. use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
  10. use PhpOffice\PhpSpreadsheet\Worksheet\RowIterator;
  11. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  12. class ImportBaseService
  13. {
  14. protected array $headers = [];
  15. protected Worksheet $sheet;
  16. protected RowIterator $rowIterator;
  17. public function __construct(protected readonly Import $import)
  18. {}
  19. public function readFile(): void
  20. {
  21. $path = Storage::disk('upload')->path($this->import->filename);
  22. $reader = new Xlsx();
  23. $spreadsheet = $reader->load($path);
  24. $this->sheet = $spreadsheet->getActiveSheet();
  25. $this->rowIterator = $this->sheet->getRowIterator();
  26. }
  27. protected function rowToArray($row): array
  28. {
  29. $cellIterator = $row->getCellIterator();
  30. $cellIterator->setIterateOnlyExistingCells(FALSE);
  31. $row_content = [];
  32. $keys = array_values($this->headers);
  33. $i = 0;
  34. foreach ($cellIterator as $cell) {
  35. if (isset($keys[$i])) {
  36. $row_content[$keys[$i]] = $cell->getValue();
  37. }
  38. $i++;
  39. }
  40. return $row_content;
  41. }
  42. /**
  43. * @param array $headers
  44. * @return bool
  45. */
  46. protected function checkHeaders(array $headers): bool
  47. {
  48. foreach ($headers as $k => $header) {
  49. $headers[$k] = Str::before($header, "\n");
  50. }
  51. return $headers == array_keys($this->headers);
  52. }
  53. protected function findId(string $tableNameCol, string|null $value): string
  54. {
  55. if(!$value) return '';
  56. list($table, $column) = explode('.', $tableNameCol);
  57. $model = DB::table($table)
  58. ->where($column, $value)
  59. ->first();
  60. if (!$model) {
  61. echo $this->import->log("SKIP: no {$tableNameCol} {$value} found!", 'WARNING');
  62. }
  63. return $model?->id ?? '';
  64. }
  65. protected function prepare(): bool
  66. {
  67. try {
  68. $this->import->log('Reading file...');
  69. $this->readFile();
  70. } catch (Exception $exception){
  71. $this->import->log($exception->getMessage(), 'ERROR');
  72. $this->import->status = 'ERROR';
  73. $this->import->save();
  74. return false;
  75. }
  76. try {
  77. $this->import->log('Checking headers...');
  78. $headers = $this->rowToArray($this->rowIterator->current());
  79. if (!$this->checkHeaders(array_values($headers))) {
  80. throw new Exception("Invalid headers");
  81. }
  82. } catch (Exception $exception){
  83. $this->import->log($exception->getMessage(), 'ERROR');
  84. $this->import->status = 'ERROR';
  85. $this->import->save();
  86. return false;
  87. }
  88. return true;
  89. }
  90. }