|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace FromHome\ModelUpload; |
| 6 | + |
| 7 | +use Webmozart\Assert\Assert; |
| 8 | +use Maatwebsite\Excel\Concerns\ToModel; |
| 9 | +use Maatwebsite\Excel\Events\AfterImport; |
| 10 | +use Maatwebsite\Excel\Concerns\Importable; |
| 11 | +use Maatwebsite\Excel\Concerns\WithEvents; |
| 12 | +use Illuminate\Contracts\Queue\ShouldQueue; |
| 13 | +use Illuminate\Foundation\Bus\PendingDispatch; |
| 14 | +use Maatwebsite\Excel\Concerns\WithHeadingRow; |
| 15 | +use FromHome\ModelUpload\Models\ModelUploadFile; |
| 16 | +use Maatwebsite\Excel\Concerns\WithBatchInserts; |
| 17 | +use Maatwebsite\Excel\Concerns\WithChunkReading; |
| 18 | +use FromHome\ModelUpload\Models\ModelUploadRecord; |
| 19 | +use Maatwebsite\Excel\Concerns\SkipsUnknownSheets; |
| 20 | +use Maatwebsite\Excel\Concerns\WithMultipleSheets; |
| 21 | +use FromHome\ModelUpload\Jobs\ProcessModelRecordJob; |
| 22 | + |
| 23 | +final class ModelRecordImport implements ShouldQueue, SkipsUnknownSheets, ToModel, WithBatchInserts, WithChunkReading, WithEvents, WithHeadingRow, WithMultipleSheets |
| 24 | +{ |
| 25 | + use Importable; |
| 26 | + |
| 27 | + private ?ModelUploadFile $uploadFile = null; |
| 28 | + |
| 29 | + private array $meta = []; |
| 30 | + |
| 31 | + public static function new(): self |
| 32 | + { |
| 33 | + return new self(); |
| 34 | + } |
| 35 | + |
| 36 | + public function forFile(ModelUploadFile $uploadFile): self |
| 37 | + { |
| 38 | + $this->uploadFile = $uploadFile; |
| 39 | + |
| 40 | + return $this; |
| 41 | + } |
| 42 | + |
| 43 | + public function withMeta(array $meta): self |
| 44 | + { |
| 45 | + $this->meta = $meta; |
| 46 | + |
| 47 | + return $this; |
| 48 | + } |
| 49 | + |
| 50 | + public function process(): PendingDispatch |
| 51 | + { |
| 52 | + Assert::notNull($this->uploadFile); |
| 53 | + |
| 54 | + return $this->queue( |
| 55 | + $this->uploadFile->getAttribute('file_path'), |
| 56 | + $this->uploadFile->getAttribute('storage_disk'), |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + public function model(array $row): ModelUploadRecord |
| 61 | + { |
| 62 | + Assert::notNull($this->uploadFile); |
| 63 | + |
| 64 | + return new ModelUploadRecord([ |
| 65 | + 'model_upload_file_id' => $this->uploadFile->getKey(), |
| 66 | + 'payload' => $row, |
| 67 | + 'meta' => $this->meta, |
| 68 | + ]); |
| 69 | + } |
| 70 | + |
| 71 | + public function batchSize(): int |
| 72 | + { |
| 73 | + return 500; |
| 74 | + } |
| 75 | + |
| 76 | + public function chunkSize(): int |
| 77 | + { |
| 78 | + return 500; |
| 79 | + } |
| 80 | + |
| 81 | + public function registerEvents(): array |
| 82 | + { |
| 83 | + return [ |
| 84 | + AfterImport::class => function (): void { |
| 85 | + Assert::notNull($this->uploadFile); |
| 86 | + |
| 87 | + dispatch(new ProcessModelRecordJob($this->uploadFile)); |
| 88 | + }, |
| 89 | + ]; |
| 90 | + } |
| 91 | + |
| 92 | + public function sheets(): array |
| 93 | + { |
| 94 | + return [ |
| 95 | + 'DATA' => $this, |
| 96 | + ]; |
| 97 | + } |
| 98 | + |
| 99 | + public function onUnknownSheet($sheetName): void |
| 100 | + { |
| 101 | + } |
| 102 | +} |
0 commit comments