<commit_before><?php

namespace Keboola\Test\ClientProvider;

use Keboola\StorageApi\BranchAwareClient;
use Keboola\StorageApi\Client;
use Keboola\StorageApi\DevBranches;
use Keboola\Test\StorageApiTestCase;

class ClientProvider
{

    const DEFAULT_BRANCH = 'defaultBranch';
    const DEV_BRANCH = 'devBranch';

    /**
     * @var StorageApiTestCase
     */
    private $testCase;

    /**
     * @var scalar
     */
    private $dataProviderKey;

    public function __construct(StorageApiTestCase $testCase, $dataProviderKey = 0)
    {
        $this->testCase = $testCase;
        $this->dataProviderKey = $dataProviderKey;
    }

    // CREATORS

    /**
     * @param array $config
     * @param bool $useExistingBranch
     * @return Client
     */
    public function createClientForCurrentTest($config = [], $useExistingBranch = false)
    {
        if ($this->testCase->usesDataProvider()) {
            if ($this->testCase->getProvidedData()[$this->dataProviderKey] === self::DEFAULT_BRANCH) {
                return $this->getDefaultClient($config);
            } elseif ($this->testCase->getProvidedData()[$this->dataProviderKey] === self::DEV_BRANCH) {
                return $this->getDevBranchClient($config, $useExistingBranch);
            }
        }

        return $this->getDefaultClient();
    }

    /**
     * @param array $config
     * @param bool $useExistingBranch
     * @return BranchAwareClient
     */
    public function createBranchAwareClientForCurrentTest($config = [], $useExistingBranch = false)
    {
        if ($this->testCase->usesDataProvider()) {
            if ($this->testCase->getProvidedData()[$this->dataProviderKey] === self::DEFAULT_BRANCH) {
                return $this->getDefaultBranchClient($config);
            } elseif ($this->testCase->getProvidedData()[$this->dataProviderKey] === self::DEV_BRANCH) {
                return $this->getDevBranchClient($config, $useExistingBranch);
            }
        }

        return $this->getDefaultBranchClient();
    }

    // CLIENTS

    /**
     * @param array $config
     * @return Client
     */
    public function getDefaultClient($config = [])
    {
        if ($config) {
            return $this->testCase->getClient($config);
        } else {
            return $this->testCase->getDefaultClient();
        }
    }

    /**
     * @param array $config
     * @return BranchAwareClient
     */
    public function getDefaultBranchClient($config = [])
    {
        $branchId = $this->testCase->getDefaultBranchId($this->testCase);
        
        if ($config) {
            return $this->testCase->getBranchAwareClient($branchId, $config);
        } else {
            return $this->testCase->getBranchAwareDefaultClient($branchId);
        }
    }

    /**
     * @param array $config
     * @param bool $useExistingBranch
     * @return BranchAwareClient
     */
    public function getDevBranchClient($config = [], $useExistingBranch = false)
    {
        if ($useExistingBranch) {
            $branch = $this->getExistingBranchForTestCase();
        } else {
            $branch = $this->createDevBranchForTestCase();
        }

        if ($config) {
            return $this->testCase->getBranchAwareClient($branch['id'], $config);
        } else {
            return $this->testCase->getBranchAwareDefaultClient($branch['id']);
        }
    }

    // HELPERS

