ldc-cocotte/app/Cqs/Queries/Slides/GetPaginatedSlidesQuery.php
Jérémy Munsch d0efb0f527
refactor: update namespaces for actions and queries
Refactored the codebase to change the namespaces of various actions and queries from `App\Actions` to `App\Cqs\Actions` and `App\Queries` to `App\Cqs\Queries`. This restructuring improves organization and clarity in the codebase.
2026-11-10 15:13:26 +01:00

58 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Cqs\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 Illuminate\Support\Facades\Date;
use Webmozart\Assert\Assert;
/**
* phpcs:disable Generic.Files.LineLength.TooLong
*
* @implements \App\Contracts\Query<\App\Cqs\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) {
$now = Date::now();
$query->where(function ($q) use ($now): void {
$q->whereNull('date_start')
->orWhere('date_start', '<=', $now);
})->where(function ($q) use ($now): void {
$q->whereNull('date_end')
->orWhere('date_end', '>=', $now);
});
}
if ($input->forConnectedCustomers === false) {
$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));
}
}