refactor(inmemory): simplify filtering and sorting logic
Refactor the findAll method by extracting filtering and sorting logic into dedicated private methods for better readability and maintainability.
This commit is contained in:
parent
fcf184dd1d
commit
fcbd2aa9c0
1 changed files with 73 additions and 33 deletions
|
|
@ -49,42 +49,11 @@ final class InMemoryIncoSheetDirectory implements IncoSheetDirectory
|
|||
$filtered = array_values($this->sheets);
|
||||
|
||||
if ($filter !== null) {
|
||||
$filtered = array_filter($filtered, static function (IncoSheet $sheet) use ($filter): bool {
|
||||
if ($filter->since !== null) {
|
||||
$createdAtMatches = $sheet->createdAt !== null && $sheet->createdAt >= $filter->since;
|
||||
$updatedAtMatches = $sheet->updatedAt !== null && $sheet->updatedAt >= $filter->since;
|
||||
if (! $createdAtMatches && ! $updatedAtMatches) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (! empty($filter->brands)) {
|
||||
$matched = false;
|
||||
foreach ($filter->brands as $brandCode) {
|
||||
if ($sheet->brand->code->value === $brandCode->value) {
|
||||
$matched = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (! $matched) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
$filtered = array_filter($filtered, self::incoSheetFilter($filter));
|
||||
$filtered = array_values($filtered);
|
||||
}
|
||||
|
||||
usort($filtered, static function (IncoSheet $a, IncoSheet $b) use ($sorter): int {
|
||||
$cmp = match ($sorter->sortBy) {
|
||||
IncoSheetSortColumn::incoSheetId => (int) $a->id->value <=> (int) $b->id->value,
|
||||
IncoSheetSortColumn::createdAt => $a->createdAt <=> $b->createdAt,
|
||||
IncoSheetSortColumn::updatedAt => $a->updatedAt <=> $b->updatedAt,
|
||||
};
|
||||
|
||||
return $sorter->direction === \SortDirection::Ascending ? $cmp : -$cmp;
|
||||
});
|
||||
usort($filtered, self::incoSheetSorter($sorter));
|
||||
|
||||
$total = count($filtered);
|
||||
$offset = $pagination->from();
|
||||
|
|
@ -131,4 +100,75 @@ final class InMemoryIncoSheetDirectory implements IncoSheetDirectory
|
|||
|
||||
return new Paginated($pagination, $paged, $total);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Closure(IncoSheet): bool
|
||||
*/
|
||||
private static function incoSheetFilter(IncoSheetFilter $filter): \Closure
|
||||
{
|
||||
return static function (IncoSheet $sheet) use ($filter): bool {
|
||||
if (! self::incoSheetWasModified($filter->since)($sheet)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! self::filterByBrands(...$filter->brands)($sheet)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Closure(IncoSheet): bool
|
||||
*/
|
||||
private static function filterByBrands(BrandCode ...$brands): \Closure
|
||||
{
|
||||
return static function (IncoSheet $sheet) use ($brands): bool {
|
||||
if (empty($brands)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach ($brands as $brandCode) {
|
||||
if ($sheet->brand->code->value === $brandCode->value) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Closure(IncoSheet): bool
|
||||
*/
|
||||
private static function incoSheetWasModified(?\DateTimeInterface $since): \Closure
|
||||
{
|
||||
return static function (IncoSheet $sheet) use ($since): bool {
|
||||
if ($since === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$createdAtMatches = $sheet->createdAt !== null && $sheet->createdAt >= $since;
|
||||
$updatedAtMatches = $sheet->updatedAt !== null && $sheet->updatedAt >= $since;
|
||||
|
||||
return $createdAtMatches || $updatedAtMatches;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Closure(IncoSheet, IncoSheet): int
|
||||
*/
|
||||
private static function incoSheetSorter(IncoSheetSorter $sorter): \Closure
|
||||
{
|
||||
return static function (IncoSheet $a, IncoSheet $b) use ($sorter): int {
|
||||
$cmp = match ($sorter->sortBy) {
|
||||
IncoSheetSortColumn::incoSheetId => (int) $a->id->value <=> (int) $b->id->value,
|
||||
IncoSheetSortColumn::createdAt => $a->createdAt <=> $b->createdAt,
|
||||
IncoSheetSortColumn::updatedAt => $a->updatedAt <=> $b->updatedAt,
|
||||
};
|
||||
|
||||
return $sorter->direction === \SortDirection::Ascending ? $cmp : -$cmp;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue