From d9adb526ce93f15cbe2565d6d2a7af7e09bd5aa6 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Thu, 1 Aug 2024 13:33:28 +0200 Subject: [PATCH] Remove Setting store actions --- app/Form/Actions/SettingStoreAction.php | 46 ------------------------- app/Form/FormSettings.php | 19 +++++++--- app/Invoice/InvoiceSettings.php | 26 ++++++++++++-- app/Invoice/SettingSaveAction.php | 43 ----------------------- app/Module/ModuleSettings.php | 22 +++++++++--- app/Module/ModuleStoreAction.php | 45 ------------------------ app/Nami/Actions/SettingSaveAction.php | 42 ---------------------- app/Setting/Actions/StoreAction.php | 40 +++++++++++++++++++++ app/Setting/Contracts/Storeable.php | 24 ++++++++++--- app/Setting/LocalSettings.php | 4 +-- app/Setting/NamiSettings.php | 27 ++++++++++++--- app/Setting/SettingFactory.php | 13 ++----- app/Setting/SettingServiceProvider.php | 5 ++- resources/js/views/setting/Bill.vue | 2 +- resources/js/views/setting/Form.vue | 4 +-- tests/EndToEnd/MassstoreActionTest.php | 2 -- tests/Feature/Invoice/SettingTest.php | 19 +++++++--- 17 files changed, 161 insertions(+), 222 deletions(-) delete mode 100644 app/Form/Actions/SettingStoreAction.php delete mode 100644 app/Invoice/SettingSaveAction.php delete mode 100644 app/Module/ModuleStoreAction.php delete mode 100644 app/Nami/Actions/SettingSaveAction.php create mode 100644 app/Setting/Actions/StoreAction.php diff --git a/app/Form/Actions/SettingStoreAction.php b/app/Form/Actions/SettingStoreAction.php deleted file mode 100644 index 25f9897a..00000000 --- a/app/Form/Actions/SettingStoreAction.php +++ /dev/null @@ -1,46 +0,0 @@ - $input - */ - public function handle(array $input): void - { - $settings = app(FormSettings::class); - - $settings->fill([ - 'registerUrl' => $input['register_url'], - 'clearCacheUrl' => $input['clear_cache_url'], - ]); - - $settings->save(); - } - - /** - * @return array - */ - public function rules(): array - { - return [ - 'register_url' => 'present|string', - 'clear_cache_url' => 'present|string', - ]; - } - - public function asController(ActionRequest $request): RedirectResponse - { - $this->handle($request->validated()); - - return redirect()->back(); - } -} diff --git a/app/Form/FormSettings.php b/app/Form/FormSettings.php index 59932c78..9f4cdb57 100644 --- a/app/Form/FormSettings.php +++ b/app/Form/FormSettings.php @@ -5,6 +5,7 @@ namespace App\Form; use App\Form\Actions\SettingStoreAction; use App\Setting\Contracts\Storeable; use App\Setting\LocalSettings; +use Lorisleiva\Actions\ActionRequest; class FormSettings extends LocalSettings implements Storeable { @@ -21,9 +22,19 @@ class FormSettings extends LocalSettings implements Storeable return 'Formulare'; } - public static function storeAction(): string + /** + * @inheritdoc + */ + public function rules(): array + { + return [ + 'registerUrl' => 'present|string', + 'clearCacheUrl' => 'present|string', + ]; + } + + public function beforeSave(ActionRequest $request): void { - return SettingStoreAction::class; } /** @@ -34,8 +45,8 @@ class FormSettings extends LocalSettings implements Storeable return [ 'data' => [ 'data' => [ - 'register_url' => $this->registerUrl, - 'clear_cache_url' => $this->clearCacheUrl, + 'registerUrl' => $this->registerUrl, + 'clearCacheUrl' => $this->clearCacheUrl, ] ] ]; diff --git a/app/Invoice/InvoiceSettings.php b/app/Invoice/InvoiceSettings.php index 9d44f915..23a30710 100644 --- a/app/Invoice/InvoiceSettings.php +++ b/app/Invoice/InvoiceSettings.php @@ -4,6 +4,7 @@ namespace App\Invoice; use App\Setting\Contracts\Storeable; use App\Setting\LocalSettings; +use Lorisleiva\Actions\ActionRequest; class InvoiceSettings extends LocalSettings implements Storeable { @@ -51,14 +52,33 @@ class InvoiceSettings extends LocalSettings implements Storeable 'zip' => $this->zip, 'iban' => $this->iban, 'bic' => $this->bic, - 'remember_weeks' => $this->rememberWeeks, + 'rememberWeeks' => $this->rememberWeeks, ] ]; } - public static function storeAction(): string + /** + * @inheritdoc + */ + public function rules(): array + { + return [ + 'from_long' => '', + 'from' => '', + 'mobile' => '', + 'email' => '', + 'website' => '', + 'address' => '', + 'place' => '', + 'zip' => '', + 'iban' => '', + 'bic' => '', + 'rememberWeeks' => '', + ]; + } + + public function beforeSave(ActionRequest $request): void { - return SettingSaveAction::class; } public static function title(): string diff --git a/app/Invoice/SettingSaveAction.php b/app/Invoice/SettingSaveAction.php deleted file mode 100644 index 38b0ec0f..00000000 --- a/app/Invoice/SettingSaveAction.php +++ /dev/null @@ -1,43 +0,0 @@ - $input - */ - public function handle(array $input): void - { - $settings = app(InvoiceSettings::class); - - $settings->fill([ - 'from_long' => $input['from_long'] ?? '', - 'from' => $input['from'] ?? '', - 'mobile' => $input['mobile'] ?? '', - 'email' => $input['email'] ?? '', - 'website' => $input['website'] ?? '', - 'address' => $input['address'] ?? '', - 'place' => $input['place'] ?? '', - 'zip' => $input['zip'] ?? '', - 'iban' => $input['iban'] ?? '', - 'bic' => $input['bic'] ?? '', - 'rememberWeeks' => $input['remember_weeks'] ?? 1, - ]); - - $settings->save(); - } - - public function asController(ActionRequest $request): RedirectResponse - { - $this->handle($request->all()); - - return redirect()->back(); - } -} diff --git a/app/Module/ModuleSettings.php b/app/Module/ModuleSettings.php index 51117160..45556149 100644 --- a/app/Module/ModuleSettings.php +++ b/app/Module/ModuleSettings.php @@ -4,6 +4,8 @@ namespace App\Module; use App\Setting\Contracts\Storeable; use App\Setting\LocalSettings; +use Illuminate\Validation\Rule; +use Lorisleiva\Actions\ActionRequest; class ModuleSettings extends LocalSettings implements Storeable { @@ -20,16 +22,26 @@ class ModuleSettings extends LocalSettings implements Storeable return 'Module'; } - public static function storeAction(): string - { - return ModuleStoreAction::class; - } - public function hasModule(string $module): bool { return in_array($module, $this->modules); } + public function beforeSave(ActionRequest $request): void + { + } + + /** + * @inheritdoc + */ + public function rules(): array + { + return [ + 'modules' => 'present|array', + 'modules.*' => ['string', Rule::in(Module::values())], + ]; + } + /** * @inheritdoc */ diff --git a/app/Module/ModuleStoreAction.php b/app/Module/ModuleStoreAction.php deleted file mode 100644 index aab3dff2..00000000 --- a/app/Module/ModuleStoreAction.php +++ /dev/null @@ -1,45 +0,0 @@ - $input - */ - public function handle(array $input): void - { - $settings = app(ModuleSettings::class); - - $settings->fill([ - 'modules' => $input['modules'], - ]); - - $settings->save(); - } - - /** - * @return array - */ - public function rules(): array - { - return [ - 'modules' => 'present|array', - 'modules.*' => ['string', Rule::in(Module::values())], - ]; - } - - public function asController(ActionRequest $request): RedirectResponse - { - $this->handle($request->validated()); - - return redirect()->back(); - } -} diff --git a/app/Nami/Actions/SettingSaveAction.php b/app/Nami/Actions/SettingSaveAction.php deleted file mode 100644 index 197cb379..00000000 --- a/app/Nami/Actions/SettingSaveAction.php +++ /dev/null @@ -1,42 +0,0 @@ - $input - */ - public function handle(array $input): void - { - $settings = app(NamiSettings::class); - - $settings->fill([ - 'mglnr' => $input['mglnr'] ?? '', - 'password' => $input['password'] ?? '', - 'default_group_id' => $input['default_group_id'] ?? '', - ]); - - $settings->save(); - } - - public function asController(ActionRequest $request): RedirectResponse - { - NamiLoginCheckAction::run([ - 'mglnr' => $request->mglnr, - 'password' => $request->password, - ]); - - $this->handle($request->all()); - - return redirect()->back(); - } -} diff --git a/app/Setting/Actions/StoreAction.php b/app/Setting/Actions/StoreAction.php new file mode 100644 index 00000000..b7e66600 --- /dev/null +++ b/app/Setting/Actions/StoreAction.php @@ -0,0 +1,40 @@ + $input + */ + public function handle(Storeable $settings, array $input): void + { + $settings->fill($input)->save(); + } + + /** + * @return array + */ + public function rules(): array + { + /** @var Storeable */ + $group = request()->route('settingGroup'); + + return $group->rules(); + } + + public function asController(ActionRequest $request, Storeable $settingGroup): RedirectResponse + { + $settingGroup->beforeSave($request); + $this->handle($settingGroup, $request->validated()); + + return redirect()->back(); + } +} diff --git a/app/Setting/Contracts/Storeable.php b/app/Setting/Contracts/Storeable.php index 8d976e6d..ee8ec324 100644 --- a/app/Setting/Contracts/Storeable.php +++ b/app/Setting/Contracts/Storeable.php @@ -2,12 +2,26 @@ namespace App\Setting\Contracts; +use App\Setting\LocalSettings; +use Lorisleiva\Actions\ActionRequest; +use Spatie\LaravelSettings\Settings; + +/** + * @mixin LocalSettings + */ interface Storeable { - /** - * @return class-string - */ - public static function storeAction(): string; + public function url(): string; - public static function url(): string; + /** + * @param array $input + */ + public function fill(array $input): Settings; + + /** + * @return array + */ + public function rules(): array; + + public function beforeSave(ActionRequest $request): void; } diff --git a/app/Setting/LocalSettings.php b/app/Setting/LocalSettings.php index d83a4c94..46618f37 100644 --- a/app/Setting/LocalSettings.php +++ b/app/Setting/LocalSettings.php @@ -8,9 +8,9 @@ abstract class LocalSettings extends Settings { abstract public static function title(): string; - public static function url(): string + public function url(): string { - return '/setting/' . static::group(); + return route('setting.view', ['settingGroup' => $this->group()]); } /** diff --git a/app/Setting/NamiSettings.php b/app/Setting/NamiSettings.php index 91641d4d..12dbd1d1 100644 --- a/app/Setting/NamiSettings.php +++ b/app/Setting/NamiSettings.php @@ -3,8 +3,10 @@ namespace App\Setting; use App\Group; +use App\Initialize\Actions\NamiLoginCheckAction; use App\Nami\Actions\SettingSaveAction; use App\Setting\Contracts\Storeable; +use Lorisleiva\Actions\ActionRequest; use Zoomyboy\LaravelNami\Api; use Zoomyboy\LaravelNami\Nami; @@ -29,16 +31,31 @@ class NamiSettings extends LocalSettings implements Storeable return Nami::login($this->mglnr, $this->password); } + /** + * @inheritdoc + */ + public function rules(): array + { + return [ + 'mglnr' => 'required', + 'password' => 'required', + 'default_group_id' => 'required', + ]; + } + + public function beforeSave(ActionRequest $request): void + { + NamiLoginCheckAction::run([ + 'mglnr' => $request->mglnr, + 'password' => $request->password, + ]); + } + public function localGroup(): ?Group { return Group::firstWhere('nami_id', $this->default_group_id); } - public static function storeAction(): string - { - return SettingSaveAction::class; - } - public static function title(): string { return 'NaMi-Login'; diff --git a/app/Setting/SettingFactory.php b/app/Setting/SettingFactory.php index e6e3ee22..35424065 100644 --- a/app/Setting/SettingFactory.php +++ b/app/Setting/SettingFactory.php @@ -20,10 +20,6 @@ class SettingFactory { $this->settings[] = $setting; - if (new $setting instanceof Storeable) { - $this->registerStoreRoute(new $setting); - } - if (1 === count($this->settings)) { app(Router::class)->redirect('/setting', '/setting/' . $setting::group()); } @@ -35,8 +31,8 @@ class SettingFactory public function getShare(): array { return collect($this->settings)->map(fn ($setting) => [ - 'url' => $setting::url(), - 'is_active' => '/' . request()->path() === $setting::url(), + 'url' => (new $setting)->url(), + 'is_active' => url(request()->path()) === (new $setting)->url(), 'title' => $setting::title(), ]) ->toArray(); @@ -48,9 +44,4 @@ class SettingFactory return app($settingClass); } - - protected function registerStoreRoute(Storeable $setting): void - { - app(Router::class)->middleware(['web', 'auth:web'])->post($setting::url(), $setting::storeAction()); - } } diff --git a/app/Setting/SettingServiceProvider.php b/app/Setting/SettingServiceProvider.php index 35c21505..bd297cb2 100644 --- a/app/Setting/SettingServiceProvider.php +++ b/app/Setting/SettingServiceProvider.php @@ -8,6 +8,7 @@ use App\Invoice\InvoiceSettings; use App\Mailgateway\MailgatewaySettings; use App\Module\ModuleSettings; use App\Prevention\PreventionSettings; +use App\Setting\Actions\StoreAction; use App\Setting\Actions\ViewAction; use Illuminate\Routing\Router; use Illuminate\Support\ServiceProvider; @@ -23,7 +24,9 @@ class SettingServiceProvider extends ServiceProvider { app()->singleton(SettingFactory::class, fn () => new SettingFactory()); app(Router::class)->bind('settingGroup', fn ($param) => app(SettingFactory::class)->resolveGroupName($param)); - app(Router::class)->middleware(['web', 'auth:web'])->get('/setting/{settingGroup}', ViewAction::class); + app(Router::class)->middleware(['web', 'auth:web'])->name('setting.view')->get('/setting/{settingGroup}', ViewAction::class); + app(Router::class)->middleware(['web', 'auth:web'])->name('setting.data')->get('/setting/{settingGroup}/data', ViewAction::class); + app(Router::class)->middleware(['web', 'auth:web'])->name('setting.store')->post('/setting/{settingGroup}', StoreAction::class); } /** diff --git a/resources/js/views/setting/Bill.vue b/resources/js/views/setting/Bill.vue index 76193419..b6b71e1b 100644 --- a/resources/js/views/setting/Bill.vue +++ b/resources/js/views/setting/Bill.vue @@ -17,7 +17,7 @@ - + diff --git a/resources/js/views/setting/Form.vue b/resources/js/views/setting/Form.vue index 2c302efb..ff8a95af 100644 --- a/resources/js/views/setting/Form.vue +++ b/resources/js/views/setting/Form.vue @@ -9,8 +9,8 @@

Hier kannst du Einstellungen für Anmeldeformulare setzen.

- - + +
diff --git a/tests/EndToEnd/MassstoreActionTest.php b/tests/EndToEnd/MassstoreActionTest.php index f76a4be4..37f08caa 100644 --- a/tests/EndToEnd/MassstoreActionTest.php +++ b/tests/EndToEnd/MassstoreActionTest.php @@ -11,10 +11,8 @@ use App\Membership\Actions\MassStoreAction; use App\Membership\Actions\MembershipDestroyAction; use App\Membership\Actions\MembershipStoreAction; use App\Subactivity; -use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Support\Facades\Queue; use Tests\EndToEndTestCase; -use Tests\TestCase; use Throwable; use Zoomyboy\LaravelNami\Fakes\MembershipFake; diff --git a/tests/Feature/Invoice/SettingTest.php b/tests/Feature/Invoice/SettingTest.php index a7826b8d..e4ce8e3c 100644 --- a/tests/Feature/Invoice/SettingTest.php +++ b/tests/Feature/Invoice/SettingTest.php @@ -10,6 +10,15 @@ class SettingTest extends TestCase { use DatabaseTransactions; + public function testItDisplaysView(): void + { + $this->withoutExceptionHandling()->login()->loginNami(); + + $this->get(route('setting.view', ['settingGroup' => 'bill'])) + ->assertOk() + ->assertComponent('setting/Bill'); + } + public function testDisplaySettings(): void { $this->withoutExceptionHandling()->login()->loginNami(); @@ -27,7 +36,7 @@ class SettingTest extends TestCase 'rememberWeeks' => 6 ])->save(); - $this->get('/setting/bill') + $this->get(route('setting.data', ['settingGroup' => 'bill'])) ->assertOk() ->assertComponent('setting/Bill') ->assertInertiaPath('data.from_long', 'DPSG Stamm Muster') @@ -40,16 +49,16 @@ class SettingTest extends TestCase ->assertInertiaPath('data.zip', '12345') ->assertInertiaPath('data.iban', 'DE05') ->assertInertiaPath('data.bic', 'SOLSDE') - ->assertInertiaPath('data.remember_weeks', 6); + ->assertInertiaPath('data.rememberWeeks', 6); } public function testItReturnsTabs(): void { $this->withoutExceptionHandling()->login()->loginNami(); - $this->get('/setting/bill') + $this->get(route('setting.view', ['settingGroup' => 'bill'])) ->assertInertiaPath('setting_menu.1.title', 'Rechnung') - ->assertInertiaPath('setting_menu.1.url', '/setting/bill') + ->assertInertiaPath('setting_menu.1.url', url('/setting/bill')) ->assertInertiaPath('setting_menu.1.is_active', true) ->assertInertiaPath('setting_menu.0.is_active', false); } @@ -69,7 +78,7 @@ class SettingTest extends TestCase 'zip' => '12345', 'iban' => 'DE05', 'bic' => 'SOLSDE', - 'remember_weeks' => 10 + 'rememberWeeks' => 10 ]); $response->assertRedirect('/setting/bill');