ImportBaseService.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 $value): string
  54. {
  55. list($table, $column) = explode('.', $tableNameCol);
  56. $model = DB::table($table)
  57. ->where($column, $value)
  58. ->first();
  59. if (!$model) {
  60. echo $this->import->log("SKIP: no {$tableNameCol} {$value} found!", 'WARNING');
  61. }
  62. return $model?->id ?? '';
  63. }
  64. protected function prepare(): bool
  65. {
  66. try {
  67. $this->import->log('Reading file...');
  68. $this->readFile();
  69. } catch (Exception $exception){
  70. $this->import->log($exception->getMessage(), 'ERROR');
  71. $this->import->status = 'ERROR';
  72. $this->import->save();
  73. return false;
  74. }
  75. try {
  76. $this->import->log('Checking headers...');
  77. $headers = $this->rowToArray($this->rowIterator->current());
  78. if (!$this->checkHeaders(array_values($headers))) {
  79. throw new Exception("Invalid headers");
  80. }
  81. } catch (Exception $exception){
  82. $this->import->log($exception->getMessage(), 'ERROR');
  83. $this->import->status = 'ERROR';
  84. $this->import->save();
  85. return false;
  86. }
  87. return true;
  88. }
  89. }