<commit_before><?php

declare(strict_types=1);

/*
 * This file is part of gpupo/common-sdk
 * Created by <NAME> <<EMAIL>>
 * For the information of copyright and license you should read the file
 * LICENSE which is distributed with this source code.
 * Para a informação dos direitos autorais e de licença você deve ler o arquivo
 * LICENSE que é distribuído com este código-fonte.
 * Para obtener la información de los derechos de autor y la licencia debe leer
 * el archivo LICENSE que se distribuye con el código fuente.
 * For more information, see <https://opensource.gpupo.com/>.
 *
 */

namespace Gpupo\CommonSdk\Client;

use Gpupo\CommonSdk\Exception\ClientException;
use Gpupo\CommonSdk\Exception\RequestException;
use Gpupo\CommonSdk\Request;
use Gpupo\CommonSdk\Response;
use Gpupo\CommonSdk\Transport;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

abstract class ClientAbstract extends BoardAbstract
{
    const PROTOCOL = 'https';

    const ENDPOINT = 'api.localhost';

    const CONTENT_TYPE_DEFAULT = 'application/json;charset=UTF-8';

    const ACCEPT_DEFAULT = 'application/json;charset=UTF-8';

    const CACHE_TTL = 3600;

    protected $mode;

    public function getDefaultOptions(): array
    {
        return [
            'client_id' => false,
            'client_secret' => false,
            'access_token' => false,
            'user_id' => false,
            'refresh_token' => false,
            'users_url' => sprintf('%s://%s/users', $this::PROTOCOL, $this::ENDPOINT),
            'base_url' => sprintf('%s://%s', $this::PROTOCOL, $this::ENDPOINT),
            'oauth_url' => sprintf('%s://%s/oauth', $this::PROTOCOL, $this::ENDPOINT),
            'common_schema_namespace' => '\\Gpupo\\CommonSchema\\ORM',
            'verbose' => true,
            'cacheTTL' => $this::CACHE_TTL,
            'offset' => 0,
            'limit' => 30,
        ];
    }

    public function setMode($mode): void
    {
        $this->mode = $mode;
    }

    public function getMode()
    {
        return $this->mode;
    }

    public function factoryRequest(string $resource, string $method = '', bool $destroyCache = false): Request
    {
        if (false !== $destroyCache) {
            $this->destroyCache($resource);
        }

        $request = new Request();

        if (!empty($method)) {
            $request->setMethod($method);
        }

        $request
            ->setTransport($this->factoryTransport())
            ->setHeader($this->renderHeader())
            ->setUrl($this->getResourceUri($resource));

        return $request;
    }

    public function downloadFile(string $resource, string $filename = null)
    {
        $request = $this->factoryRequest($resource);
        $data = $request->exec();

        return file_put_contents($filename, $data['responseRaw']);
    }

    public function get(string $resource, int $ttl = null, string $method = ''): Response
    {
        $request = $this->factoryRequest($resource, $method);

        //Cache
        if (null === $ttl && $this->hasSimpleCache() && 'DELETE' !== $method) {
            $cacheId = $this->simpleCacheGenerateId($resource);

            if ($this->getSimpleCache()->has($cacheId)) {
                $response = $this->getSimpleCache()->get($cacheId);

                return $response;<commit_msg>split abstract  methods<commit_after><?php

declare(strict_types=1);

/*
 * This file is part of gpupo/common-sdk
 * Created by <NAME> <<EMAIL>>
 * For the information of copyright and license you should read the file
 * LICENSE which is distributed with this source code.
 * Para a informação dos direitos autorais e de licença você deve ler o arquivo
 * LICENSE que é distribuído com este código-fonte.
 * Para obtener la información de los derechos de autor y la licencia debe leer
 * el archivo LICENSE que se distribuye con el código fuente.
 * For more information, see <https://opensource.gpupo.com/>.
 *
 */

namespace Gpupo\CommonSdk\Client;

use Gpupo\CommonSdk\Exception\ClientException;
use Gpupo\CommonSdk\Exception\RequestException;
use Gpupo\CommonSdk\Request;
use Gpupo\CommonSdk\Response;
use Gpupo\CommonSdk\Transport;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

abstract class ClientAbstract extends BoardAbstract
{
    const PROTOCOL = 'https';

    const ENDPOINT = 'api.localhost';

    const CONTENT_TYPE_DEFAULT = 'application/json;charset=UTF-8';

    const ACCEPT_DEFAULT = 'application/json;charset=UTF-8';

    const CACHE_TTL = 3600;

    protected $mode;

    public function getDefaultOptions(): array
    {
        return [
            'client_id' => false,
            'client_secret' => false,
            'access_token' => false,
            'user_id' => false,
            'refresh_token' => false,
            'users_url' => sprintf('%s://%s/users', $this::PROTOCOL, $this::ENDPOINT),
            'base_url' => sprintf('%s://%s', $this::PROTOCOL, $this::ENDPOINT),
            'oauth_url' => sprintf('%s://%s/oauth', $this::PROTOCOL, $this::ENDPOINT),
            'common_schema_namespace' => '\\Gpupo\\CommonSchema\\ORM',
            'verbose' => true,
            'cacheTTL' => $this::CACHE_TTL,
            'offset' => 0,
            'limit' => 30,
        ];
    }

    public function setMode($mode): void
    {
        $this->mode = $mode;
    }

    public function getMode()
    {
        return $this->mode;
    }

    public function factoryRequest(string $resource, string $method = '', bool $destroyCache = false): Request
    {
        if (false !== $destroyCache) {
            $this->destroyCache($resource);
        }

        $request = new Request();

        if (!empty($method)) {
            $request->setMethod($method);
        }

        $request
            ->setTransport($this->factoryTransport())
            ->setHeader($this->renderHeader())
            ->setUrl($this->getResourceUri($resource));

        return $request;
    }

    public function downloadFile(string $resource, string $filename = null)
    {
        $request = $this->factoryRequest($resource);

        return $this->downloadFileByRequest($request, $filename);
    }

    public function downloadFileByRequest(Request $request, string $filename = null)
    {
        $data = $request->exec();

        return file_put_contents($filename, $data['responseRaw']);
    }

    public function get(string $resource, int $ttl = null, string $method = ''): Response
    {
        $request = $this->factoryRequest($resource, $method);

        //Cache
        if (null === $ttl && $this->hasSimpleCache() && 'DELETE' !== $method) {
            $cacheId = $this->simpleCacheGenerateId($resource);

            if ($this->getSimpleCache()->has($cacheId)) {
                $response = $this->getSimpleCache()->get($cacheId);

                return $response;
{'fraction_non_alphanumeric': 0.13666133022849658, 'fraction_numerical': 0.0029107844564110028, 'mean_word_length': 2.682743837084673, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 76, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6175385', 'n_tokens_mistral': 2057, 'n_tokens_neox': 1987, 'n_words': 483}