jikan-rest/app/Features/QueryTopCharactersHandler.php
pushrbx a145f18bbd wip - major refactor
- AppServiceProvider is needs more work to wire in new services
- todo: more dtos
- todo: add unit tests
- todo: add more integration tests
2023-05-21 11:14:34 +01:00

44 lines
1.2 KiB
PHP

<?php
namespace App\Features;
use App\Contracts\CharacterRepository;
use App\Contracts\RequestHandler;
use App\Dto\QueryTopCharactersCommand;
use App\Http\Resources\V4\CharacterCollection;
use App\Services\QueryBuilderPaginatorService;
/**
* @implements RequestHandler<QueryTopCharactersCommand, CharacterCollection>
*/
final class QueryTopCharactersHandler implements RequestHandler
{
public function __construct(
private readonly CharacterRepository $repository,
private readonly QueryBuilderPaginatorService $paginatorService
)
{
}
/**
* @param QueryTopCharactersCommand $request
* @return CharacterCollection
*/
public function handle($request): CharacterCollection
{
$requestParams = collect($request->all());
$topItemsQuery = $this->repository->topCharacters()->filter($requestParams);
$results = $this->paginatorService->paginate(
$topItemsQuery, $requestParams->get("limit"), $requestParams->get("page")
);
return new CharacterCollection($results);
}
public function requestClass(): string
{
return QueryTopCharactersCommand::class;
}
}