Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 47 additions & 15 deletions src/Migration/Sources/CSV.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class CSV extends Source

private Reader $database;

private bool $downloaded = false;

public function __construct(
string $resourceId,
string $filePath,
Expand All @@ -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);

Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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;
}
}