Add configurable system settings service

This commit is contained in:
Arya Dwi Putra
2025-12-01 07:38:04 +07:00
parent 03e9d968f5
commit 9aab35ff05
4 changed files with 99 additions and 0 deletions

View File

@@ -59,6 +59,14 @@ MAIL_PASSWORD=null
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="${APP_NAME}"
ASSET_CODE_PREFIX=AST
ASSET_QR_ENABLED=true
ASSET_WARRANTY_REMINDER_DAYS=30
SYSTEM_DATE_FORMAT=d/m/Y
SYSTEM_TIME_FORMAT=H:i
SYSTEM_DEFAULT_CURRENCY=IDR
SYSTEM_AUDIT_LOG=true
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1

View File

@@ -0,0 +1,35 @@
<?php
namespace App\Services\System;
use Illuminate\Support\Arr;
class SystemSettingService
{
/**
* Ambil semua konfigurasi sistem dengan struktur terkelompok.
*/
public function all(): array
{
return config('system', []);
}
/**
* Ambil satu nilai konfigurasi dengan dukungan dot notation.
*/
public function get(string $key, mixed $default = null): mixed
{
return Arr::get($this->all(), $key, $default);
}
/**
* Set konfigurasi secara runtime tanpa menyentuh database.
*/
public function set(string $key, mixed $value): void
{
$settings = $this->all();
Arr::set($settings, $key, $value);
config(['system' => $settings]);
}
}

22
config/system.php Normal file
View File

@@ -0,0 +1,22 @@
<?php
return [
'application' => [
'name' => env('APP_NAME', 'Asset Management System'),
'timezone' => env('APP_TIMEZONE', 'Asia/Jakarta'),
'locale' => env('APP_LOCALE', 'id'),
],
'asset' => [
'code_prefix' => env('ASSET_CODE_PREFIX', 'AST'),
'qr_enabled' => env('ASSET_QR_ENABLED', true),
'warranty_reminder_days' => env('ASSET_WARRANTY_REMINDER_DAYS', 30),
],
'ui' => [
'date_format' => env('SYSTEM_DATE_FORMAT', 'd/m/Y'),
'time_format' => env('SYSTEM_TIME_FORMAT', 'H:i'),
'currency' => env('SYSTEM_DEFAULT_CURRENCY', 'IDR'),
],
'security' => [
'audit_log' => env('SYSTEM_AUDIT_LOG', true),
],
];

View File

@@ -0,0 +1,34 @@
<?php
namespace Tests\Unit;
use App\Services\System\SystemSettingService;
use Tests\TestCase;
class SystemSettingServiceTest extends TestCase
{
public function test_it_reads_system_settings_from_config(): void
{
// Override konfigurasi untuk memastikan service mengambil nilai terbaru.
config([
'system.asset.code_prefix' => 'ASTX',
'system.application.name' => 'Custom AMS',
]);
$service = app(SystemSettingService::class);
$this->assertSame('ASTX', $service->get('asset.code_prefix'));
$this->assertSame('Custom AMS', $service->get('application.name'));
$this->assertTrue($service->get('asset.qr_enabled'));
}
public function test_it_can_override_runtime_settings(): void
{
$service = app(SystemSettingService::class);
$service->set('ui.date_format', 'Y-m-d');
$this->assertSame('Y-m-d', $service->get('ui.date_format'));
$this->assertSame('Y-m-d', config('system.ui.date_format'));
}
}