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) { // if job <> Details the movie in movie table should exist $query = ItemToFetch::whereHas('jobs', function($query) use ($job){ return $query->where('jobs', '<>', null)->whereRaw('FIND_IN_SET(?, jobs)', [$job]); })->with(['jobs']); if ($job !== ItemToFetchJobs::JOB_DETAILS) { $query = $query->join('movies', 'movies.tmdb_id', '=', 'items_to_fetch.tmdb_id'); } return $query ->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); } }