ldc-boutique-cnc-sable/app/Queries/Slides/GetPaginatedSlidesQuery.php

50 lines
1.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Queries\Slides;
use App\Contracts\Query;
use App\Contracts\QueryInput;
use App\Dto\SlideDto;
use App\Models\Slide;
use App\Services\DataTableService;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Webmozart\Assert\Assert;
/**
* phpcs:disable Generic.Files.LineLength.TooLong
*
* @implements \App\Contracts\Query<\App\Queries\Slides\GetPaginatedSlidesQueryInput, \Illuminate\Contracts\Pagination\LengthAwarePaginator<int, \App\Dto\SlideDto>>
*/
final readonly class GetPaginatedSlidesQuery implements Query
{
public function __construct(
private DataTableService $dataTable,
) {
}
public function __invoke(QueryInput $input): LengthAwarePaginator
{
Assert::isAOf(
$input,
GetPaginatedSlidesQueryInput::class,
);
$query = Slide::query();
if ($input->publishedOnly) {
$query->where('status', true);
}
if (! $input->forConnectedCustomers) {
$query->where('connected', false);
}
$this->dataTable->sortQuery(Slide::class, $query);
$paginator = $this->dataTable->paginateQuery($query, $input->pagination);
return $paginator->through(fn (Slide $slide): SlideDto => SlideDto::fromModel($slide));
}
}