<commit_before><?php

declare(strict_types=1);

namespace RateLimit;

use RateLimit\Exception\CannotUseRateLimiter;
use RateLimit\Exception\LimitExceeded;

final class ApcuRateLimiter implements RateLimiter, SilentRateLimiter
{
    /** @var string */
    private $keyPrefix;

    public function __construct(string $keyPrefix = '')
    {
        if (!\extension_loaded('apcu') || \ini_get('apc.enabled') === '0') {
            throw new CannotUseRateLimiter('APCu extension is not loaded or not enabled.');
        }

        if (\ini_get('apc.use_request_time') === '1') {
            throw new CannotUseRateLimiter('APCu ini configuration "apc.use_request_time" should be set to "0".');
        }

        $this->keyPrefix = $keyPrefix;
    }

    public function limit(string $identifier, Rate $rate): void
    {
        $limitKey = $this->limitKey($identifier, $rate->getInterval());

        $current = $this->getCurrent($limitKey);
        if ($current >= $rate->getOperations()) {
            throw LimitExceeded::for($identifier, $rate);
        }

        $this->updateCounter($limitKey, $rate->getInterval());
    }

    public function limitSilently(string $identifier, Rate $rate): Status
    {
        $interval = $rate->getInterval();
        $limitKey = $this->limitKey($identifier, $interval);
        $timeKey = $this->timeKey($identifier, $interval);

        $current = $this->getCurrent($limitKey);
        if ($current <= $rate->getOperations()) {
            $current = $this->updateCounterAndTime($limitKey, $timeKey, $interval);
        }

        return Status::from(
            \sprintf('%s%s', $this->keyPrefix, $identifier),
            $current,
            $rate->getOperations(),
            \time() + \max(0, $interval - $this->getElapsedTime($timeKey))
        );
    }

    private function limitKey(string $identifier, int $interval): string
    {
        return "{$this->keyPrefix}{$identifier}:$interval";
    }

    private function timeKey(string $identifier, int $interval): string
    {
        return "{$this->keyPrefix}{$identifier}:$interval:time";
    }

    private function getCurrent(string $limitKey): int<commit_msg>no prefix on status identifier<commit_after><?php

declare(strict_types=1);

namespace RateLimit;

use RateLimit\Exception\CannotUseRateLimiter;
use RateLimit\Exception\LimitExceeded;

final class ApcuRateLimiter implements RateLimiter, SilentRateLimiter
{
    /** @var string */
    private $keyPrefix;

    public function __construct(string $keyPrefix = '')
    {
        if (!\extension_loaded('apcu') || \ini_get('apc.enabled') === '0') {
            throw new CannotUseRateLimiter('APCu extension is not loaded or not enabled.');
        }

        if (\ini_get('apc.use_request_time') === '1') {
            throw new CannotUseRateLimiter('APCu ini configuration "apc.use_request_time" should be set to "0".');
        }

        $this->keyPrefix = $keyPrefix;
    }

    public function limit(string $identifier, Rate $rate): void
    {
        $limitKey = $this->limitKey($identifier, $rate->getInterval());

        $current = $this->getCurrent($limitKey);
        if ($current >= $rate->getOperations()) {
            throw LimitExceeded::for($identifier, $rate);
        }

        $this->updateCounter($limitKey, $rate->getInterval());
    }

    public function limitSilently(string $identifier, Rate $rate): Status
    {
        $interval = $rate->getInterval();
        $limitKey = $this->limitKey($identifier, $interval);
        $timeKey = $this->timeKey($identifier, $interval);

        $current = $this->getCurrent($limitKey);
        if ($current <= $rate->getOperations()) {
            $current = $this->updateCounterAndTime($limitKey, $timeKey, $interval);
        }

        return Status::from(
            $identifier,
            $current,
            $rate->getOperations(),
            \time() + \max(0, $interval - $this->getElapsedTime($timeKey))
        );
    }

    private function limitKey(string $identifier, int $interval): string
    {
        return \sprintf('%s%s:%d', $this->keyPrefix, $identifier, $interval);
    }

    private function timeKey(string $identifier, int $interval): string
    {
        return \sprintf('%s%s:%d:time', $this->keyPrefix, $identifier, $interval);
    }

    private function getCurrent(string $limitKey): int
{'fraction_non_alphanumeric': 0.14755228683061367, 'fraction_numerical': 0.0022983222247759134, 'mean_word_length': 2.9384615384615387, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 40, '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': '7522460', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1233, 'n_words': 267}