<?php
/**
 * Handles the transmutation of host names.
 *
 * @package Ealab\Deployer
 */

namespace Ealab\Deployer\Tracking\ItemValueHandler\ValueProcessor\TransmutationHandler;

use Ealab\Deployer\Exception\IgnoredTransmutationException;
use Ealab\Deployer\Exception\WpDeployException;

/**
 * Handles the transmutation of host names.
 *
 * This handler is different from the UrlHostNameProcessor in that it is able
 * to handle the transmutation in nested serialized data.
 */
class HostNameTransmutationHandler extends TransmutationHandlerBase {
	/**
	 * Gets the exported representation of the given value.
	 *
	 * @param string $value The value to process.
	 *
	 * @return string The exported representation of the given value.
	 *
	 * @throws IgnoredTransmutationException Could not transmute the value.
	 */
	public static function getExportRepresentation( string $value ): string {
		if ( empty( $value ) ) {
			throw new IgnoredTransmutationException( _dpgettext( 'deployer-core', 'Transmutation handler', 'HostNameTransmutationHandler: The value does not need transmutation.' ) );
		}

		$current_host_name = untrailingslashit( get_site_url() );

		return str_replace( $current_host_name, static::getProcessorPlaceholder(), $value );
	}

	/**
	 * {@inheritDoc}
	 */
	public static function getId(): string {
		return 'host_name';
	}

	/**
	 * Gets the imported representation of the given value.
	 *
	 * @param string $value The value to process.
	 *
	 * @return string The imported representation of the given value.
	 *
	 * @throws IgnoredTransmutationException|WpDeployException Could not transmute the value.
	 */
	public static function getImportRepresentation( string $value ): string {
		if ( empty( $value ) ) {
			return $value;
		}

		$current_host_name = untrailingslashit( get_site_url() );

		return str_replace( static::getProcessorPlaceholder(), $current_host_name, $value );
	}

	/**
	 * {@inheritDoc}
	 */
	static public function getPublicName(): string {
		return _dpgettext( 'deployer-core', 'Transmutation handler public name', 'Host name' );
	}

	/**
	 * {@inheritDoc}
	 */
	public function toExport( $value, ?array $options = array() ) {
		if ( ! is_string( $value ) ) {
			// This is not a file path. Leave this attribute value as is.
			throw new IgnoredTransmutationException();
		}

		$encoded_file_data = static::getExportRepresentation( $value );

		// Finally encapsulate the attribute value in a placeholder.
		return static::getProcessorPlaceholder( $encoded_file_data );
	}

	/**
	 * {@inheritdoc}
	 */
	public function toImportFinalize( $value, ?array $options = array() ) {
		return static::applyOnPlaceholders(
			$value,
			function ( array $regex_match ) {
				return static::getImportRepresentation( $regex_match[3] );
			}
		);
	}

	/**
	 * {@inheritdoc}
	 */
	public function toImportPresave( $value, ?array $options = array() ) {
		return static::applyOnPlaceholders(
			$value,
			function ( array $regex_match ) {
				return static::getImportRepresentation( $regex_match[3] );
			}
		);
	}
}
