117 lines
3.2 KiB
PHP
117 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands\ItemProcessing;
|
|
|
|
use App\Models\ItemToFetch;
|
|
use App\Models\ItemToFetchJobs;
|
|
use App\Models\Movie;
|
|
use App\Models\UpcomingMovie;
|
|
use App\Service\MediaProcessing\MovieProcessingService;
|
|
use App\Service\TMDBApi\MoviesApi;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class FetchUpcomingMoviesCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'processing:fetch-upcoming-movies';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Fetch Upcoming movies.';
|
|
|
|
private MoviesApi $moviesApi;
|
|
private MovieProcessingService $movieProcessingService;
|
|
|
|
public function __construct(
|
|
MoviesApi $moviesApi,
|
|
MovieProcessingService $movieProcessingService
|
|
)
|
|
{
|
|
$this->moviesApi = $moviesApi;
|
|
$this->movieProcessingService = $movieProcessingService;
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle()
|
|
{
|
|
$upcomingMovies = $this->moviesApi->getUpcoming();
|
|
|
|
if (empty($upcomingMovies) || empty($upcomingMovies['results'])) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
$newUpcomingIds = [];
|
|
$oldUpcomingIds = UpcomingMovie::all()->pluck('movie_id');
|
|
|
|
foreach ($upcomingMovies['results'] as $upcomingMovie) {
|
|
echo ".";
|
|
$movie = $this->process($upcomingMovie);
|
|
|
|
if ($movie) {
|
|
$newUpcomingIds[] = [
|
|
'movie_id' => $movie->id
|
|
];
|
|
}
|
|
}
|
|
|
|
if (!empty($newUpcomingIds)) {
|
|
UpcomingMovie::whereIn('id', $oldUpcomingIds)->delete();
|
|
UpcomingMovie::insert($newUpcomingIds);
|
|
echo "+";
|
|
}
|
|
|
|
DB::commit();
|
|
} catch (\Throwable $e) {
|
|
DB::rollBack();
|
|
dump($e);
|
|
}
|
|
|
|
}
|
|
|
|
private function process(array $upcomingMovie)
|
|
{
|
|
$movie = Movie::where('tmdb_id', $upcomingMovie['id'])->first();
|
|
|
|
if ($movie) {
|
|
return $movie;
|
|
}
|
|
|
|
$itemToFetch = ItemToFetch::where('tmdb_id', $upcomingMovie['id'])->where('type', ItemToFetch::TYPE_MOVIE)->first();
|
|
|
|
if ($itemToFetch) {
|
|
$this->movieProcessingService->process($itemToFetch, ItemToFetchJobs::JOB_DETAILS);
|
|
return Movie::where('tmdb_id', $upcomingMovie['id'])->first();
|
|
}
|
|
|
|
$itemToFetch = ItemToFetch::create([
|
|
'tmdb_id' => $upcomingMovie['id'],
|
|
'type' => ItemToFetch::TYPE_MOVIE,
|
|
]);
|
|
|
|
$jobs = ItemToFetchJobs::JOBS;
|
|
$jobs[] = "Details";
|
|
|
|
ItemToFetchJobs::create([
|
|
'tmdb_id' => $upcomingMovie['id'],
|
|
'type' => ItemToFetch::TYPE_MOVIE,
|
|
'remaining_jobs' => json_encode($jobs),
|
|
]);
|
|
|
|
$this->movieProcessingService->process($itemToFetch, ItemToFetchJobs::JOB_DETAILS);
|
|
return Movie::where('tmdb_id', $upcomingMovie['id'])->first();
|
|
}
|
|
}
|