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