From fcbd2aa9c03a5d4c4b9c96ebba5ee0a90257f32d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Fri, 21 Aug 2026 09:55:09 +0200 Subject: [PATCH] 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. --- .../InMemoryIncoSheetDirectory.php | 106 ++++++++++++------ 1 file changed, 73 insertions(+), 33 deletions(-) diff --git a/src/Infra/Adapters/Directories/InMemoryIncoSheetDirectory.php b/src/Infra/Adapters/Directories/InMemoryIncoSheetDirectory.php index 73be5f5..cf5a9d3 100644 --- a/src/Infra/Adapters/Directories/InMemoryIncoSheetDirectory.php +++ b/src/Infra/Adapters/Directories/InMemoryIncoSheetDirectory.php @@ -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; + }; + } }