|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Coderstm\Jobs; |
| 4 | + |
| 5 | +use Coderstm\Exceptions\ImportSkippedException; |
| 6 | +use Coderstm\Models\Import; |
| 7 | +use Coderstm\Notifications\ImportCompletedNotification; |
| 8 | +use League\Csv\Reader; |
| 9 | +use Illuminate\Bus\Queueable; |
| 10 | +use Illuminate\Support\Facades\DB; |
| 11 | +use Illuminate\Queue\SerializesModels; |
| 12 | +use Illuminate\Queue\InteractsWithQueue; |
| 13 | +use Illuminate\Contracts\Queue\ShouldQueue; |
| 14 | +use Illuminate\Foundation\Bus\Dispatchable; |
| 15 | + |
| 16 | +class ProcessCsvImport implements ShouldQueue |
| 17 | +{ |
| 18 | + use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
| 19 | + |
| 20 | + public Import $import; |
| 21 | + public string $model; |
| 22 | + public string $filePath; |
| 23 | + public array $options; |
| 24 | + |
| 25 | + /** |
| 26 | + * Create a new job instance. |
| 27 | + */ |
| 28 | + public function __construct(Import $import) |
| 29 | + { |
| 30 | + $this->import = $import; |
| 31 | + $this->model = $import->model; |
| 32 | + $this->filePath = $import->file->path(); |
| 33 | + $this->options = $import->options; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Execute the job. |
| 38 | + */ |
| 39 | + public function handle(): void |
| 40 | + { |
| 41 | + $csv = Reader::createFromPath($this->filePath, 'r'); |
| 42 | + $csv->setHeaderOffset(0); |
| 43 | + $csv->setDelimiter(','); |
| 44 | + |
| 45 | + // Normalize CSV headers to remove newlines |
| 46 | + $csvHeaders = array_map('trim', $csv->getHeader()); |
| 47 | + $mappedHeaders = $this->model::getMappedAttributes(); |
| 48 | + |
| 49 | + // Map $headers from $mapped |
| 50 | + $finalHeaders = []; |
| 51 | + foreach ($csvHeaders as $header) { |
| 52 | + if (isset($mappedHeaders[$header])) { |
| 53 | + $finalHeaders[] = $mappedHeaders[$header]; |
| 54 | + } else { |
| 55 | + $finalHeaders[] = $header; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + $this->import->update(['status' => Import::STATUS_PROCESSING]); |
| 60 | + |
| 61 | + // Begin a database transaction |
| 62 | + DB::beginTransaction(); |
| 63 | + |
| 64 | + try { |
| 65 | + foreach ($csv->getRecords($finalHeaders) as $key => $row) { |
| 66 | + try { |
| 67 | + $this->model::createFromCsv($row, $this->options); |
| 68 | + $this->import->addLogs("success", $key); |
| 69 | + } catch (\Exception $e) { |
| 70 | + if ($e instanceof ImportSkippedException) { |
| 71 | + $this->import->addLogs("skipped", $key); |
| 72 | + } else { |
| 73 | + $this->import->addLogs("failed", $key); |
| 74 | + } |
| 75 | + //throw $e; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // Commit the transaction if all records are successfully processed |
| 80 | + DB::commit(); |
| 81 | + |
| 82 | + // Update import status to completed |
| 83 | + $this->import->update(['status' => Import::STATUS_COMPLETED]); |
| 84 | + admin_notify(new ImportCompletedNotification($this->import)); |
| 85 | + } catch (\Exception $e) { |
| 86 | + // Rollback the transaction in case of an error |
| 87 | + DB::rollback(); |
| 88 | + throw $e; |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments