<?php

declare(strict_types=1);

/*
 * PaypalServerSDKLib
 *
 * This file was automatically generated by APIMATIC v3.0 ( https://www.apimatic.io ).
 */

namespace PaypalServerSDKLib\Tests;

use Core\Types\CallbackCatcher;
use PaypalServerSDKLib\Authentication\ClientCredentialsAuthCredentialsBuilder;
use PaypalServerSDKLib\Environment;
use PaypalServerSDKLib\PaypalServerSDKClient;
use PaypalServerSDKLib\PaypalServerSDKClientBuilder;

class ClientFactory
{
    public static function create(CallbackCatcher $httpCallback): PaypalServerSDKClient
    {
        $clientBuilder = PaypalServerSDKClientBuilder::init();
        $clientBuilder = self::addConfigurationFromEnvironment($clientBuilder);
        $clientBuilder = self::addTestConfiguration($clientBuilder);
        return $clientBuilder->httpCallback($httpCallback)->build();
    }

    public static function addTestConfiguration(PaypalServerSDKClientBuilder $builder): PaypalServerSDKClientBuilder
    {
        return $builder;
    }

    public static function addConfigurationFromEnvironment(
        PaypalServerSDKClientBuilder $builder
    ): PaypalServerSDKClientBuilder {
        $timeout = '10000';
        $environment = Environment::SANDBOX;
        $oAuthClientId = getenv('CLIENT_ID');
        $oAuthClientSecret = getenv('CLIENT_SECRET');

        if (!empty($timeout) && \is_numeric($timeout)) {
            $builder->timeout(intval($timeout));
        }

        if (!empty($environment)) {
            $builder->environment($environment);
        }

        if (!empty($oAuthClientId) && !empty($oAuthClientSecret)) {
            $builder->clientCredentialsAuthCredentials(
                ClientCredentialsAuthCredentialsBuilder::init($oAuthClientId, $oAuthClientSecret)
            );
        }

        return $builder;
    }
}