    /**
     * @return string
     */
    public function getDevBranchName()
    {
        $providedToken = $this->getDefaultClient()->verifyToken();
        return implode('\\', [
            __CLASS__,
            $this->testCase->getName(false),<commit_msg>KBC-1977 add Guzzle client to ClientProvider<commit_after><?php

namespace Keboola\Test\ClientProvider;

use GuzzleHttp\Client as GuzzleClient;
use Keboola\StorageApi\BranchAwareClient;
use Keboola\StorageApi\BranchAwareGuzzleClient;
use Keboola\StorageApi\Client;
use Keboola\StorageApi\DevBranches;
use Keboola\Test\StorageApiTestCase;

class ClientProvider
{

    const DEFAULT_BRANCH = 'defaultBranch';
    const DEV_BRANCH = 'devBranch';

    /**
     * @var StorageApiTestCase
     */
    private $testCase;

    /**
     * @var scalar
     */
    private $dataProviderKey;

    public function __construct(StorageApiTestCase $testCase, $dataProviderKey = 0)
    {
        $this->testCase = $testCase;
        $this->dataProviderKey = $dataProviderKey;
    }

    // CREATORS

    /**
     * @param array $config
     * @param bool $useExistingBranch
     * @return Client
     */
    public function createClientForCurrentTest($config = [], $useExistingBranch = false)
    {
        if ($this->testCase->usesDataProvider()) {
            if ($this->testCase->getProvidedData()[$this->dataProviderKey] === self::DEFAULT_BRANCH) {
                return $this->getDefaultClient($config);
            } elseif ($this->testCase->getProvidedData()[$this->dataProviderKey] === self::DEV_BRANCH) {
                return $this->getDevBranchClient($config, $useExistingBranch);
            }
        }

        return $this->getDefaultClient();
    }

    /**
     * @param array $config
     * @param bool $useExistingBranch
     * @return BranchAwareClient
     */
    public function createBranchAwareClientForCurrentTest($config = [], $useExistingBranch = false)
    {
        if ($this->testCase->usesDataProvider()) {
            if ($this->testCase->getProvidedData()[$this->dataProviderKey] === self::DEFAULT_BRANCH) {
                return $this->getDefaultBranchClient($config);
            } elseif ($this->testCase->getProvidedData()[$this->dataProviderKey] === self::DEV_BRANCH) {
                return $this->getDevBranchClient($config, $useExistingBranch);
            }
        }

        return $this->getDefaultBranchClient();
    }

    /**
     * @param array $config
     * @param bool $useExistingBranch
     * @return GuzzleClient
     */
    public function createGuzzleClientForCurrentTest($config, $useExistingBranch = false)
    {
        if ($this->testCase->usesDataProvider()) {
            if ($this->testCase->getProvidedData()[$this->dataProviderKey] === self::DEFAULT_BRANCH) {
                return $this->getGuzzleClient($config);
            } elseif ($this->testCase->getProvidedData()[$this->dataProviderKey] === self::DEV_BRANCH) {
                return $this->getBranchAwareGuzzleClient($config, $useExistingBranch);
            }
        }

        return $this->getGuzzleClient($config);
    }

    // CLIENTS

    /**
     * @param array $config
     * @return Client
     */
    public function getDefaultClient($config = [])
    {
        if ($config) {
            return $this->testCase->getClient($config);
        } else {
            return $this->testCase->getDefaultClient();
        }
    }

    /**
     * @param array $config
     * @return BranchAwareClient
     */
    public function getDefaultBranchClient($config = [])
    {
        $branchId = $this->testCase->getDefaultBranchId($this->testCase);
        
        if ($config) {
            return $this->testCase->getBranchAwareClient($branchId, $config);
        } else {
            return $this->testCase->getBranchAwareDefaultClient($branchId);
        }
    }

    /**
     * @param array $config
     * @param bool $useExistingBranch
     * @return BranchAwareClient
     */
    public function getDevBranchClient($config = [], $useExistingBranch = false)
    {
        if ($useExistingBranch) {
            $branch = $this->getExistingBranchForTestCase();
        } else {
            $branch = $this->createDevBranchForTestCase();
        }

        if ($config) {
            return $this->testCase->getBranchAwareClient($branch['id'], $config);
        } else {
            return $this->testCase->getBranchAwareDefaultClient($branch['id']);
        }
    }

    /**
     * @param array $config
     * @return GuzzleClient
     */
    public function getGuzzleClient($config = [])
    {
        return new GuzzleClient($config);
    }

    /**
     * @param array $config
     * @param bool $useExistingBranch
     * @return BranchAwareGuzzleClient
     */
    public function getBranchAwareGuzzleClient($config = [], $useExistingBranch = false)
    {
        if ($useExistingBranch) {
            $branch = $this->getExistingBranchForTestCase();
        } else {
            $branch = $this->createDevBranchForTestCase();
        }

        return new BranchAwareGuzzleClient($branch['id'], $config);
    }

    // HELPERS

    /**
     * @return string
     */
    public function getDevBranchName()
    {
        $providedToken = $this->getDefaultClient()->verifyToken();
        return implode('\\', [
            __CLASS__,
            $this->testCase->getName(false),
{'fraction_non_alphanumeric': 0.13311985361390669, 'fraction_numerical': 0.0006861848124428179, 'mean_word_length': 2.418686473807662, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 106, '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': '3616751', 'n_tokens_mistral': 2526, 'n_tokens_neox': 2532, 'n_words': 409}