Files
riksdagen-app/app/Console/Commands/ImportRiksdagData.php

94 lines
2.9 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Jobs\ImportRiksdagDataJob;
use Illuminate\Console\Command;
class ImportRiksdagData extends Command
{
/**
* The name and signature of the console command.
*/
protected $signature = 'riksdag:import
{file? : The path to the JSON file to import}
{--url= : URL to download ZIP file from}
{--queue : Run the import job in the background queue}';
/**
* The console command description.
*/
protected $description = 'Import Riksdag data from a JSON file or download and process a ZIP file from URL';
/**
* Execute the console command.
*/
public function handle()
{
$filePath = $this->argument('file');
$url = $this->option('url');
if (! $filePath && ! $url) {
$this->error('Either provide a file path or use --url option');
return 1;
}
if ($url) {
// Process ZIP from URL
$this->info("Starting ZIP download and import from: {$url}");
if ($this->option('queue')) {
ImportRiksdagDataJob::dispatch($url, true);
$this->info('ZIP import job has been queued. Check queue workers for progress.');
} else {
try {
$job = new ImportRiksdagDataJob($url, true);
$job->handle();
$this->info('ZIP import completed successfully!');
} catch (\Exception $e) {
$this->error('ZIP import failed: '.$e->getMessage());
return 1;
}
}
} else {
// Process single file
if (! file_exists($filePath)) {
$this->error("File not found: {$filePath}");
return 1;
}
// Validate JSON format
$jsonContent = file_get_contents($filePath);
$testDecode = json_decode($jsonContent, true);
if (json_last_error() !== JSON_ERROR_NONE) {
$this->error('Invalid JSON format: '.json_last_error_msg());
return 1;
}
$this->info("Starting import from: {$filePath}");
if ($this->option('queue')) {
ImportRiksdagDataJob::dispatch($filePath, false);
$this->info('Import job has been queued. Check queue workers for progress.');
} else {
try {
$job = new ImportRiksdagDataJob($filePath, false);
$job->handle();
$this->info('Import completed successfully!');
} catch (\Exception $e) {
$this->error('Import failed: '.$e->getMessage());
return 1;
}
}
}
return 0;
}
}