<commit_before><?php

declare(strict_types = 1);

namespace unreal4u;

use GuzzleHttp\Client;
use unreal4u\Abstracts\TelegramTypes;
use unreal4u\InternalFunctionality\DummyLogger;
use unreal4u\InternalFunctionality\TelegramDocument;
use unreal4u\Abstracts\TelegramMethods;
use unreal4u\Telegram\Types\File;
use Psr\Log\LoggerInterface;

/**
 * The main API which does it all
 */
class TgLog
{
    /**
     * Stores the token
     * @var string
     */
    private $botToken = '';

    /**
     * Contains an instance to a PSR-3 compatible logger
     * @var LoggerInterface
     */
    protected $logger = null;

    /**
     * Stores the API URL from Telegram
     * @var string
     */
    private $apiUrl = '';

    /**
     * With this flag we'll know what type of request to send to Telegram
     *
     * 'application/x-www-form-urlencoded' is the "normal" one, which is simpler and quicker.
     * 'multipart/form-data' should be used only to upload documents, photos, etc.
     *
     * @var string
     */
    private $formType = 'application/x-www-form-urlencoded';

    /**
     * Stores the last method name used
     * @var string
     */
    protected $methodName = '';

    /**
     * TelegramLog constructor.
     * @param string $botToken
     */
    public function __construct(string $botToken, LoggerInterface $logger = null)
    {
        $this->botToken = $botToken;

        if (is_null($logger)) {
            $logger = new DummyLogger();
        }
        $this->logger = $logger;

        $this->constructApiUrl();
    }

    /**
     * Prepares and sends an API request to Telegram
     *
     * @param TelegramMethods $method
     * @return TelegramTypes
     */
    public function performApiRequest(TelegramMethods $method): TelegramTypes
    {
        $this->logger->debug('Going to perform API request, resetting internal class values');
        $this->resetObjectValues();
        $jsonDecoded = $this->sendRequestToTelegram($method, $this->constructFormData($method));

        $returnObject = 'unreal4u\\Telegram\\Types\\' . $method::bindToObjectType();
        $this->logger->debug(sprintf('Decoded response from server, instantiating new %s class', $returnObject));
        return new $returnObject($jsonDecoded['result'], $this->logger);
    }

    /**
     * Will download a file from the Telegram server. Before calling this function, you have to call the getFile method!
     *
     * @see unreal4u\Telegram\Types\File
     * @see unreal4u\Telegram\Methods\GetFile
     *
     * @param File $file
     * @return TelegramDocument
     */
    public function downloadFile(File $file): TelegramDocument
    {
        $this->logger->debug('Downloading file from Telegram, creating URI');
        $url = 'https://api.telegram.org/file/bot' . $this->botToken . '/' . $file->file_path;
        $client = new Client();
        $this->logger->debug('About to perform request');
        return new TelegramDocument($client->get($url));
    }

    /**
     * Builds up the Telegram API url
     * @return TgLog
     */
    final private function constructApiUrl(): TgLog
    {
        $this->apiUrl = 'https://api.telegram.org/bot' . $this->botToken . '/';
        $this->logger->debug('Built up the API URL');
        return $this;
    }

    /**
     * This is the method that actually makes the call, which can be easily overwritten so that our unit tests can work
     *
     * @param TelegramMethods $method
     * @param array $formData
     * @return array
     */
    protected function sendRequestToTelegram(TelegramMethods $method, array $formData): array
    {
        $this->logger->debug('About to instantiate HTTP Client');
        $client = new Client();
        $response = $client->post($this->composeApiMethodUrl($method), $formData);
        $this->logger->debug('Got response back from Telegram, applying json_decode');
        return json_decode((string)$response->getBody(), true);
    }

