40 lines
1.3 KiB
PHP
40 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\Movies\Services\Browse\Entities;
|
|
|
|
use App\Modules\Movies\Models\{Actor, Movie};
|
|
use App\Modules\Movies\Services\Contracts\GetActorWithMoviesServiceInterface;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
|
|
class GetActorWithMoviesService implements GetActorWithMoviesServiceInterface
|
|
{
|
|
/**
|
|
* @param int $id
|
|
* @param array{q?: string|null, per_page?: int|null} $params
|
|
* @return array{entity: Actor, movies: LengthAwarePaginator}
|
|
*/
|
|
public function handle(int $id, array $params = []): array
|
|
{
|
|
$actor = Actor::query()->find($id);
|
|
if (!$actor) {
|
|
throw (new ModelNotFoundException())->setModel(Actor::class, [$id]);
|
|
}
|
|
|
|
$perPage = max(1, min((int)($params['per_page'] ?? 20), 50));
|
|
$q = isset($params['q']) ? trim((string)$params['q']) : '';
|
|
|
|
$movies = Movie::query()
|
|
->whereHas('actors', fn($q2) => $q2->whereKey($actor->id))
|
|
->with(['genres'])
|
|
->when($q !== '', fn($query) => $query->where('title', 'like', "%{$q}%"))
|
|
->latest()
|
|
->paginate($perPage);
|
|
|
|
return [
|
|
'entity' => $actor,
|
|
'movies' => $movies,
|
|
];
|
|
}
|
|
}
|