<?php
/**
 * Config provider for the repository config for "exclusions".
 *
 * @package Ealab\Deployer
 */

namespace Ealab\Deployer\Tracking\Config\Exclusions;

use Ealab\Deployer\Tracking\Config\ConfigProviderBase;
use Ealab\Deployer\Tracking\Config\RepositoryConfigProviderInterface;
use Ealab\Deployer\Utility\DeployerConfigRepository\DeployerConfig;
use Ealab\Deployer\Utility\DeployerConfigRepository\InstalledPluginKeyConfigFilter;
use Ealab\Deployer\Utility\DeployerConfigRepository\InstalledPluginsConfigFilter;

/**
 * Config provider for the repository config for "exclusions".
 */
class ExclusionsRepositoryConfigProvider extends ConfigProviderBase implements
	RepositoryConfigProviderInterface {

	/**
	 * {@inheritDoc}
	 */
	public function collect(): ?array {
		// Load the repository configuration.
		$repo_config = DeployerConfig::getConfig(
			'exclusions',
			array(
				// Filter out using the quick filter first.
				InstalledPluginKeyConfigFilter::class,
				// Filter out using the version filter.
				InstalledPluginsConfigFilter::class,
			),
		);

		$this->config = array();

		// Build the final configuration.
		foreach ( $repo_config as $plugin_slug => $items ) {
			// In theory, there should be only a single item per plugin.
			// We simply take the last one.
			$plugin_level_config = end( $items );

			// The real config is nested under the 'value' key.
			$this->config[ $plugin_slug ] = json_decode( $plugin_level_config['value'], true );
		}

		return $this->config;
	}
}
