<?php
/**
 * Credorax Payments For WooCommerce
 * https://www.credorax.com/
 *
 * @category Credorax
 * @package  WC_Credorax
 * @author   Girit-Interactive (https://www.girit-tech.com/)
 */

  if (! defined('ABSPATH')) {
      exit; // Exit if accessed directly.
  }

/**
 * Credorax Credorax response factory model.
 */
class WC_Credorax_Model_Response_Factory
{
    /**
     * Set of requests.
     *
     * @var array
     */
    private $invokableClasses = [
        WC_Credorax_Model_Response_AbstractPayment::PAYMENT_AUTH_HANDLER => WC_Credorax_Model_Response_Payment_Auth::class,
        WC_Credorax_Model_Response_AbstractPayment::PAYMENT_AUTH_TOKENIZATION_HANDLER => WC_Credorax_Model_Response_Payment_AuthTokenization::class,
        WC_Credorax_Model_Response_AbstractPayment::PAYMENT_AUTH_USE_TOKEN_HANDLER => WC_Credorax_Model_Response_Payment_AuthUseToken::class,
        WC_Credorax_Model_Response_AbstractPayment::PAYMENT_SALE_HANDLER => WC_Credorax_Model_Response_Payment_Sale::class,
        WC_Credorax_Model_Response_AbstractPayment::PAYMENT_SALE_TOKENIZATION_HANDLER => WC_Credorax_Model_Response_Payment_SaleTokenization::class,
        WC_Credorax_Model_Response_AbstractPayment::PAYMENT_SALE_USE_TOKEN_HANDLER => WC_Credorax_Model_Response_Payment_SaleUseToken::class,
        WC_Credorax_Model_Response_AbstractGateway::GATEWAY_CAPTURE_HANDLER => WC_Credorax_Model_Response_Gateway_Capture::class,
        WC_Credorax_Model_Response_AbstractGateway::GATEWAY_REFUND_HANDLER => WC_Credorax_Model_Response_Gateway_Refund::class,
        WC_Credorax_Model_Response_AbstractGateway::GATEWAY_VOID_HANDLER => WC_Credorax_Model_Response_Gateway_Cancel::class,
    ];

    /**
     * Create response model.
     *
     * @param string         $type
     * @param array          $curl
     * @param WC_Order|null  $payment
     *
     * @return WC_Credorax_Model_ResponseInterface
     * @throws Exception
     */
    public function create(
        $type,
        $curlResponse = [],
        $order = null
    ) {
        $className = !empty($this->invokableClasses[$type])
            ? $this->invokableClasses[$type]
            : null;

        if ($className === null) {
            throw new \Exception(__('%s type is not supported.', 'woocommerce-gateway-credorax'));
        }
        $model = new $className($curlResponse, $order);

        if (!$model instanceof WC_Credorax_Model_ResponseInterface) {
            throw new \Exception(
                __(sprintf('%s doesn\'t implement WC_Credorax_Model_ResponseInterface', $className), 'woocommerce-gateway-credorax')
            );
        }

        return $model;
    }
}
