Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
UpdateCategoryHandler
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 handle
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace App\Expense\Application\Command;
6
7use App\Expense\Application\Query\CategoryView;
8use App\Expense\Domain\Category;
9use App\Expense\Domain\CategorySetting;
10use App\Expense\Domain\CategorySettingRepository;
11use App\Expense\Domain\Exceptions\CategoryNotFound;
12
13final readonly class UpdateCategoryHandler
14{
15    public function __construct(private CategorySettingRepository $settings)
16    {
17    }
18
19    /**
20     * Upserts the overlay row: a category edited for the first time has none yet.
21     * The value set is the enum's — this never creates a category, only curates one.
22     *
23     * @return array{value: string, label: string, icon: string, color: string, active: bool}
24     */
25    public function handle(UpdateCategoryCommand $command): array
26    {
27        $category = Category::tryFrom($command->category)
28            ?? throw CategoryNotFound::withValue($command->category);
29
30        $setting = $this->settings->ofCategory($category) ?? CategorySetting::for($category);
31        $setting->change($command->label, $command->icon, $command->color, $command->active);
32        $this->settings->save($setting);
33
34        return CategoryView::of($category, $setting);
35    }
36}