<?php
/**
 * Basic configuration repository.
 *
 * @package EaLab\WP_Deployer
 */

namespace EaLab\WP_Deployer\includes\deployerConfig;

use EaLab\WP_Deployer\vendor\Ealab\Deployer\Utility\DeployerConfigRepository\ConfigRepositoryBase;

/**
 * Basic configuration repository.
 *
 * This provider reads configuration from the deployer-config.json file.
 */
class BasicConfigRepository extends ConfigRepositoryBase {
	/**
	 * {@inheritDoc}
	 */
	public function getConfig(): ?array {
		// Return cached config if available
		if ( $this->deployerConfig !== null ) {
			return $this->deployerConfig;
		}

		// Path to the deployer-config.json file
		$config_file_path = DEPLOY_SYNC_CONTENT__PLUGIN_DIR . 'deployer-config.json';

		if ( ! file_exists( $config_file_path ) ) {
			return null;
		}

		// Read and decode the configuration file
		$config_content = file_get_contents( $config_file_path );

		if ( $config_content === false ) {
			return null;
		}

		$decoded_config = json_decode( $config_content, true );

		if ( json_last_error() !== JSON_ERROR_NONE ) {
			return null;
		}

		if ( ! is_array( $decoded_config ) ) {
			return null;
		}

		// Cache the configuration
		$this->deployerConfig = $decoded_config;

		return array( 'deployerConfig' => $decoded_config );
	}
}
