<?php
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Small WordPress-backed configuration store. The fiscal core keeps the same
 * configuration keys as the PrestaShop edition, but no PrestaShop global
 * classes are introduced in WordPress.
 */
final class DdvWpConfigStore
{
    private const PREFIX = 'ddverifactu_cfg_';

    public static function get($key)
    {
        $sentinel = new stdClass();
        $value = get_option(self::optionName($key), $sentinel);
        return $value === $sentinel ? false : $value;
    }

    public static function updateValue($key, $value, $html = false)
    {
        unset($html);
        $name = self::optionName($key);
        $sentinel = new stdClass();
        $current = get_option($name, $sentinel);
        if ($current !== $sentinel && $current === $value) {
            return true;
        }
        return update_option($name, $value, false);
    }

    public static function deleteByName($key)
    {
        return delete_option(self::optionName($key));
    }

    private static function optionName($key)
    {
        return self::PREFIX . strtolower(preg_replace('/[^A-Za-z0-9_\-]/', '_', (string) $key));
    }
}
