adrema/app/Initialize/Actions/InitializeAction.php

112 lines
3.3 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;
2023-02-13 15:56:20 +01:00
use Zoomyboy\LaravelNami\Api;
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,
];
private Api $api;
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
{
2023-02-13 15:56:20 +01:00
foreach ($this->initializers as $initializer) {
app($initializer)->handle($this->api);
}
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',
];
}
2022-08-31 00:33:36 +02:00
public function asController(ActionRequest $request, NamiSettings $settings): RedirectResponse
{
2023-02-13 15:56:20 +01:00
$this->api = Nami::login($request->input('mglnr'), $request->input('password'));
2022-09-01 23:56:34 +02:00
2023-02-13 15:56:20 +01:00
if (!$this->api->hasGroup($request->input('group_id'))) {
2022-09-01 23:56:34 +02:00
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');
2023-02-13 15:56:20 +01:00
$this->api = Nami::login($mglnr, $password);
2023-02-07 01:38:27 +01:00
$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
}