From 1fb3a0ecf8d96bad05df921e9153270e5909e483 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 2 Jul 2025 14:16:11 -0400 Subject: [PATCH] Download for report --- src/Migration/Sources/CSV.php | 62 ++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 15 deletions(-) diff --git a/src/Migration/Sources/CSV.php b/src/Migration/Sources/CSV.php index 721b84ae..459f9053 100644 --- a/src/Migration/Sources/CSV.php +++ b/src/Migration/Sources/CSV.php @@ -30,6 +30,8 @@ class CSV extends Source private Reader $database; + private bool $downloaded = false; + public function __construct( string $resourceId, string $filePath, @@ -55,14 +57,23 @@ public static function getSupportedResources(): array } // called before the `exportGroupDatabases`. + + /** + * @throws \Exception + */ public function report(array $resources = []): array { $report = []; - if (! $this->device->exists($this->filePath)) { + if (!$this->device->exists($this->filePath)) { return $report; } + $this->downloadToLocal( + $this->device, + $this->filePath, + ); + $file = new \SplFileObject($this->filePath, 'r'); $file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY); @@ -313,20 +324,11 @@ private function withCsvStream(callable $callback): void return; } - if ($this->device->getType() !== Storage::DEVICE_LOCAL) { - try { - $success = $this->device->transfer( - $this->filePath, - $this->filePath, - new Device\Local('/'), - ); - } catch (\Exception $e) { - $success = false; - } - - if (!$success) { - throw new \Exception('Failed to transfer CSV file from device to local storage.', previous: $e ?? null); - } + if (!$this->downloaded) { + $this->downloadToLocal( + $this->device, + $this->filePath, + ); } $stream = \fopen($this->filePath, 'r'); @@ -370,4 +372,34 @@ private function validateCSVHeaders(array $headers, array $attributeTypes): void throw new \Exception('CSV header mismatch. '.implode(' | ', $messages)); } } + + /** + * @throws \Exception + */ + private function downloadToLocal( + Device $device, + string $filePath + ): void { + if ($this->downloaded + || $device->getType() === Storage::DEVICE_LOCAL + ) { + return; + } + + try { + $success = $device->transfer( + $filePath, + $filePath, + new Device\Local('/'), + ); + } catch (\Exception $e) { + $success = false; + } + + if (!$success) { + throw new \Exception('Failed to transfer CSV file from device to local storage.', previous: $e ?? null); + } + + $this->downloaded = true; + } }