adrema/app/Initialize/Actions/InitializeAction.php

105 lines
3.0 KiB
PHP
Raw Normal View History

2022-08-31 00:33:36 +02:00
<?php
namespace App\Initialize\Actions;
2023-02-13 15:56:20 +01:00
use App\Initialize\InitializeActivities;
use App\Initialize\InitializeConfessions;
use App\Initialize\InitializeCountries;
use App\Initialize\InitializeCourses;
use App\Initialize\InitializeFees;
use App\Initialize\InitializeGenders;
use App\Initialize\InitializeGroups;
use App\Initialize\InitializeMembers;
use App\Initialize\InitializeNationalities;
use App\Initialize\InitializeRegions;
2022-08-31 00:33:36 +02:00
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
2023-02-13 15:56:20 +01:00
/**
* @var array<int, class-string>
*/
public array $initializers = [
InitializeGroups::class,
InitializeNationalities::class,
InitializeFees::class,
InitializeActivities::class,
InitializeConfessions::class,
InitializeCountries::class,
InitializeGenders::class,
InitializeRegions::class,
InitializeCourses::class,
InitializeMembers::class,
];
2023-05-05 12:02:58 +02:00
public function handle(): void
2022-08-31 00:33:36 +02:00
{
2023-05-05 12:22:28 +02:00
$api = app(NamiSettings::class)->login();
2023-02-13 15:56:20 +01:00
foreach ($this->initializers as $initializer) {
2023-05-05 12:22:28 +02:00
app($initializer)->handle($api);
2023-02-13 15:56:20 +01:00
}
2022-08-31 00:33:36 +02:00
}
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',
];
}
2023-05-05 12:02:58 +02:00
public function asController(ActionRequest $request): RedirectResponse
2022-08-31 00:33:36 +02:00
{
2023-05-05 12:02:58 +02:00
$api = Nami::freshLogin($request->input('mglnr'), $request->input('password'));
2022-09-01 23:56:34 +02:00
2023-05-05 12:02:58 +02:00
if (!$api->hasGroup($request->input('group_id'))) {
2022-09-01 23:56:34 +02:00
throw ValidationException::withMessages(['nami' => 'Gruppierung nicht gefunden.']);
}
2023-05-05 12:02:58 +02:00
$this->setApi((int) $request->input('mglnr'), $request->input('password'), (int) $request->input('group_id'));
2023-05-05 12:22:28 +02:00
self::dispatch();
2022-08-31 00:33:36 +02:00
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
2023-05-05 12:02:58 +02:00
public function asCommand(Command $command): void
{
$this->setApi((int) $command->option('mglnr'), $command->option('password'), (int) $command->option('group'));
2023-05-05 12:22:28 +02:00
self::dispatch();
2023-05-05 12:02:58 +02:00
}
private function setApi(int $mglnr, string $password, int $groupId): void
2023-02-07 01:38:27 +01:00
{
2023-05-05 12:02:58 +02:00
$settings = app(NamiSettings::class);
2023-02-07 01:38:27 +01:00
$settings->mglnr = $mglnr;
$settings->password = $password;
2023-05-05 12:02:58 +02:00
$settings->default_group_id = $groupId;
2023-02-07 01:38:27 +01:00
$settings->save();
}
2022-08-31 00:33:36 +02:00
}