[#7] upcoming
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
<?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) {
|
||||
$movie = $this->process($upcomingMovie);
|
||||
|
||||
if ($movie) {
|
||||
$newUpcomingIds[] = [
|
||||
'movie_id' => $movie->id
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($newUpcomingIds)) {
|
||||
UpcomingMovie::whereIn('id', $oldUpcomingIds)->delete();
|
||||
UpcomingMovie::insert($newUpcomingIds);
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
} catch (\Throwable $e) {
|
||||
DB::rollBack();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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', $type)->first();
|
||||
|
||||
if ($itemToFetch) {
|
||||
$this->movieProcessingService->process($itemToFetch, ItemToFetchJobs::JOB_DETAILS);
|
||||
return Movie::where('tmdb_id', $upcomingMovie['id'])->first();
|
||||
}
|
||||
|
||||
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),
|
||||
]);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user