refactor: extract action classes, add Blade components, reduce Commandocentrum

- Create EmulatorControlAction and NitroControlAction classes
- Extract business logic from Commandocentrum controller methods
- Add Blade components for status cards, diagnostics, and summary cards
- Replace shell_exec with file_get_contents in config reading
- Remove duplicate methods and unused code
- Commandocentrum reduced from 2033 to 1780 lines
This commit is contained in:
root
2026-05-19 20:57:31 +02:00
parent 976b990a8a
commit cbe189fd96
6 changed files with 364 additions and 294 deletions
+87
View File
@@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
namespace App\Actions\Commandocentrum;
use App\Services\EmulatorUpdateService;
use App\Services\RconService;
use App\Services\SettingsService;
use Illuminate\Support\Facades\Process;
class EmulatorControlAction
{
public function __construct(
private readonly SettingsService $settings,
private readonly EmulatorUpdateService $updateService,
) {}
public function start(): array
{
$serviceName = $this->settings->getOrDefault('emulator_service_name', 'arcturus');
$result = Process::timeout(30)->run("systemctl start {$serviceName} 2>&1");
return [
'success' => $result->successful(),
'message' => $result->successful() ? 'Emulator gestart!' : ($result->output() ?: 'Kon emulator niet starten'),
];
}
public function stop(): array
{
$serviceName = $this->settings->getOrDefault('emulator_service_name', 'arcturus');
$result = Process::timeout(30)->run("systemctl stop {$serviceName} 2>&1");
return [
'success' => $result->successful(),
'message' => $result->successful() ? 'Emulator gestopt!' : ($result->output() ?: 'Kon emulator niet stoppen'),
];
}
public function restart(): array
{
$serviceName = $this->settings->getOrDefault('emulator_service_name', 'arcturus');
$result = Process::timeout(60)->run("systemctl restart {$serviceName} 2>&1");
return [
'success' => $result->successful(),
'message' => $result->successful() ? 'Emulator herstart!' : ($result->output() ?: 'Kon emulator niet herstarten'),
];
}
public function sendAlert(string $message): array
{
if (empty($message)) {
return ['success' => false, 'message' => 'Bericht mag niet leeg zijn'];
}
app(RconService::class)->sendCommand('alert', ['message' => $message]);
return ['success' => true, 'message' => 'Alert verstuurd naar alle gebruikers!'];
}
public function build(): array
{
return $this->updateService->buildFromSource();
}
public function update(): array
{
return $this->updateService->updateEmulator();
}
public function runSqlUpdates(): array
{
return $this->updateService->runSqlUpdates();
}
public function getBackups(): array
{
return $this->updateService->getBackupList();
}
public function restoreBackup(string $backupName): array
{
return $this->updateService->restoreBackup($backupName);
}
}
+119
View File
@@ -0,0 +1,119 @@
<?php
declare(strict_types=1);
namespace App\Actions\Commandocentrum;
use App\Services\SettingsService;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Process;
class NitroControlAction
{
public function __construct(
private readonly SettingsService $settings,
) {}
public function pullUpdates(string $clientPath, string $rendererPath, string $branch): array
{
$this->runGitPull($clientPath, $branch);
$this->runGitPull($rendererPath, $branch);
return ['success' => true, 'message' => 'Nitro bijgewerkt van GitHub'];
}
public function build(string $clientPath, string $rendererPath, string $branch): array
{
$this->runGitPull($clientPath, $branch);
$this->runGitPull($rendererPath, $branch);
Process::timeout(120)->run('cd ' . escapeshellarg($clientPath) . ' && sudo -u www-data npm install 2>&1');
Process::timeout(120)->run('cd ' . escapeshellarg($rendererPath) . ' && sudo -u www-data npm install 2>&1');
$exitCode = Artisan::call('build:theme');
return [
'success' => $exitCode === 0,
'message' => $exitCode === 0 ? 'Nitro build succesvol!' : 'Build gestart - controleer handmatig',
];
}
public function generateConfigs(string $siteUrl, string $webroot, string $gamedataPath): array
{
if (! filter_var($siteUrl, FILTER_VALIDATE_URL)) {
return ['success' => false, 'message' => 'Voer een geldige URL in'];
}
$existingConfigs = $this->readExistingConfigs($webroot, $gamedataPath);
$exitCode = Artisan::call('app:generate-nitro-configs', ['--site-url' => $siteUrl]);
if ($existingConfigs !== [] && $exitCode === 0) {
$this->mergeExistingConfigs($webroot, $existingConfigs);
}
$this->settings->set('nitro_last_checked', now()->toIso8601String());
return [
'success' => $exitCode === 0,
'message' => $exitCode === 0 ? 'Configs gegenereerd & bestaande instellingen behouden!' : 'Config gegenereerd (controleer handmatig)',
];
}
private function runGitPull(string $path, string $branch): void
{
Process::timeout(60)->run('cd ' . escapeshellarg($path) . ' && sudo -u www-data git pull origin ' . escapeshellarg($branch) . ' 2>&1');
}
private function readExistingConfigs(string $webroot, string $gamedataPath): array
{
$configs = [];
$files = ['renderer-config.json', 'ui-config.json', 'UITexts.json'];
foreach ($files as $file) {
$path = $webroot . '/' . $file;
$content = @file_get_contents($path);
if ($content) {
$decoded = json_decode($content, true);
if (json_last_error() === JSON_ERROR_NONE) {
$configs[$file] = $decoded;
}
}
}
if ($gamedataPath !== '') {
$gamedataConfigs = [
'ExternalTexts.json' => $gamedataPath . '/config/ExternalTexts.json',
'FurnitureData.json' => $gamedataPath . '/config/FurnitureData.json',
'ProductData.json' => $gamedataPath . '/config/ProductData.json',
'FigureData.json' => $gamedataPath . '/config/FigureData.json',
];
foreach ($gamedataConfigs as $key => $path) {
$content = @file_get_contents($path);
if ($content) {
$decoded = json_decode($content, true);
if (json_last_error() === JSON_ERROR_NONE) {
$configs['gamedata.' . $key] = $decoded;
}
}
}
}
return $configs;
}
private function mergeExistingConfigs(string $webroot, array $existingConfigs): void
{
if (isset($existingConfigs['renderer-config.json'])) {
$newPath = $webroot . '/renderer-config.json';
$newContent = @file_get_contents($newPath);
$newConfig = json_decode($newContent, true);
if ($newConfig && json_last_error() === JSON_ERROR_NONE) {
$merged = array_merge($existingConfigs['renderer-config.json'], $newConfig);
@file_put_contents($newPath, json_encode($merged, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
}
}
}