-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCsvImporter.php
162 lines (128 loc) · 4.13 KB
/
CsvImporter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace Coderflex\LaravelCsv\Http\Livewire;
use Coderflex\LaravelCsv\Concerns;
use function Coderflex\LaravelCsv\csv_view_path;
use Coderflex\LaravelCsv\Facades\LaravelCsv;
use Coderflex\LaravelCsv\Jobs\ImportCsv;
use Coderflex\LaravelCsv\Utilities\ChunkIterator;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\Validator;
use Livewire\Component;
use Livewire\WithFileUploads;
class CsvImporter extends Component
{
use WithFileUploads;
use Concerns\InteractsWithColumns;
use Concerns\HasCsvProperties;
/** @var string */
public $model;
public bool $open = false;
/** @var object */
public $file;
public array $columnsToMap = [];
public array $requiredColumns = [];
public array $columnLabels = [];
public array $fileHeaders = [];
public int $fileRowCount = 0;
/** @var array */
protected $exceptions = [
'model', 'columnsToMap', 'open',
'columnLabels', 'requiredColumns',
];
/** @var array */
protected $listeners = [
'toggle',
];
public function mount()
{
// map and coverts the columnsToMap property into an associative array
$this->columnsToMap = $this->mapThroughColumns();
// map and coverts the requiredColumns property int key => value array
$this->columnLabels = $this->mapThroughColumnLabels();
// map and coverts the requiredColumns property int key => required value
$this->requiredColumns = $this->mapThroughRequiredColumns();
}
public function updatedFile()
{
$this->validateOnly('file');
$this->setCsvProperties();
$this->resetValidation();
}
public function import()
{
$this->validate();
$this->importCsv();
$this->resetExcept($this->exceptions);
$this->emitTo('handle-imports', 'imports.refresh');
}
public function toggle()
{
$this->open = ! $this->open;
}
public function render()
{
return view(csv_view_path('csv-importer'), [
'fileSize' => LaravelCsv::formatFileSize(
config('laravel_csv.file_upload_size', 20000)
),
]);
}
protected function validationAttributes()
{
return $this->columnLabels;
}
protected function rules()
{
return [
'file' => 'required|file|mimes:csv,txt|max:'.config('laravel_csv.file_upload_size', '20000'),
] + $this->requiredColumns;
}
protected function setCsvProperties()
{
if (! $this->handleCsvProperties() instanceof MessageBag) {
return [
$this->fileHeaders,
$this->fileRowCount
] = $this->handleCsvProperties();
}
$this->withValidator(function (Validator $validator) {
$validator->after(function ($validator) {
$validator->errors()->merge(
$this->handleCsvProperties()->getMessages()
);
});
})->validate();
}
protected function importCsv()
{
$import = $this->createNewImport();
$chunks = (new ChunkIterator($this->csvRecords->getIterator(), 10))->get();
$jobs = collect($chunks)
->map(
fn ($chunk) => new ImportCsv(
$import,
$this->model,
$chunk,
$this->columnsToMap
)
);
Bus::batch($jobs)
->name('import-csv')
->finally(
fn () => $import->touch('completed_at')
)->dispatch();
}
protected function createNewImport()
{
/**
* @var \Coderflex\LaravelCsv\Tests\Models\User */
$user = auth()->user();
return $user->imports()->create([
'model' => $this->model,
'file_path' => $this->file->getRealPath(),
'file_name' => $this->file->getClientOriginalName(),
'total_rows' => $this->fileRowCount,
]);
}
}