From 1a61adb8237d030fbc595751267e6ef16c9cd911 Mon Sep 17 00:00:00 2001 From: Chris Bautista Date: Fri, 11 Oct 2024 13:07:41 +0800 Subject: [PATCH] ExportProcessor updates --- README.md | 65 ++++++++++++++++++++++++++++- config/nova-data-sync.php | 1 + src/Export/Jobs/ExportProcessor.php | 17 ++++---- 3 files changed, 71 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index b36b397..030f76e 100644 --- a/README.md +++ b/README.md @@ -284,8 +284,69 @@ class UserExportProcessor extends ExportProcessor } ``` -Next, create an `ExportNovaAction` class and create a `processor()` function that returns the processor class you just -created. +You can also override methods in the `ExportProcessor` class to customize the export process. The following methods can +be overridden: + +```php +startDate)->startOfDay(); + $endDate = Carbon::make($this->endDate)->endOfDay(); + + return Product::query() + ->whereBetween('created_at', [$startDate, $endDate]) + ->with('productCategory'); + } + + public function formatRow($row): array + { + return [ + 'name' => $row->name, + 'product cat' => $row->productCategory->name ?? null, + 'price' => $row->price, + ]; + } + + public static function queueName(): string + { + return 'custom-queue-name'; // Default is whatever is set in the config + } + + public function allowFailures(): bool + { + return true; // Default is whatever is set in the config + } + + public function disk(): string + { + return 'custom-disk-name'; // Default is whatever is set in the config + } + + public static function chunkSize(): int + { + return 100; // Default is whatever is set in the config + } +} +``` + +Next, in order to use it as a Nova Action, create an `ExportNovaAction` class and create a `processor()` function that +returns the processor class you just created. ```php namespace App\Nova\Exports; diff --git a/config/nova-data-sync.php b/config/nova-data-sync.php index 527687e..35ca023 100644 --- a/config/nova-data-sync.php +++ b/config/nova-data-sync.php @@ -14,6 +14,7 @@ 'chunk_size' => 1000, 'queue' => 'default', 'table_name' => 'exports', + 'allow_failures' => true, ], 'nova_resources' => [ diff --git a/src/Export/Jobs/ExportProcessor.php b/src/Export/Jobs/ExportProcessor.php index e90f816..c6240fd 100644 --- a/src/Export/Jobs/ExportProcessor.php +++ b/src/Export/Jobs/ExportProcessor.php @@ -28,14 +28,11 @@ abstract class ExportProcessor implements ShouldQueue protected string $directory = ''; - /** - * Pass your query here as a Builder instance - */ abstract public function query(): Builder; public function __construct() { - $this->onQueue($this->queue()); + $this->onQueue(self::queueName()); } /** @@ -104,7 +101,7 @@ public function handle(): void }) ->allowFailures($this->allowFailures()) ->name($this->name()) - ->onQueue(config('nova-data-sync.exports.queue', 'default')) + ->onQueue(self::queueName()) ->dispatch(); } @@ -124,19 +121,19 @@ protected function name(): string } /** - * Override this in your ExportProcessor class to set a different queue. + * The queue to use for the export */ - protected function queue(): string + public static function queueName(): string { return config('nova-data-sync.exports.queue', 'default'); } /** - * Override this in your ExportProcessor class to set allow failures in the batch job. + * Whether to allow failures in the batch job */ - protected function allowFailures(): bool + public function allowFailures(): bool { - return true; + return config('nova-data-sync.exports.allow_failures', true); } protected function directory(): string