Files
movies/app/Console/Commands/ItemProcessing/FetchItemDataCommand.php
T

79 lines
2.1 KiB
PHP

<?php
namespace App\Console\Commands\ItemProcessing;
use App\Models\ItemToFetch;
use App\Service\MediaProcessing\FactoryService;
use Illuminate\Console\Command;
class FetchItemDataCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'processing:fetch-item-data {type=movie} {batch=50} {job=Details}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
private FactoryService $factoryService;
public function __construct(FactoryService $factoryService)
{
$this->factoryService = $factoryService;
parent::__construct();
}
/**
* Execute the console command.
*/
public function handle()
{
$type = $this->argument('type');
$batch = $this->argument('batch');
$job = $this->argument('job');
$items = $this->getBatch($type, $batch, $job);
$this->processItems($items, $job);
}
private function getBatch(string $type, int $batch, string $job)
{
return ItemToFetch::has('jobs')
->with([
// 'jobs' => fn ($jobs) => dd($jobs->first()->remaining_jobs) /*array_search($job, $jobs->first()->remaining_jobs, true)*/,
'jobs' => function ($jobs) use ($job) {
$remainingJobs = $jobs->first()->remaining_jobs;
if (is_string($remainingJobs)) {
$remainingJobs = json_decode($jobs->first()->remaining_jobs, true);
}
return array_search($job, $remainingJobs, true);
},
])
->where('type', $type)
->take($batch)
->get();
}
private function processItems(\Illuminate\Database\Eloquent\Collection $items, $job)
{
foreach ($items as $item) {
$this->processItem($item, $job);
}
}
private function processItem($item, $job)
{
$this->factoryService->process($item, $job);
}
}