-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQueuedExportAsCsv.php
110 lines (91 loc) · 3.16 KB
/
QueuedExportAsCsv.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
<?php
namespace NovaKit\NovaQueuedExportAsCsv\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\Auth\User;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Http\File;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;
use Laravel\Nova\Util;
use NovaKit\NovaQueuedExportAsCsv\Events\QueuedCsvExported;
use Rap2hpoutre\FastExcel\FastExcel;
use function Laravie\SerializesQuery\unserialize;
class QueuedExportAsCsv implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* The query builder.
*
* @var array<string, mixed>
*/
public $query;
/**
* The User ID.
*
* @var string|int
*/
public $userId;
/**
* The custom format callback.
*
* @var (callable(\Illuminate\Database\Eloquent\Model):array<string, mixed>)|null
*/
public $withFormatCallback;
/**
* The configuration options.
*
* @var array{filename: string, storageDisk: string|null, notify: string}
*/
public $options;
/**
* Create a new job instance.
*
* @param array<string, mixed> $query
* @param string|int $userId
* @param (callable(\Illuminate\Database\Eloquent\Model):array<string, mixed>)|null $withFormatCallback
* @param array{filename: string, storageDisk: string|null, notify: string} $options
* @return void
*/
public function __construct(array $query, $userId, $withFormatCallback, array $options)
{
$this->query = $query;
$this->userId = $userId;
$this->withFormatCallback = $withFormatCallback;
$this->options = array_merge([
'storageDisk' => null,
], $options);
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$query = unserialize($this->query);
$eloquentGenerator = function () use ($query) {
foreach ($query->cursor() as $model) {
yield $model;
}
};
/** @phpstan-ignore-next-line */
$withFormatCallback = ! is_null($this->withFormatCallback) ? \unserialize($this->withFormatCallback)->getClosure() : null;
/** @var \Illuminate\Contracts\Auth\Authenticatable&\Illuminate\Database\Eloquent\Model $userModel */
$userModel = Util::userModel();
$storageDisk = $this->options['storageDisk'];
$filename = $this->options['filename'];
$exportedFilename = (new FastExcel($eloquentGenerator()))->export("/tmp/{$filename}", $withFormatCallback);
$storedFilename = Storage::disk($storageDisk)->putFileAs(
'nova-actions-export-as-csv', new File($exportedFilename), $filename, 'public'
);
(new Filesystem())->delete($exportedFilename);
/** @var \Illuminate\Contracts\Auth\Authenticatable&\Illuminate\Database\Eloquent\Model $user */
$user = $userModel::findOrFail($this->userId);
QueuedCsvExported::dispatch(
$user, $storedFilename, $storageDisk
);
}
}