Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| ListCategoriesHandler | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| handle | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Expense\Application\Query; |
| 6 | |
| 7 | use App\Expense\Domain\Category; |
| 8 | use App\Expense\Domain\CategorySettingRepository; |
| 9 | |
| 10 | final readonly class ListCategoriesHandler |
| 11 | { |
| 12 | public function __construct(private CategorySettingRepository $settings) |
| 13 | { |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * The enum drives existence, the overlay only tweaks presentation: iterate |
| 18 | * Category::cases() and merge any override row on top. A category with no row |
| 19 | * reads its code defaults and is active. |
| 20 | * |
| 21 | * @return list<array{value: string, label: string, icon: string, color: string, active: bool}> |
| 22 | */ |
| 23 | public function handle(ListCategoriesQuery $query): array |
| 24 | { |
| 25 | $overrides = $this->settings->all(); |
| 26 | |
| 27 | $categories = []; |
| 28 | foreach (Category::cases() as $category) { |
| 29 | $view = CategoryView::of($category, $overrides[$category->value] ?? null); |
| 30 | |
| 31 | if ($view['active'] || $query->includeInactive) { |
| 32 | $categories[] = $view; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | return $categories; |
| 37 | } |
| 38 | } |