<?php
/**
 * OptionStore-backed global plugin settings repository.
 *
 * @package WP_PowerSuite\Core
 * @since 1.2.0
 */

namespace WP_PowerSuite\Core;

use WP_PowerSuite\Core\Contracts\PluginSettingsRepository;

final class OptionStorePluginSettingsRepository implements PluginSettingsRepository {

	private OptionStore $store;

	public function __construct( ?OptionStore $store = null ) {
		$this->store = $store ?? OptionStore::getInstance();
	}

	public static function instance(): self {
		return new self();
	}

	public function get( string $key, mixed $default = null ): mixed {
		return $this->store->get( $key, $default );
	}

	public function set( string $key, mixed $value ): bool {
		$success = $this->store->set( $key, $value );
		if ( $success ) {
			$this->clearCache();
		}

		return $success;
	}

	public function clearCache(): void {
		$this->store->clearCache();
	}
}
