<commit_before><?php

/**
 * This file is part of the komtet/kassa-sdk library
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Komtet\KassaSdk;

use Komtet\KassaSdk\Exception\ClientException;
use Psr\Log\LogLevel;
use Psr\Log\LoggerInterface;

class Client
{
    /**
     * @var string
     */
    private $host = 'https://kassa.komtet.ru';

    /**
     * @var string
     */
    private $partner = null;

    /**
     * @var string
     */
    private $key;

    /**
     * @var string
     */
    private $secret;

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @var array A list of headers to be masked in logs
     */
    private $maskedHeaders = ['Authorization', 'X-HMAC-Signature'];

    /**
     * @param string $key Shop ID
     * @param string $secret Secret key
     * @param LoggerInterface $logger PSR Logger
     *
     * @return Client
     */
    public function __construct($key, $secret, LoggerInterface $logger = null)
    {
        $this->key = $key;
        $this->secret = $secret;
        $this->logger = $logger;
    }

    /**
     * @param string $value
     *
     * @return Client
     */
    public function setHost($value)
    {
        $this->host = $value;

        return $this;
    }

    /**
     * @param string $value
     *
     * @return Client
     */
    public function setPartner($value)
    {
        $this->partner = $value;

        return $this;
    }

    /**
     * @param string $path
     * @param mixed $data
     *
     * @return mixed
     */
    public function sendRequest($path, $data = null, $method = null)
    {
        if (is_array($data)) {
            $data = json_encode($data);
        } elseif ($data) {
            throw new InvalidArgumentException('Unexpected type of $data, excepts array or null');
        }

        if (!$method) {
            $method = $data !== null ? 'POST' : 'GET';
        }

        if (class_exists('Psr\Log\LogLevel')) {
            $log_level_debug = LogLevel::DEBUG;
            $log_level_warning = LogLevel::WARNING;
        }
        else {
            $log_level_debug = 'debug';
            $log_level_warning = 'warning';
        }

        $url = sprintf('%s/%s', $this->host, $path);
        $signature = hash_hmac('md5', $method . $url . ($data ? $data : ''), $this->secret);

        $headers = [
            'Accept: application/json',
            sprintf('Authorization: %s', $this->key),
            sprintf('X-HMAC-Signature: %s', $signature)
        ];
        if (!empty($this->partner)) {
            $headers[] = sprintf('X-Partner-ID: %s', $this->partner);
        }
        if (in_array($method, array('POST', 'PUT'))) {
            $headers[] = 'Content-Type: application/json';
        }

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        if ($method == 'POST') {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        $response = curl_exec($ch);

        $this->log($log_level_debug, 'request: url={url} headers={headers} data={data}', [
            'url' => $url,
            'headers' => $this->maskHeaders($headers),
            'data' => $data<commit_msg>add data for put method<commit_after><?php

/**
 * This file is part of the komtet/kassa-sdk library
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Komtet\KassaSdk;

use Komtet\KassaSdk\Exception\ClientException;
use Psr\Log\LogLevel;
use Psr\Log\LoggerInterface;

class Client
{
    /**
     * @var string
     */
    private $host = 'https://kassa.komtet.ru';

    /**
     * @var string
     */
    private $partner = null;

    /**
     * @var string
     */
    private $key;

    /**
     * @var string
     */
    private $secret;

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @var array A list of headers to be masked in logs
     */
    private $maskedHeaders = ['Authorization', 'X-HMAC-Signature'];

    /**
     * @param string $key Shop ID
     * @param string $secret Secret key
     * @param LoggerInterface $logger PSR Logger
     *
     * @return Client
     */
    public function __construct($key, $secret, LoggerInterface $logger = null)
    {
        $this->key = $key;
        $this->secret = $secret;
        $this->logger = $logger;
    }

    /**
     * @param string $value
     *
     * @return Client
     */
    public function setHost($value)
    {
        $this->host = $value;

        return $this;
    }

    /**
     * @param string $value
     *
     * @return Client
     */
    public function setPartner($value)
    {
        $this->partner = $value;

        return $this;
    }

    /**
     * @param string $path
     * @param mixed $data
     *
     * @return mixed
     */
    public function sendRequest($path, $data = null, $method = null)
    {
        if (is_array($data)) {
            $data = json_encode($data);
        } elseif ($data) {
            throw new InvalidArgumentException('Unexpected type of $data, excepts array or null');
        }

        if (!$method) {
            $method = $data !== null ? 'POST' : 'GET';
        }

        if (class_exists('Psr\Log\LogLevel')) {
            $log_level_debug = LogLevel::DEBUG;
            $log_level_warning = LogLevel::WARNING;
        }
        else {
            $log_level_debug = 'debug';
            $log_level_warning = 'warning';
        }

        $url = sprintf('%s/%s', $this->host, $path);
        $signature = hash_hmac('md5', $method . $url . ($data ? $data : ''), $this->secret);

        $headers = [
            'Accept: application/json',
            sprintf('Authorization: %s', $this->key),
            sprintf('X-HMAC-Signature: %s', $signature)
        ];
        if (!empty($this->partner)) {
            $headers[] = sprintf('X-Partner-ID: %s', $this->partner);
        }
        if (in_array($method, array('POST', 'PUT'))) {
            $headers[] = 'Content-Type: application/json';
        }

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        if (in_array($method, array('POST', 'PUT'))) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        $response = curl_exec($ch);

        $this->log($log_level_debug, 'request: url={url} headers={headers} data={data}', [
            'url' => $url,
            'headers' => $this->maskHeaders($headers),
            'data' => $data
{'fraction_non_alphanumeric': 0.1498267898383372, 'fraction_numerical': 0.0002886836027713626, 'mean_word_length': 1.9776536312849162, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, 'https://': 2, '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': '1723227', 'n_tokens_mistral': 2180, 'n_tokens_neox': 2125, 'n_words': 492}