<?php
/**
 * A factory that builds a wrapper around the payment providers.
 *
 * @since 3.0.0
 */

declare(strict_types=1);
namespace FeData\FastEvents\Payment;

defined( 'ABSPATH' ) || exit;


use FeData\FastEvents\Logging\LoggerFactory;
use FeData\FastEvents\Storage\PluginSettings;

/**
 * A class for creating a reference to the used payment provider in the plugin settings.
 *
 * @mixin PaymentInterface
 */
final class PaymentFactory {

	/**
	 * Payment provider that implemented the PaymentInterface.
	 *
	 * @since 3.0.0
	 * @var PaymentInterface
	 */
	private PaymentInterface $payment;

	/**
	 * PaymentFactory constructor.
	 *
	 * @since 3.0.0
	 *
	 * @param PluginSettings $settings Plugin settings.
	 * @param LoggerFactory  $logger   Logger.
	 */
	public function __construct( private readonly PluginSettings $settings, private readonly LoggerFactory $logger ) {
		global $fevt_container;

		$config = $this->settings->payment_config();

		if ( empty( $config->provider->class ) ) {
			$this->logger->alert( __( 'Empty payment provider class', 'fast-events' ) );
		}
		$class = $config->provider->class;
		if ( ! class_exists( $class ) ) {
			/* translators: %d is the class name */
			$this->logger->alert( sprintf( esc_html__( 'Payment class "%1$s" not found', 'fast-events' ), $config->provider->class ) );
		}
		$this->payment = $fevt_container->get( $class );
	}

	/**
	 * Singletons should not be cloneable.
	 *
	 * @since 2.5.0
	 */
	private function __clone(): void {}

	/**
	 * Prevent from being unserialized.
	 *
	 * @since 2.5.0
	 */
	public function __wakeup(): void {}

	/**
	 * Forward any other method to the payment provider.
	 *
	 * @since 2.5.0
	 *
	 * @param string $method Provider method name.
	 * @param array  $args   Arguments.
	 * @return mixed
	 */
	public function __call( string $method, array $args ) {
		return $this->payment->$method( ...$args );
	}
}
