Remove Setting store actions
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is failing Details

This commit is contained in:
philipp lang 2024-08-01 13:33:28 +02:00
parent daf6e87814
commit d9adb526ce
17 changed files with 161 additions and 222 deletions

View File

@ -1,46 +0,0 @@
<?php
namespace App\Form\Actions;
use App\Form\FormSettings;
use Illuminate\Http\RedirectResponse;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
class SettingStoreAction
{
use AsAction;
/**
* @param array<string, mixed> $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<string, mixed>
*/
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();
}
}

View File

@ -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,
]
]
];

View File

@ -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

View File

@ -1,43 +0,0 @@
<?php
namespace App\Invoice;
use Illuminate\Http\RedirectResponse;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
class SettingSaveAction
{
use AsAction;
/**
* @param array<string, string> $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();
}
}

View File

@ -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
*/

View File

@ -1,45 +0,0 @@
<?php
namespace App\Module;
use Illuminate\Http\RedirectResponse;
use Illuminate\Validation\Rule;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
class ModuleStoreAction
{
use AsAction;
/**
* @param array<string, mixed> $input
*/
public function handle(array $input): void
{
$settings = app(ModuleSettings::class);
$settings->fill([
'modules' => $input['modules'],
]);
$settings->save();
}
/**
* @return array<string, mixed>
*/
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();
}
}

View File

@ -1,42 +0,0 @@
<?php
namespace App\Nami\Actions;
use App\Initialize\Actions\NamiLoginCheckAction;
use App\Setting\NamiSettings;
use Illuminate\Http\RedirectResponse;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
class SettingSaveAction
{
use AsAction;
/**
* @param array<string, string> $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();
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace App\Setting\Actions;
use App\Setting\Contracts\Storeable;
use Illuminate\Http\RedirectResponse;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
class StoreAction
{
use AsAction;
/**
* @param array<string, mixed> $input
*/
public function handle(Storeable $settings, array $input): void
{
$settings->fill($input)->save();
}
/**
* @return array<string, mixed>
*/
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();
}
}

View File

@ -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<string, mixed> $input
*/
public function fill(array $input): Settings;
/**
* @return array<string, mixed>
*/
public function rules(): array;
public function beforeSave(ActionRequest $request): void;
}

View File

@ -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()]);
}
/**

View File

@ -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';

View File

@ -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());
}
}

View File

@ -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);
}
/**

View File

@ -17,7 +17,7 @@
<f-text id="website" v-model="inner.website" label="Webseite"></f-text>
<f-text id="iban" v-model="inner.iban" label="IBAN"></f-text>
<f-text id="bic" v-model="inner.bic" label="BIC"></f-text>
<f-text id="remember_weeks" v-model="inner.remember_weeks" type="number" label="Erinnerung alle X Wochen versenden"></f-text>
<f-text id="remember_weeks" v-model="inner.rememberWeeks" type="number" label="Erinnerung alle X Wochen versenden"></f-text>
</form>
</setting-layout>
</page-layout>

View File

@ -9,8 +9,8 @@
<p class="text-sm">Hier kannst du Einstellungen für Anmeldeformulare setzen.</p>
</div>
<div class="grid grid-cols-2 gap-4">
<f-text id="register_url" v-model="inner.register_url" label="Formular-Link"></f-text>
<f-text id="clear_cache_url" v-model="inner.clear_cache_url" label="Frontend-Cache-Url"></f-text>
<f-text id="register_url" v-model="inner.registerUrl" label="Formular-Link"></f-text>
<f-text id="clear_cache_url" v-model="inner.clearCacheUrl" label="Frontend-Cache-Url"></f-text>
</div>
</form>
</setting-layout>

View File

@ -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;

View File

@ -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');