2026-07-06 03:42:24 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Modules\Core\Command;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
use Modules\Core\MigrateImport\ImportSpec;
|
|
|
|
|
use Modules\Core\MigrateImport\RunMigrateImportJob;
|
|
|
|
|
|
|
|
|
|
class MigrateImportCommand extends Command
|
|
|
|
|
{
|
|
|
|
|
protected $signature = "boboko:migrate:import
|
|
|
|
|
{--source= : The source platform to import from}
|
|
|
|
|
{--type= : The import mechanism for the chosen source}
|
|
|
|
|
{--file= : Path to the export file (when type is export)}";
|
|
|
|
|
|
|
|
|
|
protected $description = "Migrate data (products, customers, etc.) from an external source";
|
|
|
|
|
|
|
|
|
|
// Supported sources. Only "shopify" is wired up for now.
|
|
|
|
|
private const SOURCES = [
|
|
|
|
|
"shopify",
|
|
|
|
|
// "woocommerce",
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// Supported import types per source. Only "export" is wired up for now.
|
|
|
|
|
private const TYPES = [
|
|
|
|
|
"export",
|
|
|
|
|
// "api",
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
public function handle(): void
|
|
|
|
|
{
|
|
|
|
|
$source = $this->option("source")
|
|
|
|
|
?? $this->choice("Select a source:", self::SOURCES, 0);
|
|
|
|
|
|
|
|
|
|
$type = $this->option("type")
|
|
|
|
|
?? $this->choice("Select an import type:", self::TYPES, 0);
|
|
|
|
|
|
|
|
|
|
if ($type === "api") {
|
|
|
|
|
$credentials = [
|
|
|
|
|
"api_key" => $this->secret("API key:"),
|
|
|
|
|
"api_secret" => $this->secret("API secret:"),
|
|
|
|
|
];
|
|
|
|
|
$filePath = null;
|
|
|
|
|
} else {
|
2026-07-09 00:37:14 +03:00
|
|
|
$importsPath = storage_path("app/private/imports");
|
|
|
|
|
|
2026-07-06 03:42:24 +03:00
|
|
|
$filePath = $this->option("file")
|
2026-07-09 00:37:14 +03:00
|
|
|
?? $this->resolveImportPath($importsPath, $this->ask("Path to the export file, relative to {$importsPath}:"));
|
|
|
|
|
|
|
|
|
|
while (blank($filePath) || ! is_file($filePath)) {
|
|
|
|
|
if (filled($filePath)) {
|
|
|
|
|
$this->error("File not found: {$filePath}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$filePath = $this->resolveImportPath(
|
|
|
|
|
$importsPath,
|
|
|
|
|
$this->ask("Path to the export file, relative to {$importsPath}:"),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-06 03:42:24 +03:00
|
|
|
$credentials = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$spec = new ImportSpec(
|
|
|
|
|
source: $source,
|
|
|
|
|
type: $type,
|
|
|
|
|
filePath: $filePath,
|
|
|
|
|
credentials: $credentials,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
RunMigrateImportJob::dispatch($spec);
|
|
|
|
|
|
|
|
|
|
$this->info("Import queued.");
|
|
|
|
|
}
|
2026-07-09 00:37:14 +03:00
|
|
|
|
|
|
|
|
// Answers are relative to storage/app/private/imports (e.g. "shopify" or
|
|
|
|
|
// "shopify/products_export.csv"); absolute paths are used as-is. A
|
|
|
|
|
// directory answer picks the first CSV file found inside it.
|
|
|
|
|
private function resolveImportPath(string $importsPath, ?string $answer): ?string
|
|
|
|
|
{
|
|
|
|
|
if (blank($answer)) {
|
|
|
|
|
return $answer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$path = str_starts_with($answer, "/") ? $answer : "{$importsPath}/{$answer}";
|
|
|
|
|
|
|
|
|
|
if (is_dir($path)) {
|
|
|
|
|
$csv = collect(glob("{$path}/*.csv"))->first();
|
|
|
|
|
|
|
|
|
|
return $csv ?? $path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $path;
|
|
|
|
|
}
|
2026-07-06 03:42:24 +03:00
|
|
|
}
|