urlsnail/app/Console/Commands/ImportBookmarks.php

48 lines
1.1 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Console\Commands;
2024-05-26 21:53:09 +00:00
use App\Jobs\ImportBookmark;
2024-05-30 23:01:16 +00:00
use App\Models\User;
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-30 23:01:16 +00:00
protected $signature = 'app:import-bookmarks {user} {json_file}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle()
{
2024-05-30 23:01:16 +00:00
$user = User::find($this->argument('user'));
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) {
2024-05-30 23:01:16 +00:00
ImportBookmark::dispatch($user, ...$bookmarks_json);
}
}
}