From fe692e37c373285fbb8f5d72010cb1308584ae28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Falc=C3=B3n?= Date: Wed, 3 Jun 2026 17:39:40 +0200 Subject: [PATCH] refactor(requests): share category cashflow direction resolution (#479) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## What `StoreCategoryRequest` and `UpdateCategoryRequest` had identical 24-line `prepareForValidation()` deriving `cashflow_direction` from category type. Moved to `app/Http/Requests/Concerns/ResolvesCategoryCashflowDirection` trait. ## Stats - **-48 / +43 lines** (net small, removes a whole duplicated logic block that must stay in sync) ## Checks - `php artisan test --filter=Categor` — 104 passed (565 assertions) - `vendor/bin/pint --dirty` — pass Part of duplication-removal series (#475–#478). --- .../ResolvesCategoryCashflowDirection.php | 37 +++++++++++++++++++ .../Settings/StoreCategoryRequest.php | 28 ++------------ .../Settings/UpdateCategoryRequest.php | 28 ++------------ 3 files changed, 43 insertions(+), 50 deletions(-) create mode 100644 app/Http/Requests/Concerns/ResolvesCategoryCashflowDirection.php diff --git a/app/Http/Requests/Concerns/ResolvesCategoryCashflowDirection.php b/app/Http/Requests/Concerns/ResolvesCategoryCashflowDirection.php new file mode 100644 index 00000000..55419017 --- /dev/null +++ b/app/Http/Requests/Concerns/ResolvesCategoryCashflowDirection.php @@ -0,0 +1,37 @@ +input('type')); + + if (in_array($type, [CategoryType::Savings, CategoryType::Investment], true)) { + $this->merge([ + 'cashflow_direction' => CategoryCashflowDirection::Outflow->value, + ]); + + return; + } + + if ($type !== CategoryType::Transfer) { + $this->merge([ + 'cashflow_direction' => CategoryCashflowDirection::Hidden->value, + ]); + + return; + } + + $this->merge([ + 'cashflow_direction' => $this->input('cashflow_direction', CategoryCashflowDirection::Hidden->value), + ]); + } +} diff --git a/app/Http/Requests/Settings/StoreCategoryRequest.php b/app/Http/Requests/Settings/StoreCategoryRequest.php index 8bd2c4c0..921062f9 100644 --- a/app/Http/Requests/Settings/StoreCategoryRequest.php +++ b/app/Http/Requests/Settings/StoreCategoryRequest.php @@ -5,12 +5,15 @@ namespace App\Http\Requests\Settings; use App\Enums\CategoryCashflowDirection; use App\Enums\CategoryColor; use App\Enums\CategoryType; +use App\Http\Requests\Concerns\ResolvesCategoryCashflowDirection; use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Validation\Rule; class StoreCategoryRequest extends FormRequest { + use ResolvesCategoryCashflowDirection; + /** * Determine if the user is authorized to make this request. */ @@ -19,31 +22,6 @@ class StoreCategoryRequest extends FormRequest return true; } - protected function prepareForValidation(): void - { - $type = CategoryType::tryFrom((string) $this->input('type')); - - if (in_array($type, [CategoryType::Savings, CategoryType::Investment], true)) { - $this->merge([ - 'cashflow_direction' => CategoryCashflowDirection::Outflow->value, - ]); - - return; - } - - if ($type !== CategoryType::Transfer) { - $this->merge([ - 'cashflow_direction' => CategoryCashflowDirection::Hidden->value, - ]); - - return; - } - - $this->merge([ - 'cashflow_direction' => $this->input('cashflow_direction', CategoryCashflowDirection::Hidden->value), - ]); - } - /** * Get the validation rules that apply to the request. * diff --git a/app/Http/Requests/Settings/UpdateCategoryRequest.php b/app/Http/Requests/Settings/UpdateCategoryRequest.php index 4fb35df8..a9be4cd7 100644 --- a/app/Http/Requests/Settings/UpdateCategoryRequest.php +++ b/app/Http/Requests/Settings/UpdateCategoryRequest.php @@ -5,12 +5,15 @@ namespace App\Http\Requests\Settings; use App\Enums\CategoryCashflowDirection; use App\Enums\CategoryColor; use App\Enums\CategoryType; +use App\Http\Requests\Concerns\ResolvesCategoryCashflowDirection; use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Validation\Rule; class UpdateCategoryRequest extends FormRequest { + use ResolvesCategoryCashflowDirection; + /** * Determine if the user is authorized to make this request. */ @@ -19,31 +22,6 @@ class UpdateCategoryRequest extends FormRequest return true; } - protected function prepareForValidation(): void - { - $type = CategoryType::tryFrom((string) $this->input('type')); - - if (in_array($type, [CategoryType::Savings, CategoryType::Investment], true)) { - $this->merge([ - 'cashflow_direction' => CategoryCashflowDirection::Outflow->value, - ]); - - return; - } - - if ($type !== CategoryType::Transfer) { - $this->merge([ - 'cashflow_direction' => CategoryCashflowDirection::Hidden->value, - ]); - - return; - } - - $this->merge([ - 'cashflow_direction' => $this->input('cashflow_direction', CategoryCashflowDirection::Hidden->value), - ]); - } - /** * Get the validation rules that apply to the request. *