-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
ImportCsv.php
165 lines (135 loc) · 4.52 KB
/
ImportCsv.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
163
164
165
<?php
namespace Filament\Actions\Imports\Jobs;
use Carbon\CarbonInterface;
use Exception;
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
use Filament\Actions\Imports\Importer;
use Filament\Actions\Imports\Models\FailedImportRow;
use Filament\Actions\Imports\Models\Import;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
use Throwable;
class ImportCsv implements ShouldQueue
{
use Batchable;
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
public bool $deleteWhenMissingModels = true;
protected Importer $importer;
/**
* @param array<array<string, string>> | string $rows
* @param array<string, string> $columnMap
* @param array<string, mixed> $options
*/
public function __construct(
protected Import $import,
protected array | string $rows,
protected array $columnMap,
protected array $options = [],
) {
$this->importer = $this->import->getImporter(
$this->columnMap,
$this->options,
);
}
/**
* @return array<int, object>
*/
public function middleware(): array
{
return $this->importer->getJobMiddleware();
}
public function handle(): void
{
/** @var Authenticatable $user */
$user = $this->import->user;
auth()->setUser($user);
$exceptions = [];
$processedRows = 0;
$successfulRows = 0;
if (! is_array($this->rows)) {
$rows = unserialize(base64_decode($this->rows));
}
foreach (($rows ?? $this->rows) as $row) {
$row = $this->utf8Encode($row);
try {
DB::transaction(fn () => ($this->importer)($row));
$successfulRows++;
} catch (RowImportFailedException $exception) {
$this->logFailedRow($row, $exception->getMessage());
} catch (ValidationException $exception) {
$this->logFailedRow($row, collect($exception->errors())->flatten()->implode(' '));
} catch (Throwable $exception) {
$exceptions[$exception::class] = $exception;
$this->logFailedRow($row);
}
$processedRows++;
}
$this->import->refresh();
$importProcessedRows = $this->import->processed_rows + $processedRows;
$this->import->processed_rows = ($importProcessedRows < $this->import->total_rows) ?
$importProcessedRows :
$this->import->total_rows;
$importSuccessfulRows = $this->import->successful_rows + $successfulRows;
$this->import->successful_rows = ($importSuccessfulRows < $this->import->total_rows) ?
$importSuccessfulRows :
$this->import->total_rows;
$this->import->save();
$this->handleExceptions($exceptions);
}
public function retryUntil(): ?CarbonInterface
{
return $this->importer->getJobRetryUntil();
}
/**
* @return array<int, string>
*/
public function tags(): array
{
return $this->importer->getJobTags();
}
/**
* @param array<string, mixed> $data
*/
protected function logFailedRow(array $data, ?string $validationError = null): void
{
$failedRow = app(FailedImportRow::class);
$failedRow->import()->associate($this->import);
$failedRow->data = $data;
$failedRow->validation_error = $validationError;
$failedRow->save();
}
protected function utf8Encode(mixed $value): mixed
{
if (is_array($value)) {
return array_map($this->utf8Encode(...), $value);
}
if (is_string($value)) {
return mb_convert_encoding($value, 'UTF-8', 'UTF-8');
}
return $value;
}
/**
* @param array<Throwable> $exceptions
*/
protected function handleExceptions(array $exceptions): void
{
if (empty($exceptions)) {
return;
}
if (count($exceptions) > 1) {
throw new Exception('Multiple types of exceptions occurred: [' . implode('], [', array_keys($exceptions)) . ']');
}
throw Arr::first($exceptions);
}
}