55 lines
1.9 KiB
PHP
55 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Queries\Orders;
|
|
|
|
use App\Contracts\Query;
|
|
use App\Contracts\QueryInput;
|
|
use App\Dto\OrderDto;
|
|
use App\Models\Order;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Pagination\LengthAwarePaginator as ConcreteLengthAwarePaginator;
|
|
use SortDirection;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
/**
|
|
* @implements \App\Contracts\Query<
|
|
* \App\Queries\Orders\GetFoCustomerPaginatedOrdersQueryInput,
|
|
* \Illuminate\Contracts\Pagination\LengthAwarePaginator<int, \App\Dto\OrderDto>
|
|
* >
|
|
*/
|
|
final readonly class GetFoCustomerPaginatedOrdersQuery implements Query
|
|
{
|
|
/**
|
|
* @param \App\Queries\Orders\GetFoCustomerPaginatedOrdersQueryInput $input
|
|
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator<int, \App\Dto\OrderDto>
|
|
*/
|
|
#[\Override]
|
|
public function __invoke(QueryInput $input): LengthAwarePaginator
|
|
{
|
|
Assert::isInstanceOf($input, GetFoCustomerPaginatedOrdersQueryInput::class);
|
|
|
|
/** @var \Domain\Models\ValueObjects\Matricule|null $employeeMatricule */
|
|
$employeeMatricule = $input->customer->employee()->value('matricule');
|
|
|
|
if ($employeeMatricule === null) {
|
|
return new ConcreteLengthAwarePaginator(
|
|
items: [],
|
|
total: 0,
|
|
perPage: $input->pagination->pageSize()->value(),
|
|
currentPage: $input->pagination->pageNumber()->value(),
|
|
);
|
|
}
|
|
|
|
$paginator = Order::query()
|
|
->where('employee_matricule', $employeeMatricule->value)
|
|
->orderBy('id', SortDirection::Descending)
|
|
->paginate(
|
|
perPage: $input->pagination->pageSize()->value(),
|
|
page: $input->pagination->pageNumber()->value(),
|
|
);
|
|
|
|
return $paginator->through(fn (Order $order): OrderDto => OrderDto::fromModel($order));
|
|
}
|
|
}
|