2024-05-24 23:48:23 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
2024-05-26 21:53:09 +00:00
|
|
|
use App\Jobs\ImportBookmark;
|
2024-05-24 23:48:23 +00:00
|
|
|
use Illuminate\Console\Command;
|
2024-05-26 21:53:09 +00:00
|
|
|
use RuntimeException;
|
2024-05-24 23:48:23 +00:00
|
|
|
|
|
|
|
class ImportBookmarks extends Command
|
|
|
|
{
|
2024-05-26 21:53:09 +00:00
|
|
|
static $perBatch = 100;
|
|
|
|
|
2024-05-24 23:48:23 +00:00
|
|
|
/**
|
|
|
|
* 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}';
|
2024-05-24 23:48:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-24 23:48:23 +00:00
|
|
|
|
2024-05-26 21:53:09 +00:00
|
|
|
$json = file_get_contents($json_file_path);
|
|
|
|
$bookmarks = json_decode($json, true);
|
2024-05-24 23:48:23 +00:00
|
|
|
|
2024-05-26 21:53:09 +00:00
|
|
|
foreach (array_chunk($bookmarks, self::$perBatch) as $bookmarks_json) {
|
|
|
|
ImportBookmark::dispatch(...$bookmarks_json);
|
2024-05-24 23:48:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|