|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NovaExportConfiguration\Export; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Illuminate\Support\Facades\Log; |
|
7
|
|
|
use Maatwebsite\Excel\Concerns\Exportable; |
|
8
|
|
|
use Maatwebsite\Excel\Concerns\FromQuery; |
|
9
|
|
|
use Maatwebsite\Excel\Concerns\WithCustomChunkSize; |
|
10
|
|
|
use Maatwebsite\Excel\Concerns\WithEvents; |
|
11
|
|
|
use Maatwebsite\Excel\Concerns\WithHeadings; |
|
12
|
|
|
use Maatwebsite\Excel\Concerns\WithMapping; |
|
13
|
|
|
use Maatwebsite\Excel\Events\AfterSheet; |
|
14
|
|
|
use NovaResourceDynamicExport\Export\HasFileModel; |
|
15
|
|
|
use NovaResourceDynamicExport\Export\WithNotification; |
|
16
|
|
|
|
|
17
|
|
|
abstract class ConfiguredExport implements FromQuery, WithMapping, WithEvents, WithHeadings, WithCustomChunkSize |
|
18
|
|
|
{ |
|
19
|
|
|
use Exportable, HasFileModel, WithNotification; |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
protected ExportQuery $exportQuery; |
|
22
|
|
|
|
|
23
|
|
|
protected int $chunkSize = 500; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct(ExportQuery $exportQuery) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->exportQuery = $exportQuery; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
abstract public function headings(): array; |
|
31
|
|
|
|
|
32
|
|
|
abstract public function map($row): array; |
|
33
|
|
|
|
|
34
|
|
|
public function query() |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->exportQuery->query(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function setChunkSize(int $chunkSize): static |
|
40
|
|
|
{ |
|
41
|
|
|
$this->chunkSize = $chunkSize; |
|
42
|
|
|
|
|
43
|
|
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function chunkSize(): int |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->chunkSize; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function registerEvents(): array |
|
52
|
|
|
{ |
|
53
|
|
|
return [ |
|
54
|
|
|
AfterSheet::class => function (AfterSheet $event) { |
|
|
|
|
|
|
55
|
|
|
try { |
|
56
|
|
|
$this->exportQuery->getConfigurationModel()->update([ |
|
57
|
|
|
'last_export_at' => Carbon::now(), |
|
58
|
|
|
]); |
|
59
|
|
|
} catch (\Exception $e) { |
|
60
|
|
|
Log::error($e->getMessage()); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$fileModel = $this->saveFileFromModel(); |
|
64
|
|
|
if($fileModel?->exists) { |
|
65
|
|
|
$this->notifyUser($fileModel); |
|
66
|
|
|
} |
|
67
|
|
|
}, |
|
68
|
|
|
]; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|