    private function resetObjectValues(): TgLog
    {
        $this->formType = 'application/x-www-form-urlencoded';<commit_msg>Optimize performance. For example send 3 message before~0.98, after~0.53<commit_after><?php

declare(strict_types = 1);

namespace unreal4u;

use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use unreal4u\Abstracts\TelegramTypes;
use unreal4u\InternalFunctionality\DummyLogger;
use unreal4u\InternalFunctionality\TelegramDocument;
use unreal4u\Abstracts\TelegramMethods;
use unreal4u\Telegram\Types\File;
use Psr\Log\LoggerInterface;

/**
 * The main API which does it all
 */
class TgLog
{
    /**
     * @var ClientInterface
     */
    protected $httpClient;

    /**
     * Stores the token
     * @var string
     */
    private $botToken = '';

    /**
     * Contains an instance to a PSR-3 compatible logger
     * @var LoggerInterface
     */
    protected $logger = null;

    /**
     * Stores the API URL from Telegram
     * @var string
     */
    private $apiUrl = '';

    /**
     * With this flag we'll know what type of request to send to Telegram
     *
     * 'application/x-www-form-urlencoded' is the "normal" one, which is simpler and quicker.
     * 'multipart/form-data' should be used only to upload documents, photos, etc.
     *
     * @var string
     */
    private $formType = 'application/x-www-form-urlencoded';

    /**
     * Stores the last method name used
     * @var string
     */
    protected $methodName = '';

    /**
     * TelegramLog constructor.
     * @param string $botToken
     */
    public function __construct(string $botToken, LoggerInterface $logger = null)
    {
        $this->botToken = $botToken;

        if (is_null($logger)) {
            $logger = new DummyLogger();
        }
        $this->logger = $logger;
        $this->httpClient = new Client();

        $this->constructApiUrl();
    }

    /**
     * Prepares and sends an API request to Telegram
     *
     * @param TelegramMethods $method
     * @return TelegramTypes
     */
    public function performApiRequest(TelegramMethods $method): TelegramTypes
    {
        $this->logger->debug('Going to perform API request, resetting internal class values');
        $this->resetObjectValues();
        $jsonDecoded = $this->sendRequestToTelegram($method, $this->constructFormData($method));

        $returnObject = 'unreal4u\\Telegram\\Types\\' . $method::bindToObjectType();
        $this->logger->debug(sprintf('Decoded response from server, instantiating new %s class', $returnObject));
        return new $returnObject($jsonDecoded['result'], $this->logger);
    }

    /**
     * Will download a file from the Telegram server. Before calling this function, you have to call the getFile method!
     *
     * @see unreal4u\Telegram\Types\File
     * @see unreal4u\Telegram\Methods\GetFile
     *
     * @param File $file
     * @return TelegramDocument
     */
    public function downloadFile(File $file): TelegramDocument
    {
        $this->logger->debug('Downloading file from Telegram, creating URI');
        $url = 'https://api.telegram.org/file/bot' . $this->botToken . '/' . $file->file_path;
        $this->logger->debug('About to perform request');
        return new TelegramDocument($this->httpClient->get($url));
    }

    /**
     * Builds up the Telegram API url
     * @return TgLog
     */
    final private function constructApiUrl(): TgLog
    {
        $this->apiUrl = 'https://api.telegram.org/bot' . $this->botToken . '/';
        $this->logger->debug('Built up the API URL');
        return $this;
    }

    /**
     * This is the method that actually makes the call, which can be easily overwritten so that our unit tests can work
     *
     * @param TelegramMethods $method
     * @param array $formData
     * @return array
     */
    protected function sendRequestToTelegram(TelegramMethods $method, array $formData): array
    {
        $this->logger->debug('About to instantiate HTTP Client');
        $response = $this->httpClient->post($this->composeApiMethodUrl($method), $formData);
        $this->logger->debug('Got response back from Telegram, applying json_decode');
        return json_decode((string)$response->getBody(), true);
    }

    private function resetObjectValues(): TgLog
    {
        $this->formType = 'application/x-www-form-urlencoded';
{'fraction_non_alphanumeric': 0.123147367152669, 'fraction_numerical': 0.003494396915290999, 'mean_word_length': 2.688888888888889, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 66, 'https://': 4, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '174540', 'n_tokens_mistral': 2432, 'n_tokens_neox': 2353, 'n_words': 638}