diff --git a/app/Traits/TorrentMeta.php b/app/Traits/TorrentMeta.php index 6932c560e8..b9a8d28ab2 100644 --- a/app/Traits/TorrentMeta.php +++ b/app/Traits/TorrentMeta.php @@ -18,11 +18,22 @@ use App\Models\Movie; use App\Models\Tv; +use JsonException; use MarcReichel\IGDBLaravel\Models\Game; +use ReflectionException; trait TorrentMeta { - public function scopeMeta($torrents): \Illuminate\Pagination\LengthAwarePaginator|\Illuminate\Database\Eloquent\Collection + /** + * @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Pagination\CursorPaginator<\App\Models\Torrent>|\Illuminate\Pagination\LengthAwarePaginator<\App\Models\Torrent> $torrents + * + * @throws \MarcReichel\IGDBLaravel\Exceptions\MissingEndpointException + * @throws \MarcReichel\IGDBLaravel\Exceptions\InvalidParamsException + * @throws ReflectionException + * @throws JsonException + * @return ($torrents is \Illuminate\Database\Eloquent\Collection ? \Illuminate\Support\Collection : ($torrents is \Illuminate\Pagination\CursorPaginator<\App\Models\Torrent> ? \Illuminate\Pagination\CursorPaginator<\App\Models\Torrent> : \Illuminate\Pagination\LengthAwarePaginator<\App\Models\Torrent>)) + */ + public function scopeMeta(\Illuminate\Database\Eloquent\Collection|\Illuminate\Pagination\CursorPaginator|\Illuminate\Pagination\LengthAwarePaginator $torrents): \Illuminate\Support\Collection|\Illuminate\Pagination\CursorPaginator|\Illuminate\Pagination\LengthAwarePaginator { $movieIds = $torrents->where('meta', '=', 'movie')->pluck('tmdb'); $tvIds = $torrents->where('meta', '=', 'tv')->pluck('tmdb'); @@ -33,18 +44,27 @@ public function scopeMeta($torrents): \Illuminate\Pagination\LengthAwarePaginato $games = []; foreach ($gameIds as $gameId) { - $games[$gameId] = Game::with(['cover' => ['url', 'image_id']])->find($gameId); + $games[$gameId] = Game::with(['cover' => ['url', 'image_id'], 'genres' => ['name']])->find($gameId); } - return $torrents->map(function ($torrent) use ($movies, $tv, $games) { - $torrent->meta = match ($torrent->meta) { - 'movie' => $movies[$torrent->tmdb] ?? null, - 'tv' => $tv[$torrent->tmdb] ?? null, - 'game' => $games[$torrent->igdb] ?? null, - default => null, - }; + $setRelation = function ($torrent) use ($movies, $tv, $games) { + $torrent->setRelation( + 'meta', + match ($torrent->meta) { + 'movie' => $movies[$torrent->tmdb] ?? null, + 'tv' => $tv[$torrent->tmdb] ?? null, + 'game' => $games[$torrent->igdb] ?? null, + default => null, + }, + ); return $torrent; - }); + }; + + if ($torrents instanceof \Illuminate\Database\Eloquent\Collection) { + return $torrents->map($setRelation); + } + + return $torrents->through($setRelation); } }