saufen/src/Service/ConfigurationService.php
2025-05-31 21:43:13 +02:00

197 lines
5.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Service;
use InvalidArgumentException;
use App\Entity\SystemConfig;
use App\Repository\SystemConfigRepository;
use App\Enum\SystemSettingKey;
class ConfigurationService
{
// For backward compatibility
/**
* @deprecated Use SystemSettingKey::STOCK_ADJUSTMENT_LOOKBACK instead
*/
public const KEY_STOCK_ADJUSTMENT_LOOKBACK = 'stock_adjustment_lookback';
/**
* @deprecated Use SystemSettingKey::STOCK_ADJUSTMENT_LOOKBACK_ORDERS instead
*/
public const KEY_STOCK_ADJUSTMENT_LOOKBACK_ORDERS = 'stock_adjustment_lookback_orders';
/**
* @deprecated Use SystemSettingKey::STOCK_ADJUSTMENT_MAGNITUDE instead
*/
public const KEY_STOCK_ADJUSTMENT_MAGNITUDE = 'stock_adjustment_magnitude';
/**
* @deprecated Use SystemSettingKey::STOCK_ADJUSTMENT_THRESHOLD instead
*/
public const KEY_STOCK_ADJUSTMENT_THRESHOLD = 'stock_adjustment_threshold';
/**
* @deprecated Use SystemSettingKey::LOW_STOCK_THRESHOLD instead
*/
public const KEY_LOW_STOCK_THRESHOLD = 'low_stock_threshold';
/**
* @deprecated Use SystemSettingKey::CRITICAL_STOCK_THRESHOLD instead
*/
public const KEY_CRITICAL_STOCK_THRESHOLD = 'critical_stock_threshold';
/**
* @deprecated Use SystemSettingKey::DEFAULT_DESIRED_STOCK instead
*/
public const KEY_DEFAULT_DESIRED_STOCK = 'default_desired_stock';
/**
* @deprecated Use SystemSettingKey::SYSTEM_NAME instead
*/
public const KEY_SYSTEM_NAME = 'system_name';
/**
* @deprecated Use SystemSettingKey::ENABLE_AUTO_ADJUSTMENT instead
*/
public const KEY_ENABLE_AUTO_ADJUSTMENT = 'enable_auto_adjustment';
/**
* @deprecated Use SystemSettingKey::SHOW_LOW_STOCK_ALERTS instead
*/
public const KEY_SHOW_LOW_STOCK_ALERTS = 'show_low_stock_alerts';
/**
* @deprecated Use SystemSettingKey::SHOW_QUICK_UPDATE_FORM instead
*/
public const KEY_SHOW_QUICK_UPDATE_FORM = 'show_quick_update_form';
/**
* @deprecated Use SystemSettingKey::ITEMS_PER_PAGE instead
*/
public const KEY_ITEMS_PER_PAGE = 'items_per_page';
public function __construct(
private readonly SystemConfigRepository $systemConfigRepository
) {}
/**
* Get all configuration entries
*
* @return SystemConfig[]
*/
public function getAllConfigs(): array
{
return $this->systemConfigRepository->findAll();
}
/**
* Get a configuration value by key
*
* @param string $key
* @param string $default Default value if the key doesn't exist
* @return string
*/
public function getConfigValue(string $key, string $default = ''): string
{
return $this->systemConfigRepository->getValue($key, $default);
}
/**
* Set a configuration value
*
* @param string $key
* @param string $value
* @return void
*/
public function setConfigValue(string $key, string $value): void
{
$this->systemConfigRepository->setValue($key, $value);
}
/**
* Get a configuration entity by key
*
* @param string $key
* @return SystemConfig|null
*/
public function getConfigByKey(string $key): ?SystemConfig
{
return $this->systemConfigRepository->findByKey($key);
}
/**
* Create a new configuration entry
*
* @param string $key
* @param string $value
* @return SystemConfig
* @throws InvalidArgumentException If a configuration with the same key already exists
*/
public function createConfig(string $key, string $value): SystemConfig
{
if ($this->systemConfigRepository->findByKey($key) instanceof SystemConfig) {
throw new InvalidArgumentException("A configuration with the key '$key' already exists");
}
$config = new SystemConfig($key, $value);
$this->systemConfigRepository->save($config);
return $config;
}
/**
* Update an existing configuration entry
*
* @param SystemConfig $config
* @param string|null $key
* @param string|null $value
* @return SystemConfig
* @throws InvalidArgumentException If a configuration with the same key already exists
*/
public function updateConfig(
SystemConfig $config,
?string $key = null,
?string $value = null
): SystemConfig {
// Update key if provided
if ($key !== null && $key !== $config->getKey()) {
// Check if a configuration with the same key already exists
if ($this->systemConfigRepository->findByKey($key) instanceof SystemConfig) {
throw new InvalidArgumentException("A configuration with the key '$key' already exists");
}
$config->setKey($key);
}
// Update value if provided
if ($value !== null) {
$config->setValue($value);
}
$this->systemConfigRepository->save($config);
return $config;
}
/**
* Delete a configuration entry
*
* @param SystemConfig $config
* @return void
*/
public function deleteConfig(SystemConfig $config): void
{
$this->systemConfigRepository->remove($config);
}
/**
* Initialize default configuration values if they don't exist
*
* @return void
*/
public function initializeDefaultConfigs(): void
{
$defaults = [
SystemSettingKey::STOCK_ADJUSTMENT_LOOKBACK->value => '30', // Deprecated
SystemSettingKey::STOCK_ADJUSTMENT_LOOKBACK_ORDERS->value => '5',
SystemSettingKey::STOCK_ADJUSTMENT_MAGNITUDE->value => '0.2',
SystemSettingKey::STOCK_ADJUSTMENT_THRESHOLD->value => '0.1',
];
foreach ($defaults as $key => $value) {
if (!$this->getConfigByKey($key) instanceof SystemConfig) {
$this->createConfig($key, $value);
}
}
}
}