91 lines
2.4 KiB
PHP
91 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Application\Usecase\IncoSheets\Search;
|
|
|
|
use Domain\Models\Entities\Inco\IncoSheet;
|
|
use Domain\Models\Entities\Inco\IncoShortSheet;
|
|
use Domain\Ports\Directories\IncoSheetDirectory;
|
|
use Domain\Ports\Directories\IncoShortSheetDirectory;
|
|
use Shared\DDD\Usecase\Input as BaseInput;
|
|
use Shared\DDD\Usecase\Usecase;
|
|
use Shared\Paginated;
|
|
use Shared\Pagination;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
/** @implements Usecase<\Shared\DDD\Usecase\Input, \Shared\DDD\Usecase\Output> */
|
|
final readonly class SearchIncoSheets implements Usecase
|
|
{
|
|
public function __construct(
|
|
private IncoSheetDirectory $directory,
|
|
private IncoShortSheetDirectory $shortDirectory,
|
|
) {
|
|
}
|
|
|
|
#[\Override]
|
|
public function execute(BaseInput $input): Output
|
|
{
|
|
Assert::isInstanceOf($input, Input::class);
|
|
|
|
$pagination = $input->pagination ?? Pagination::firstPage();
|
|
|
|
if ($input->short) {
|
|
$shortPage = $this->shortDirectory->search(
|
|
$input->query,
|
|
$pagination,
|
|
...$input->brands
|
|
);
|
|
|
|
return $this->toShortOutput($shortPage);
|
|
}
|
|
|
|
$page = $this->directory->search(
|
|
$input->query,
|
|
$pagination,
|
|
...$input->brands
|
|
);
|
|
|
|
return $this->toFullOutput($page);
|
|
}
|
|
|
|
/**
|
|
* @param Paginated<IncoSheet> $resultPage
|
|
*/
|
|
private function toFullOutput(Paginated $resultPage): Output
|
|
{
|
|
$pagination = $resultPage->getPagination();
|
|
|
|
$items = array_map(
|
|
static fn (IncoSheet $sheet) => PaginatedIncoSheet::fromDomain($sheet),
|
|
$resultPage->getItems()
|
|
);
|
|
|
|
return new Output(
|
|
incoSheets: $items,
|
|
total: $resultPage->getTotal(),
|
|
page: $pagination->pageNumber(),
|
|
perPage: $pagination->pageSize(),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param Paginated<IncoShortSheet> $resultPage
|
|
*/
|
|
private function toShortOutput(Paginated $resultPage): Output
|
|
{
|
|
$pagination = $resultPage->getPagination();
|
|
|
|
$items = array_map(
|
|
static fn (IncoShortSheet $sheet) => PaginatedShortIncoSheet::fromDomain($sheet),
|
|
$resultPage->getItems()
|
|
);
|
|
|
|
return new Output(
|
|
incoSheets: $items,
|
|
total: $resultPage->getTotal(),
|
|
page: $pagination->pageNumber(),
|
|
perPage: $pagination->pageSize(),
|
|
);
|
|
}
|
|
}
|