|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NovaExportConfiguration\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
|
6
|
|
|
use Illuminate\Foundation\Bus\PendingDispatch; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
use NovaExportConfiguration\Export\ConfiguredExport; |
|
9
|
|
|
use NovaExportConfiguration\Export\ExportQuery; |
|
10
|
|
|
use NovaExportConfiguration\Export\NovaResourceConfig; |
|
11
|
|
|
use NovaExportConfiguration\Models\ExportConfig; |
|
12
|
|
|
use NovaResourceDynamicExport\Models\ExportStoredFile; |
|
13
|
|
|
|
|
14
|
|
|
abstract class ExportRepository |
|
15
|
|
|
{ |
|
16
|
|
|
abstract public function name(): string; |
|
17
|
|
|
|
|
18
|
|
|
public function label(): string |
|
19
|
|
|
{ |
|
20
|
|
|
return Str::ucfirst(Str::snake(Str::camel($this->name()), ' ')); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function disk(): string |
|
24
|
|
|
{ |
|
25
|
|
|
return config('nova-export-configuration.defaults.disk'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function queue(): string |
|
29
|
|
|
{ |
|
30
|
|
|
return config('nova-export-configuration.defaults.queue'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
abstract public function exportFileClass(): string; |
|
34
|
|
|
|
|
35
|
|
|
abstract public function exportQueryClass(): string; |
|
36
|
|
|
|
|
37
|
|
|
abstract public function novaResourceConfigClass(): string; |
|
38
|
|
|
|
|
39
|
|
|
public function exportFile(ExportConfig $model): ConfiguredExport |
|
40
|
|
|
{ |
|
41
|
|
|
$exportFileClass = $this->exportFileClass(); |
|
42
|
|
|
|
|
43
|
|
|
return new $exportFileClass($this->exportQuery($model)); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function exportQuery(ExportConfig $model): ExportQuery |
|
47
|
|
|
{ |
|
48
|
|
|
$exportQueryClass = $this->exportQueryClass(); |
|
49
|
|
|
|
|
50
|
|
|
return new $exportQueryClass($model); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function novaResourceConfig(): NovaResourceConfig |
|
54
|
|
|
{ |
|
55
|
|
|
$novaResourceConfigClass= $this->novaResourceConfigClass(); |
|
56
|
|
|
|
|
57
|
|
|
return new $novaResourceConfigClass(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function export(ExportConfig $model, ExportStoredFile $file): ConfiguredExport |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->exportFile($model) |
|
63
|
|
|
->useStoreFile($file) |
|
64
|
|
|
->setNotificationUser($file->meta->fromMorph('user')); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
abstract public function isFilterKey(string $key): bool; |
|
68
|
|
|
|
|
69
|
|
|
abstract public function modelSetAttribute(ExportConfig $model, $key, $value); |
|
70
|
|
|
|
|
71
|
|
|
abstract public function modelGetAttribute(ExportConfig $model, $key); |
|
72
|
|
|
|
|
73
|
|
|
public function regenerateConfigurationData(ExportConfig $model, bool $persist): void |
|
|
|
|
|
|
74
|
|
|
{ |
|
75
|
|
|
if ($model->exists && $model->getKey() && $this->pivotRelation($model)) { |
|
|
|
|
|
|
76
|
|
|
$this->rebuildAttachedPivots(dispatch(function () use ($model) { |
|
77
|
|
|
$pivotRelation = $this->pivotRelation($model); |
|
|
|
|
|
|
78
|
|
|
$query = $this->exportQuery($model)->query(); |
|
79
|
|
|
$pivotRelation->detach(); |
|
80
|
|
|
$query->select('id') |
|
81
|
|
|
->chunk(1000, function ($models) use ($pivotRelation) { |
|
82
|
|
|
$pivotRelation->syncWithoutDetaching($models->pluck('id')->toArray()); |
|
83
|
|
|
}); |
|
84
|
|
|
})); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
protected function pivotRelation(ExportConfig $model): ?BelongsToMany |
|
|
|
|
|
|
89
|
|
|
{ |
|
90
|
|
|
return null; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
protected function rebuildAttachedPivots(PendingDispatch $pendingDispatch): void |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.