47 lines
1.2 KiB
PHP
47 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
require __DIR__ . '/../bootstrap.php';
|
|
|
|
if (PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg') {
|
|
fwrite(STDERR, "This script must be run from the command line.\n");
|
|
exit(1);
|
|
}
|
|
|
|
$token = (string) ($argv[1] ?? '');
|
|
if ($token === '') {
|
|
fwrite(STDERR, "Missing import token.\n");
|
|
exit(1);
|
|
}
|
|
|
|
@set_time_limit(0);
|
|
@ini_set('memory_limit', '-1');
|
|
|
|
$database = \App\Database::fromConfig($GLOBALS['appConfig'] ?? []);
|
|
$repository = \App\Import\ImportRepository::fromDatabase($database);
|
|
$jobStore = new \App\Import\ImportJobStore();
|
|
$service = new \App\Import\ImportService(
|
|
new \App\Import\SpreadsheetReader(),
|
|
new \App\Import\SpreadsheetRowProcessor(),
|
|
$repository,
|
|
);
|
|
|
|
$state = $jobStore->get($token);
|
|
if (!is_array($state)) {
|
|
fwrite(STDERR, "Import job not found.\n");
|
|
exit(1);
|
|
}
|
|
|
|
while (!in_array(($state['status'] ?? 'pending'), ['completed', 'error'], true)) {
|
|
$state = $service->processJobState($state);
|
|
$jobStore->save($token, $state);
|
|
}
|
|
|
|
if (($state['status'] ?? '') === 'error') {
|
|
fwrite(STDERR, (string) ($state['message'] ?? 'Import failed.') . "\n");
|
|
exit(1);
|
|
}
|
|
|
|
fwrite(STDOUT, "Import completed.\n");
|