<commit_before><?php
/**
 * @copyright Copyright (c) 2014 EuroMD LTD
 */

namespace EuroMD\yandexMetrika;

use yii\base\Component;
use yii\base\InvalidParamException;
use yii\helpers\ArrayHelper;
use yii\validators\StringValidator;

/**
 * @package EuroMD\yandexMetrika
 * @author Borales
 *
 * @property OAuth2 $apiClient
 */
class Client extends Component
{
	const CODE_UNAUTHORIZED = 401;

	const YANDEX_OT_MIN = 500;
	const YANDEX_OT_MAX = 32000;

	/** @var string */
	public $cacheComponent = 'cache';
	/** @var string */
	public $clientID;
	/** @var string */
	public $clientSecret;
	/** @var OAuth2 */
	private $_apiClient;

	public function init()
	{
		parent::init();
		if (!$this->clientID || !$this->clientSecret)
			throw new InvalidParamException;
	}

	/**
	 * API Client
	 * @return OAuth2
	 */
	public function getApiClient()
	{
		if (!$this->_apiClient) {
			$this->_apiClient = new OAuth2([
				'clientId' => $this->clientID,
				'clientSecret' => $this->clientSecret,
				'cacheComponent' => $this->cacheComponent,
			]);
		}
		return $this->_apiClient;
	}

	/**
	 * Add Original Text
	 * @param string $text
	 * @param int $yandexSiteID
	 * @throws InvalidParamException
	 * @return string|bool
	 */
	public function addOriginalText($text, $yandexSiteID)
	{
		$validator = new StringValidator([
			'min' => self::YANDEX_OT_MIN,
			'max' => self::YANDEX_OT_MAX,
			'enableClientValidation' => false,
		]);

		if (!$validator->validate($text, $error)) {
			throw new InvalidParamException($error);
		}

		$text = urlencode(htmlspecialchars($text));
		$text = "<original-text><content>{$text}</content></original-text>";

		$response = $this->apiClient->api("hosts/$yandexSiteID/original-texts/", "POST", $text);
		return ArrayHelper::keyExists('id', $response) && ArrayHelper::keyExists('link', $response);
	}

	/**
	 * @param string $code
	 */
	public function setCode($code)
	{
		$this->apiClient->fetchAccessToken($code);
	}

	/**
	 * Auth header
	 * @return array
	 * @throws InvalidParamException
	 */
	protected function getAuthHeader()
	{
		if ($this->apiClient->accessToken && $this->apiClient->accessToken->isValid) {
			return 'Authorization: OAuth ' . $this->apiClient->accessToken->token;
		}
		throw new InvalidParamException('NOT VALID ACCESS TOKEN', self::CODE_UNAUTHORIZED);
	}<commit_msg>Handling "Original Text" daily limit<commit_after><?php
/**
 * @copyright Copyright (c) 2014 EuroMD LTD
 */

namespace EuroMD\yandexMetrika;

use yii\base\Component;
use yii\base\InvalidParamException;
use yii\caching\Cache;
use yii\helpers\ArrayHelper;
use yii\validators\StringValidator;

/**
 * @package EuroMD\yandexMetrika
 * @author Borales
 *
 * @property OAuth2 $apiClient
 */
class Client extends Component
{
	const CODE_UNAUTHORIZED = 401;

	/** @var int Minimum text lenght for Original Text */
	const YANDEX_OT_MIN = 500;
	/** @var int Maximum text lenght for Original Text */
	const YANDEX_OT_MAX = 32000;
	/** @var int Daily limit for Original Text */
	const YANDEX_OT_DAILY_LIMIT = 100;

	/** @var string */
	public $cacheComponent = 'cache';
	/** @var string */
	public $clientID;
	/** @var string */
	public $clientSecret;
	/** @var OAuth2 */
	private $_apiClient;

	public function init()
	{
		parent::init();
		if (!$this->clientID || !$this->clientSecret)
			throw new InvalidParamException;
	}

	/**
	 * API Client
	 * @return OAuth2
	 */
	public function getApiClient()
	{
		if (!$this->_apiClient) {
			$this->_apiClient = new OAuth2([
				'clientId' => $this->clientID,
				'clientSecret' => $this->clientSecret,
				'cacheComponent' => $this->cacheComponent,
			]);
		}
		return $this->_apiClient;
	}

	/**
	 * Add Original Text
	 * @param string $text
	 * @param int $yandexSiteID
	 * @throws InvalidParamException
	 * @return string|bool
	 */
	public function addOriginalText($text, $yandexSiteID)
	{
		/** @var Cache $cache */
		$cache = \Yii::$app->get($this->cacheComponent);
		$cacheKey = "Yandex_OT_sent:" . $this->clientID . "_" . $this->clientSecret;

		$sent = $cache->get($cacheKey);
		if($sent === false) {
			$sent = 0;
		}

		if($sent >= self::YANDEX_OT_DAILY_LIMIT) {
			throw new InvalidParamException("Daily limit exceeded!");
		}

		$validator = new StringValidator([
			'min' => self::YANDEX_OT_MIN,
			'max' => self::YANDEX_OT_MAX,
			'enableClientValidation' => false,
		]);

		if (!$validator->validate($text, $error)) {
			throw new InvalidParamException($error);
		}

		$text = urlencode(htmlspecialchars($text));
		$text = "<original-text><content>{$text}</content></original-text>";

		$response = $this->apiClient->api("hosts/$yandexSiteID/original-texts/", "POST", $text);

		$success = ArrayHelper::keyExists('id', $response) && ArrayHelper::keyExists('link', $response);
		if($success) {
			$sent++;
			$cache->set($cacheKey, $sent, 60 * 60 * 24);
		}
		return $success;
	}

	/**
	 * @param string $code
	 */
	public function setCode($code)
	{
		$this->apiClient->fetchAccessToken($code);
	}

	/**
	 * Auth header
	 * @return array
	 * @throws InvalidParamException
	 */
	protected function getAuthHeader()
	{
		if ($this->apiClient->accessToken && $this->apiClient->accessToken->isValid) {
			return 'Authorization: OAuth ' . $this->apiClient->accessToken->token;
		}
		throw new InvalidParamException('NOT VALID ACCESS TOKEN', self::CODE_UNAUTHORIZED);
	}
{'fraction_non_alphanumeric': 0.16291713535808025, 'fraction_numerical': 0.008998875140607425, 'mean_word_length': 4.1298076923076925, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 72, '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': '4422459', 'n_tokens_mistral': 1974, 'n_tokens_neox': 1788, 'n_words': 381}