27-05-2026 DHL Modul v2.1 / Optimierung tracking

This commit is contained in:
Kevin Adametz 2026-05-27 18:51:23 +02:00
parent 036595be94
commit 2bdc9ada3c
33 changed files with 2367 additions and 2086 deletions

View file

@ -4,11 +4,26 @@ namespace App\Http\Controllers;
use App\Models\Setting;
use App\Services\DhlProductResolver;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Session;
use Request;
class SettingController extends Controller
{
/**
* In-memory cache of the resolved DHL configuration for the lifetime
* of the current PHP process (request, queue job, CLI command).
*
* `getDhlConfig()` is invoked from several layers per shipment
* (`DhlShipmentService`, `CreateShipmentJob`, `DhlShipmentController`
* cancel/return paths, ) and each call previously triggered ~25 DB
* queries against `settings`. Caching here turns subsequent calls
* into pure in-memory lookups.
*
* @var array<string, mixed>|null
*/
private static ?array $cachedDhlConfig = null;
public function __construct()
{
$this->middleware('admin');
@ -31,6 +46,9 @@ class SettingController extends Controller
if (isset($data['settings'])) {
foreach ($data['settings'] as $key => $value) {
$value['val'] = isset($value['val']) ? $value['val'] : false;
if ($key === 'dhl_international_countries') {
$value['val'] = DhlProductResolver::normalizeCountryCodeList(is_array($value['val']) ? $value['val'] : []);
}
Setting::setContentBySlug($key, $value['val'], $value['type']);
}
}
@ -40,6 +58,9 @@ class SettingController extends Controller
$this->updateDhlConfigCache();
Session::flash('alert-save-dhl', 'DHL Konfiguration erfolgreich gespeichert!');
} else {
// Any other setting change could still affect cached config
// values (e.g. shared sender data), so invalidate just in case.
self::flushDhlConfigCache();
Session::flash('alert-save', '1');
}
}
@ -47,14 +68,34 @@ class SettingController extends Controller
return redirect(route('admin_settings'));
}
/**
* Drop the in-process DHL configuration cache.
*
* Call this whenever a DHL-related setting is changed so the next
* call to {@see getDhlConfig()} reads fresh data from the database.
*/
public static function flushDhlConfigCache(): void
{
self::$cachedDhlConfig = null;
}
/**
* Get DHL configuration merged from database settings and .env values
* Priority is controlled by DHL_CONFIG_SOURCE environment variable:
* - 'database' (default): Database settings override .env values
* - 'env': Environment/Config values override database settings
*
* The result is cached per process; use {@see flushDhlConfigCache()}
* to invalidate the cache after changing settings.
*
* @return array<string, mixed>
*/
public function getDhlConfig()
{
if (self::$cachedDhlConfig !== null) {
return self::$cachedDhlConfig;
}
// Check if we're in test/sandbox mode
$isTestMode = config('dhl.legacy.test_mode', false) || config('dhl.legacy.sandbox', false);
$baseUrl = $isTestMode ? config('dhl.sandbox_url') : config('dhl.base_url');
@ -62,7 +103,7 @@ class SettingController extends Controller
// Determine configuration priority
$useEnvPriority = config('dhl.config_source') === 'env';
return [
return self::$cachedDhlConfig = [
// API Settings
'base_url' => $isTestMode ? $baseUrl : $this->getConfigValue('dhl_base_url', $baseUrl, $useEnvPriority),
'api_key' => $this->getConfigValue('dhl_api_key', config('dhl.api_key'), $useEnvPriority),
@ -152,9 +193,14 @@ class SettingController extends Controller
{
$configCountries = config('dhl.international_countries', DhlProductResolver::DEFAULT_INTERNATIONAL_COUNTRIES);
$countries = $configCountries;
$storedCountries = Schema::hasTable('settings')
? Setting::getContentBySlug('dhl_international_countries')
: false;
if (! $useEnvPriority) {
$countries = Setting::getContentBySlug('dhl_international_countries') ?: $configCountries;
if (is_array($storedCountries)) {
$countries = $storedCountries;
} elseif (! $useEnvPriority) {
$countries = $storedCountries ?: $configCountries;
}
return DhlProductResolver::normalizeCountryCodeList(is_array($countries) ? $countries : []);
@ -165,6 +211,10 @@ class SettingController extends Controller
*/
private function updateDhlConfigCache()
{
// Drop the in-process DHL config cache so the next call rebuilds it
// from the freshly saved settings.
self::flushDhlConfigCache();
// Clear config cache to force reload from database
\Artisan::call('config:clear');