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); } } } }