<?php

namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory;

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Connector\Client\CurlHttpClient;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Connector\Client\Options\CurlHttpClientOptions;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\Fields\ExportFileTypeFormFields;
use RuntimeException;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Service\Dependency\Resolver\Abstraction\DependencyResolverInterface;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Connector\Abstraction\Connector;
/**
 * Class ConnectorClientFactory, connector client factory.
 *
 * @package WPDesk\Library\DropshippingExport\Factory
 */
class ConnectorClientFactory
{
    const FILTER_BYPASS_SSL = 'wpdesk_dropshipping_bypass_ssl';
    const FILTER_HTTP_CLIENT_OPTIONS = 'wpdesk_dropshipping_curl_http_client_options';
    /**
     * @var DependencyResolverInterface
     */
    protected $resolver;
    public function __construct(DependencyResolverInterface $dependency_resolver)
    {
        $this->resolver = $dependency_resolver;
    }
    public function create_client(array $parameters): Connector
    {
        $clients = $this->get_clients();
        if (isset($parameters[ExportFileTypeFormFields::CLIENT]) && array_key_exists($parameters[ExportFileTypeFormFields::CLIENT], $clients)) {
            $callback = $clients[$parameters[ExportFileTypeFormFields::CLIENT]];
            return $callback($parameters);
        }
        throw new RuntimeException('Error connection: client does not exist.');
    }
    protected function get_clients(): array
    {
        return [ExportFileTypeFormFields::CLIENT_CURL => function (array $parameters) {
            if (isset($parameters[ExportFileTypeFormFields::FILE_URL])) {
                /**
                 * @var CurlHttpClientOptions;
                 */
                $options = $this->resolver->resolve(CurlHttpClientOptions::class);
                $options->set_url($parameters[ExportFileTypeFormFields::FILE_URL]);
                if (\true === apply_filters(ConnectorClientFactory::FILTER_BYPASS_SSL, \false)) {
                    $options->set_ssl_verify_peer(\false);
                    $options->set_ssl_verify_host(0);
                }
                $options = \apply_filters(self::FILTER_HTTP_CLIENT_OPTIONS, $options);
                return $this->resolver->resolve(CurlHttpClient::class, ['options' => $options]);
            }
            throw new RuntimeException('Error, client used for connection is empty');
        }];
    }
}
