urlsnail/app/Console/Commands/ImportBookmarks.php

45 lines
1011 B
PHP
Raw Normal View History

<?php
namespace App\Console\Commands;
2024-05-26 21:53:09 +00:00
use App\Jobs\ImportBookmark;
use Illuminate\Console\Command;
2024-05-26 21:53:09 +00:00
use RuntimeException;
class ImportBookmarks extends Command
{
2024-05-26 21:53:09 +00:00
static $perBatch = 100;
/**
* The name and signature of the console command.
*
* @var string
*/
2024-05-26 21:53:09 +00:00
protected $signature = 'app:import-bookmarks {json_file}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle()
{
2024-05-26 21:53:09 +00:00
$json_file_path = $this->argument('json_file');
if (!file_exists($json_file_path)) {
throw new RuntimeException('Unable to find JSON file');
}
2024-05-26 21:53:09 +00:00
$json = file_get_contents($json_file_path);
$bookmarks = json_decode($json, true);
2024-05-26 21:53:09 +00:00
foreach (array_chunk($bookmarks, self::$perBatch) as $bookmarks_json) {
ImportBookmark::dispatch(...$bookmarks_json);
}
}
}