Skip to content

Commit

Permalink
Revert "revert: HDInnovations#3875"
Browse files Browse the repository at this point in the history
This reverts commit 8ef1860.
  • Loading branch information
Roardom committed May 27, 2024
1 parent 03f3c63 commit 37a7621
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions app/Traits/TorrentMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, \App\Models\Torrent>|\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<int, \App\Models\Torrent> ? \Illuminate\Support\Collection<int, \App\Models\Torrent> : ($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');
Expand All @@ -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);
}
}

0 comments on commit 37a7621

Please sign in to comment.