urlsnail/app/Console/Commands/ImportBookmarks.php

48 lines
1.1 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Jobs\ImportBookmark;
use App\Models\User;
use Illuminate\Console\Command;
use RuntimeException;
class ImportBookmarks extends Command
{
static $perBatch = 100;
/**
* The name and signature of the console command.
*
* @var string
*/
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()
{
$user = User::find($this->argument('user'));
$json_file_path = $this->argument('json_file');
if (!file_exists($json_file_path)) {
throw new RuntimeException('Unable to find JSON file');
}
$json = file_get_contents($json_file_path);
$bookmarks = json_decode($json, true);
foreach (array_chunk($bookmarks, self::$perBatch) as $bookmarks_json) {
ImportBookmark::dispatch($user, ...$bookmarks_json);
}
}
}