103 lines
2.9 KiB
PHP
103 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Service\MediaProcessing;
|
|
|
|
use App\Models\Episode;
|
|
use App\Models\Season;
|
|
use App\Models\Show;
|
|
use App\Service\TMDBApi\ShowsApi;
|
|
use Illuminate\Support\Str;
|
|
use function PHPUnit\Framework\containsIdentical;
|
|
|
|
class ShowProcessingService implements ProcessingInterface
|
|
{
|
|
private ShowsApi $showsApi;
|
|
|
|
public function __construct(ShowsApi $showsApi)
|
|
{
|
|
$this->showsApi = $showsApi;
|
|
}
|
|
|
|
public function process($item, $job)
|
|
{
|
|
$methodName = "get".ucfirst($job);
|
|
|
|
if (!method_exists($this, $methodName)) {
|
|
// @todo maybe raise an exception here
|
|
return;
|
|
}
|
|
|
|
$this->$methodName($item);
|
|
}
|
|
|
|
private function getDetails($item)
|
|
{
|
|
$tmdbShow = $this->showsApi->getTvshow($item->tmdb_id);
|
|
$tmdbShow['tmdb_id'] = $tmdbShow['id'];
|
|
|
|
$show = Show::where('tmdb_id', $item->tmdb_id)->first();
|
|
|
|
if ($show) {
|
|
return;
|
|
}
|
|
|
|
$tmdbShow['slug'] = Str::slug($tmdbShow['name'].'-'.$tmdbShow['tmdb_id']);
|
|
$tmdbShow['first_air_date'] = $tmdbShow['first_air_date'] ? new \DateTime($tmdbShow['first_air_date']) : null;
|
|
$tmdbShow['last_air_date'] = $tmdbShow['last_air_date'] ? new \DateTime($tmdbShow['last_air_date']) : null;
|
|
|
|
$show = Show::create($tmdbShow);
|
|
|
|
if (!empty($tmdbShow['seasons'])) {
|
|
$this->processSeasons($show, $tmdbShow['seasons']);
|
|
}
|
|
|
|
// @todo remove jobs from db
|
|
}
|
|
|
|
private function getAlternativeTitles()
|
|
{
|
|
|
|
}
|
|
|
|
private function processSeasons(Show $show, array $seasons)
|
|
{
|
|
foreach ($seasons as $season) {
|
|
$showSeason = Season::where('tmdb_id', $season['id'])
|
|
->where('show_id', $show->id)
|
|
->first();
|
|
|
|
if (!$showSeason) {
|
|
$showSeason = Season::create($season);
|
|
|
|
$tmdbSeasonDetails = $this->showsApi->getSeasonDetails($show->tmdb_id, $season['season_number']);
|
|
|
|
$this->processEpisodes($show, $showSeason, $tmdbSeasonDetails['episodes'] ?? []);
|
|
}
|
|
}
|
|
}
|
|
|
|
private function processEpisodes($show, $season, array $tmdbEpisodes)
|
|
{
|
|
if (empty($tmdbEpisodes)) {
|
|
return;
|
|
}
|
|
|
|
foreach ($tmdbEpisodes as $tmdbEpisode) {
|
|
$episode = Episode::where('show_id', $show->id)
|
|
->where('season_id', $season->id)
|
|
->where('tmdb_id', $tmdbEpisode['id'])
|
|
->first();
|
|
|
|
if (!$episode) {
|
|
$tmdbEpisode['tmdb_id'] = $tmdbEpisode['id'];
|
|
$tmdbEpisode['slug'] = Str::slug($tmdbEpisode['name']);
|
|
$tmdbEpisode['tmdb_show_id'] = $tmdbEpisode['show_id'];
|
|
$tmdbEpisode['show_id'] = $show->id;
|
|
$tmdbEpisode['tmdb_id'] = $season->id;
|
|
|
|
Episode::create($tmdbEpisode);
|
|
}
|
|
}
|
|
}
|
|
}
|