adrema/app/Initialize/Actions/InitializeAction.php

82 lines
2.4 KiB
PHP
Raw Normal View History

2022-08-31 00:33:36 +02:00
<?php
namespace App\Initialize\Actions;
use App\Initialize\InitializeJob;
use App\Setting\NamiSettings;
2023-02-07 01:38:27 +01:00
use Illuminate\Console\Command;
2022-08-31 00:33:36 +02:00
use Illuminate\Http\RedirectResponse;
2022-09-01 23:56:34 +02:00
use Illuminate\Validation\ValidationException;
2022-08-31 00:33:36 +02:00
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
2022-09-01 23:56:34 +02:00
use Zoomyboy\LaravelNami\Nami;
2022-08-31 00:33:36 +02:00
class InitializeAction
{
use AsAction;
2023-02-08 00:14:59 +01:00
public string $commandSignature = 'initialize {--mglnr=} {--password=} {--group=}';
2023-02-07 01:38:27 +01:00
2022-09-01 23:56:34 +02:00
public function handle(int $mglnr, string $password, int $groupId): void
2022-08-31 00:33:36 +02:00
{
InitializeJob::dispatch();
}
2022-09-01 23:56:34 +02:00
/**
* @return array<string, string>
*/
2022-08-31 00:33:36 +02:00
public function rules(): array
{
return [
'mglnr' => 'required|numeric',
'password' => 'required|string',
'group_id' => 'required|numeric',
];
}
2022-09-01 23:56:34 +02:00
/**
* @return array<string, string>
*/
public function getValidationAttributes(): array
{
return [
'group_id' => 'Gruppierungsnr',
];
}
2022-08-31 00:33:36 +02:00
public function asController(ActionRequest $request, NamiSettings $settings): RedirectResponse
{
2022-09-01 23:56:34 +02:00
$api = Nami::login($request->input('mglnr'), $request->input('password'));
if (!$api->hasGroup($request->input('group_id'))) {
throw ValidationException::withMessages(['nami' => 'Gruppierung nicht gefunden.']);
}
2022-08-31 00:33:36 +02:00
$settings->mglnr = $request->input('mglnr');
$settings->password = $request->input('password');
$settings->default_group_id = $request->input('group_id');
$settings->save();
$this->handle(
(int) $request->input('mglnr', 0),
(string) $request->input('password', ''),
(int) $request->input('group_id', 0)
);
2022-09-02 00:45:39 +02:00
return redirect()->route('home')->success('Initialisierung beauftragt. Wir benachrichtigen dich per Mail wenn alles fertig ist.');
2022-08-31 00:33:36 +02:00
}
2023-02-07 01:38:27 +01:00
public function asCommand(Command $command, NamiSettings $settings): void
{
$mglnr = (int) $command->option('mglnr');
$password = $command->option('password');
$group = (int) $command->option('group');
$api = Nami::login($mglnr, $password);
$settings->mglnr = $mglnr;
$settings->password = $password;
$settings->default_group_id = $group;
$settings->save();
$this->handle((int) $mglnr, (string) $password, (int) $group);
}
2022-08-31 00:33:36 +02:00
}