$records * @param array $columnMap * @param array $options */ public function __construct( protected Export $export, protected string $query, protected array $records, protected int $page, protected array $columnMap, protected array $options = [], ) { $this->exporter = $this->export->getExporter( $this->columnMap, $this->options, ); } /** * @return array */ public function middleware(): array { return $this->exporter->getJobMiddleware(); } public function handle(): void { /** @var Authenticatable $user */ $user = $this->export->user; if (method_exists(auth()->guard(), 'login')) { auth()->login($user); } else { auth()->setUser($user); } $exceptions = []; $processedRows = 0; $successfulRows = 0; $csv = Writer::createFromFileObject(new SplTempFileObject); $csv->setDelimiter($this->exporter::getCsvDelimiter()); $query = EloquentSerializeFacade::unserialize($this->query); foreach ($this->exporter->getCachedColumns() as $column) { $column->applyRelationshipAggregates($query); $column->applyEagerLoading($query); } foreach ($query->find($this->records) as $record) { try { $csv->insertOne(($this->exporter)($record)); $successfulRows++; } catch (Throwable $exception) { $exceptions[$exception::class] = $exception; } $processedRows++; } $filePath = $this->export->getFileDirectory() . DIRECTORY_SEPARATOR . str_pad(strval($this->page), 16, '0', STR_PAD_LEFT) . '.csv'; $this->export->getFileDisk()->put($filePath, $csv->toString(), Filesystem::VISIBILITY_PRIVATE); $this->export::query() ->whereKey($this->export->getKey()) ->update([ 'processed_rows' => DB::raw('processed_rows + ' . $processedRows), 'successful_rows' => DB::raw('successful_rows + ' . $successfulRows), ]); $this->export::query() ->whereKey($this->export->getKey()) ->whereColumn('processed_rows', '>', 'total_rows') ->update([ 'processed_rows' => DB::raw('total_rows'), ]); $this->export::query() ->whereKey($this->export->getKey()) ->whereColumn('successful_rows', '>', 'total_rows') ->update([ 'successful_rows' => DB::raw('total_rows'), ]); $this->handleExceptions($exceptions); } public function retryUntil(): ?CarbonInterface { return $this->exporter->getJobRetryUntil(); } /** * @return array */ public function tags(): array { return $this->exporter->getJobTags(); } /** * @param array $exceptions */ protected function handleExceptions(array $exceptions): void { if (empty($exceptions)) { return; } if (count($exceptions) > 1) { throw new Exception('Multiple types of exceptions occurred: [' . implode('], [', array_keys($exceptions)) . ']'); } throw Arr::first($exceptions); } }