adrema/app/Mailman/Actions/SettingSaveAction.php

81 lines
2.4 KiB
PHP
Raw Normal View History

2022-10-18 15:04:47 +02:00
<?php
namespace App\Mailman\Actions;
use App\Mailman\MailmanSettings;
use App\Mailman\Support\MailmanService;
use Illuminate\Contracts\Validation\Validator;
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(MailmanSettings::class);
$settings->fill([
'base_url' => $input['base_url'] ?? null,
'username' => $input['username'] ?? null,
'password' => $input['password'] ?? null,
2022-10-20 12:01:57 +02:00
'all_list' => $input['all_list'] ?? null,
'all_parents_list' => $input['all_parents_list'] ?? null,
'active_leaders_list' => $input['active_leaders_list'] ?? null,
'passive_leaders_list' => $input['passive_leaders_list'] ?? null,
2022-10-20 03:22:50 +02:00
'is_active' => $input['is_active'] ?? false,
2022-10-18 15:04:47 +02:00
]);
$settings->save();
}
/**
* @return array<string, string>
*/
public function rules(): array
{
return [
2022-10-20 03:22:50 +02:00
'base_url' => 'required_if:is_active,true',
'username' => 'required_if:is_active,true',
'password' => 'required_if:is_active,true',
];
}
2022-10-20 11:11:52 +02:00
/**
* @return array<string, string>
*/
2022-10-20 03:22:50 +02:00
public function getValidationMessages(): array
{
return [
'base_url.required_if' => 'URL ist erforderlich.',
'username.required_if' => 'Benutzername ist erforderlich.',
'password.required_if' => 'Passwort ist erforderlich.',
];
}
2022-10-18 15:04:47 +02:00
public function afterValidator(Validator $validator, ActionRequest $request): void
{
2022-10-20 03:22:50 +02:00
if (false === $request->is_active || !$request->filled(['base_url', 'username', 'password'])) {
return;
}
$result = app(MailmanService::class)->setCredentials($request->input('base_url'), $request->input('username'), $request->input('password'))->check();
if (!$result) {
2022-10-18 15:04:47 +02:00
$validator->errors()->add('mailman', 'Verbindung fehlgeschlagen.');
}
}
public function asController(ActionRequest $request): RedirectResponse
{
$this->handle($request->all());
return redirect()->back()->success('Einstellungen gespeichert');
}
}