78 lines
2.7 KiB
PHP
78 lines
2.7 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Jobs;
|
||
|
|
|
||
|
|
use App\Models\Product;
|
||
|
|
use Carbon\Carbon;
|
||
|
|
use Illuminate\Bus\Queueable;
|
||
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||
|
|
use Illuminate\Queue\InteractsWithQueue;
|
||
|
|
use Illuminate\Queue\SerializesModels;
|
||
|
|
use Illuminate\Support\Facades\Storage;
|
||
|
|
use Illuminate\Support\Str;
|
||
|
|
|
||
|
|
class ExportProduct implements ShouldQueue
|
||
|
|
{
|
||
|
|
use Dispatchable;
|
||
|
|
use InteractsWithQueue;
|
||
|
|
use Queueable;
|
||
|
|
use SerializesModels;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Execute the job.
|
||
|
|
*
|
||
|
|
* @return void
|
||
|
|
* @throws \Error If file storage config is wrong.
|
||
|
|
* @throws \RuntimeException If the storage disk export is not local file system.
|
||
|
|
*/
|
||
|
|
public function __construct(int $productId)
|
||
|
|
{
|
||
|
|
/** @var \App\Models\Product $product */
|
||
|
|
$product = Product::query()->where('id', intval($productId))->firstOrFail();
|
||
|
|
|
||
|
|
/** @var \Illuminate\Filesystem\FilesystemAdapter */
|
||
|
|
$exportDisk = Storage::disk('export');
|
||
|
|
|
||
|
|
if (!($exportDisk instanceof \Illuminate\Filesystem\FilesystemAdapter)) {
|
||
|
|
throw new \Error('Storage disk export has to be local file system');
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!$exportDisk->directoryExists('export-product')) {
|
||
|
|
$exportDisk->createDirectory('export-product');
|
||
|
|
}
|
||
|
|
|
||
|
|
// * Create zip
|
||
|
|
$zip = new \ZipArchive();
|
||
|
|
$fileName = date('d-m-Y') . '-' . Str::uuid() . '.zip';
|
||
|
|
|
||
|
|
if (!$zip->open($exportDisk->path("export-product/{$fileName}"), \ZipArchive::CREATE) === true) {
|
||
|
|
throw new \RuntimeException("The ZIP file cannot be created.");
|
||
|
|
}
|
||
|
|
|
||
|
|
$dirName = Str::slug(Carbon::parse($product->updated_at)->format('y-m-d') . '-' . $product->user->lastname . '-' . $product->user->id);
|
||
|
|
|
||
|
|
if (!$zip->addEmptyDir($dirName)) {
|
||
|
|
throw new \RuntimeException("The folder cannot be created inside the ZIP.");
|
||
|
|
}
|
||
|
|
|
||
|
|
// * Add files
|
||
|
|
$pdf = Product::generateProductPdf($product, "test");
|
||
|
|
$exportDisk->put('pdf/' . $product->id . '.pdf', $pdf->output());
|
||
|
|
$zip->addFile($exportDisk->path('pdf/' . $product->id . '.pdf'), $dirName . "/" . $product->name . ".pdf");
|
||
|
|
$zip->addFile($product->picture, $dirName . "/photo." . pathinfo($product->picture, PATHINFO_EXTENSION));
|
||
|
|
foreach ($product->docs as $doc) {
|
||
|
|
$zip->addFile($doc, $dirName . "/document." . pathinfo($doc, PATHINFO_EXTENSION));
|
||
|
|
}
|
||
|
|
|
||
|
|
// * Save zip
|
||
|
|
$zip->close();
|
||
|
|
|
||
|
|
if (!$exportDisk->exists("export-product/{$fileName}")) {
|
||
|
|
throw new \RuntimeException("The ZIP file 'export-product/{$fileName}' has not been created.");
|
||
|
|
}
|
||
|
|
|
||
|
|
Product::downloadPrivateExportFile("export-product/{$fileName}");
|
||
|
|
}
|
||
|
|
}
|