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
|
|
|
|
|
{
|
2026-07-10 15:19:48 +03:00
|
|
|
protected $signature = 'boboko:migrate:import
|
2026-07-06 03:42:24 +03:00
|
|
|
{--source= : The source platform to import from}
|
|
|
|
|
{--type= : The import mechanism for the chosen source}
|
2026-07-10 15:19:48 +03:00
|
|
|
{--file= : Path to the export file (when type is export)}';
|
2026-07-06 03:42:24 +03:00
|
|
|
|
2026-07-10 15:19:48 +03:00
|
|
|
protected $description = 'Migrate data (products, customers, etc.) from an external source';
|
2026-07-06 03:42:24 +03:00
|
|
|
|
2026-07-10 15:19:48 +03:00
|
|
|
// Supported sources. Only "shopify" and "judgeme" are wired up for now.
|
2026-07-06 03:42:24 +03:00
|
|
|
private const SOURCES = [
|
2026-07-10 15:19:48 +03:00
|
|
|
'shopify',
|
|
|
|
|
'judgeme',
|
2026-07-06 03:42:24 +03:00
|
|
|
// "woocommerce",
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// Supported import types per source. Only "export" is wired up for now.
|
|
|
|
|
private const TYPES = [
|
2026-07-10 15:19:48 +03:00
|
|
|
'export',
|
2026-07-06 03:42:24 +03:00
|
|
|
// "api",
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
public function handle(): void
|
|
|
|
|
{
|
2026-07-10 15:19:48 +03:00
|
|
|
$source = $this->option('source')
|
|
|
|
|
?? $this->choice('Select a source:', self::SOURCES, 0);
|
2026-07-06 03:42:24 +03:00
|
|
|
|
2026-07-10 15:19:48 +03:00
|
|
|
$type = $this->option('type')
|
|
|
|
|
?? $this->choice('Select an import type:', self::TYPES, 0);
|
2026-07-06 03:42:24 +03:00
|
|
|
|
2026-07-10 15:19:48 +03:00
|
|
|
if ($type === 'api') {
|
2026-07-06 03:42:24 +03:00
|
|
|
$credentials = [
|
2026-07-10 15:19:48 +03:00
|
|
|
'api_key' => $this->secret('API key:'),
|
|
|
|
|
'api_secret' => $this->secret('API secret:'),
|
2026-07-06 03:42:24 +03:00
|
|
|
];
|
|
|
|
|
$filePath = null;
|
|
|
|
|
} else {
|
2026-07-10 15:19:48 +03:00
|
|
|
$importsPath = storage_path('app/private/imports');
|
2026-07-09 00:37:14 +03:00
|
|
|
|
2026-07-10 15:19:48 +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);
|
|
|
|
|
|
2026-07-10 15:19:48 +03:00
|
|
|
$this->info('Import queued.');
|
2026-07-06 03:42:24 +03:00
|
|
|
}
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-10 15:19:48 +03:00
|
|
|
$path = str_starts_with($answer, '/') ? $answer : "{$importsPath}/{$answer}";
|
2026-07-09 00:37:14 +03:00
|
|
|
|
|
|
|
|
if (is_dir($path)) {
|
|
|
|
|
$csv = collect(glob("{$path}/*.csv"))->first();
|
|
|
|
|
|
|
|
|
|
return $csv ?? $path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $path;
|
|
|
|
|
}
|
2026-07-06 03:42:24 +03:00
|
|
|
}
|