-
Notifications
You must be signed in to change notification settings - Fork 454
/
DatabaseToCsvExporter.php
145 lines (123 loc) · 3.27 KB
/
DatabaseToCsvExporter.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
<?php
namespace App\Jobs;
use App\Exports\EloquentExporter;
use App\Models\Exporter\Export;
use App\Models\NotificationType;
use App\Services\NotificationService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\DatabaseManager;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;
class DatabaseToCsvExporter implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* @var int
*/
public $timeout = 1800;
/**
* @var Export
*/
private $export;
/**
* @var EloquentExporter
*/
private $exporter;
/**
* Create a new job instance.
*
* @param Export $export
*/
public function __construct(Export $export)
{
$this->export = $export;
}
/**
* @return EloquentExporter
*/
public function getExporter()
{
if (empty($this->exporter)) {
$this->exporter = new EloquentExporter($this->export);
}
return $this->exporter;
}
/**
* @param Export $export
*
* @return string
*/
public function transformTenantFilename(Export $export)
{
return sprintf(
'%s/csv/%s/%s',
$export->getConnectionName(),
$export->hash,
$export->filename
);
}
/**
* @param string $filename
*
* @return string
*/
public function transformTenantUrl($filename)
{
return Storage::url($filename);
}
/**
* @param EloquentExporter $exporter
*
* @return string
*/
public function getMessageToNotification(EloquentExporter $exporter)
{
return "Foram exportados {$exporter->getExportCount()} registros. Clique aqui para fazer download do arquivo {$this->export->filename}.";
}
/**
* Execute the job.
*
* @param NotificationService $notification
* @param DatabaseManager $manager
*
* @throws FileNotFoundException
*
* @return void
*/
public function handle(NotificationService $notification, DatabaseManager $manager)
{
$manager->setDefaultConnection(
$sftp = $this->export->getConnectionName()
);
$exporter = $this->getExporter();
$file = $this->export->hash;
$manager->unprepared(
"COPY ({$exporter->query()}) TO '/tmp/{$file}' CSV HEADER;"
);
Storage::disk()->put(
$filename = $this->transformTenantFilename($this->export),
Storage::disk($sftp)->get("/tmp/{$file}")
);
Storage::disk($sftp)->delete("/tmp/{$file}");
$url = $this->transformTenantUrl($filename);
$notification->createByUser(
$this->export->user_id,
$this->getMessageToNotification($exporter),
$url,
NotificationType::EXPORT_STUDENT
);
$this->export->url = $url;
$this->export->save();
}
public function tags()
{
return [
$this->export->getConnectionName(),
'csv-export'
];
}
}