<commit_before><?php
namespace Payum\OmnipayBridge;

use Omnipay\Common\Exception\OmnipayException;
use Omnipay\Common\GatewayFactory as OmnipayOmnipayGatewayFactory;
use Omnipay\Common\GatewayInterface;
use Omnipay\Omnipay;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\LogicException;
use Payum\Core\GatewayFactory;
use Payum\Core\GatewayFactoryInterface;
use Payum\OmnipayBridge\Action\CaptureAction;
use Payum\OmnipayBridge\Action\ConvertPaymentAction;
use Payum\OmnipayBridge\Action\OffsiteCaptureAction;
use Payum\OmnipayBridge\Action\StatusAction;

class OmnipayGatewayFactory extends GatewayFactory
{
    /**
     * @var string
     */
    private $omnipayGatewayTypeOrClass;

    /**
     * @var OmnipayOmnipayGatewayFactory|null
     */
    private $omnipayGatewayFactory;

    /**
     * {@inheritDoc}
     *
     * @param string $omnipayGatewayTypeOrClass
     * @param OmnipayOmnipayGatewayFactory|null $omnipayGatewayFactory
     */
    public function __construct($omnipayGatewayTypeOrClass = null, OmnipayOmnipayGatewayFactory $omnipayGatewayFactory = null, array $defaultConfig = array(), GatewayFactoryInterface $coreGatewayFactory = null)
    {
        parent::__construct($defaultConfig, $coreGatewayFactory);

        $this->omnipayGatewayTypeOrClass = $omnipayGatewayTypeOrClass;
        $this->omnipayGatewayFactory = $omnipayGatewayFactory ?: Omnipay::getFactory();
    }

    /**
     * {@inheritDoc}
     */
    protected function populateConfig(ArrayObject $config)
    {
        $config->defaults([
            'payum.action.capture' => new CaptureAction(),
            'payum.action.capture_offsite' => new OffsiteCaptureAction(),
            'payum.action.convert_payment' => new ConvertPaymentAction(),
            'payum.action.status' => new StatusAction(),
        ]);

        if (false == $config['payum.api']) {
            //BC layer
            if ($config['options']) {
                $config->defaults($config['options']);
            }

            $config->defaults([
                'type' => $this->omnipayGatewayTypeOrClass,
            ]);

            // omnipay does not provide required options.
            $config['payum.required_options'] = ['type'];

            $gateway = null;
            if ($config['type']) {
                try {
                    /** @var GatewayInterface $gateway */
                    $gateway = $this->omnipayGatewayFactory->create($config['type']);
                } catch (OmnipayException $e) {
                    throw new LogicException(sprintf(
                        'Given omnipay gateway type %s or class is not supported. Supported: %s',
                        $config['type'],
                        implode(', ', $this->omnipayGatewayFactory->getSupportedGateways())
                    ));
                }

                $config['payum.default_options'] = array_replace(['testMode' => true], $gateway->getDefaultParameters());
                $config->defaults($config['payum.default_options']);
            }

            $config['payum.api'] = function (ArrayObject $config) use ($gateway) {
                $config->validateNotEmpty($config['payum.required_options']);

                $gateway->initialize((array)$config);

                return $gateway;
            };
        }
    }
}<commit_msg>add previous exception.<commit_after><?php
namespace Payum\OmnipayBridge;

use Omnipay\Common\Exception\OmnipayException;
use Omnipay\Common\GatewayFactory as OmnipayOmnipayGatewayFactory;
use Omnipay\Common\GatewayInterface;
use Omnipay\Omnipay;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\LogicException;
use Payum\Core\GatewayFactory;
use Payum\Core\GatewayFactoryInterface;
use Payum\OmnipayBridge\Action\CaptureAction;
use Payum\OmnipayBridge\Action\ConvertPaymentAction;
use Payum\OmnipayBridge\Action\OffsiteCaptureAction;
use Payum\OmnipayBridge\Action\StatusAction;

class OmnipayGatewayFactory extends GatewayFactory
{
    /**
     * @var string
     */
    private $omnipayGatewayTypeOrClass;

    /**
     * @var OmnipayOmnipayGatewayFactory|null
     */
    private $omnipayGatewayFactory;

    /**
     * {@inheritDoc}
     *
     * @param string $omnipayGatewayTypeOrClass
     * @param OmnipayOmnipayGatewayFactory|null $omnipayGatewayFactory
     */
    public function __construct($omnipayGatewayTypeOrClass = null, OmnipayOmnipayGatewayFactory $omnipayGatewayFactory = null, array $defaultConfig = array(), GatewayFactoryInterface $coreGatewayFactory = null)
    {
        parent::__construct($defaultConfig, $coreGatewayFactory);

        $this->omnipayGatewayTypeOrClass = $omnipayGatewayTypeOrClass;
        $this->omnipayGatewayFactory = $omnipayGatewayFactory ?: Omnipay::getFactory();
    }

    /**
     * {@inheritDoc}
     */
    protected function populateConfig(ArrayObject $config)
    {
        $config->defaults([
            'payum.action.capture' => new CaptureAction(),
            'payum.action.capture_offsite' => new OffsiteCaptureAction(),
            'payum.action.convert_payment' => new ConvertPaymentAction(),
            'payum.action.status' => new StatusAction(),
        ]);

        if (false == $config['payum.api']) {
            //BC layer
            if ($config['options']) {
                $config->defaults($config['options']);
            }

            $config->defaults([
                'type' => $this->omnipayGatewayTypeOrClass,
            ]);

            // omnipay does not provide required options.
            $config['payum.required_options'] = ['type'];

            $gateway = null;
            if ($config['type']) {
                try {
                    /** @var GatewayInterface $gateway */
                    $gateway = $this->omnipayGatewayFactory->create($config['type']);
                } catch (OmnipayException $e) {
                    throw new LogicException(sprintf(
                        'Given omnipay gateway type %s or class is not supported. Supported: %s',
                        $config['type'],
                        implode(', ', $this->omnipayGatewayFactory->getSupportedGateways())
                    ), 0, $e);
                }

                $config['payum.default_options'] = array_replace(['testMode' => true], $gateway->getDefaultParameters());
                $config->defaults($config['payum.default_options']);
            }

            $config['payum.api'] = function (ArrayObject $config) use ($gateway) {
                $config->validateNotEmpty($config['payum.required_options']);

                $gateway->initialize((array)$config);

                return $gateway;
            };
        }
    }
}
{'fraction_non_alphanumeric': 0.12011382357346113, 'fraction_numerical': 0.0001497678598172832, 'mean_word_length': 2.460103626943005, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6514005', 'n_tokens_mistral': 1937, 'n_tokens_neox': 1872, 'n_words': 306}