refactor(vikon): remove hardcoded module IDs and use config everywhere

This commit is contained in:
F4ilji
2026-07-05 11:40:02 +05:00
parent 8cf195efb0
commit 0a5f99e6ae
6 changed files with 12 additions and 14 deletions
@@ -42,12 +42,8 @@ class SyncFilesAction
private function getModuleName(int $moduleId): string
{
return match ($moduleId) {
1 => 'sveden',
2 => 'abitur',
6 => 'vsoko',
default => throw new \RuntimeException("Unknown module: {$moduleId}"),
};
$modules = config('vikon.modules');
return $modules[$moduleId]['path'] ?? throw new \RuntimeException("Unknown module: {$moduleId}");
}
private function getUsedDirNames(int $moduleId, string $accessToken): array
@@ -37,8 +37,9 @@ class UpdateCoreAction
private function downloadCore(int $moduleId, string $modulePath, string $tempPath, string $accessToken): void
{
if ($moduleId === 2) {
$this->initAbiturModule($modulePath, $accessToken);
$moduleConfig = $this->modulesConfig[$moduleId] ?? [];
if (!empty($moduleConfig['init_only'])) {
$this->initModuleFromApi($moduleId, $modulePath, $accessToken);
return;
}
@@ -229,13 +230,13 @@ class UpdateCoreAction
return $response->json()['directories'] ?? [];
}
private function initAbiturModule(string $modulePath, string $accessToken): void
private function initModuleFromApi(int $moduleId, string $modulePath, string $accessToken): void
{
$response = $this->http->getWithToken('pull_updates/generateEmptyModuleCore/2', $accessToken);
$response = $this->http->getWithToken("pull_updates/generateEmptyModuleCore/{$moduleId}", $accessToken);
$body = $response->json();
if (!isset($body['success']) || $body['success'] !== true) {
throw new \RuntimeException('ABITUR init failed: ' . ($body['message'] ?? 'Unknown'));
throw new \RuntimeException("Module init failed for module {$moduleId}: " . ($body['message'] ?? 'Unknown'));
}
File::makeDirectory($modulePath, 0755, true, true);
@@ -12,7 +12,7 @@ class UpdateModuleRequest extends FormRequest
public function rules(): array
{
return [
'module_id' => ['required', 'integer', Rule::in([1, 2, 6])],
'module_id' => ['required', 'integer', Rule::in(array_keys(config('vikon.modules')))],
];
}
}
@@ -14,7 +14,7 @@ class UpdatePartRequest extends FormRequest
public function rules(): array
{
return [
'module_id' => ['required', 'integer', 'in:1,2,6'],
'module_id' => ['required', 'integer', 'in:' . implode(',', array_keys(config('vikon.modules')))],
'part' => ['required', 'string', 'max:50'],
];
}