<commit_before><?php
/**
 * Created by PhpStorm.
 * User: dany
 * Date: 5/20/16
 * Time: 2:51 AM
 */

namespace Prezto\RateLimit;

use Flintstone\Flintstone;


class RateLimitMiddleware
{
    protected $handle = null;

    protected $maxRequests = 60;

    protected $seconds = 10;

    public function __construct($maxRequests = 60, $seconds = 10)
    {
        $this->handle = new Flintstone($_SERVER["REMOTE_ADDR"], array('dir' => sys_get_temp_dir() . '/'));
        if (!is_int($maxRequests))
            throw new \InvalidArgumentException;

        if (!is_int($maxRequests))
            throw new \InvalidArgumentException;

        $this->maxRequests = $maxRequests;
        $this->seconds = $seconds;
    }

    private function increment()
    {
        $this->handle->set(microtime(), time() + $this->seconds);
    }

    private function purgeExpired()
    {
        foreach ($this->handle->getKeys() as $key) {
            if ($this->handle->get($key) > time())
                $this->handle->delete($key);
        }
    }

    public function __invoke($request, $response, $next)
    {
        $this->purgeExpired();
        $this->increment();

        if (count($this->handle->getKeys()) > $this->maxRequests)
            $response = $response->withStatus(429);

        $response = $next($request, $response);

        return $response;
    }
}<commit_msg>Update RateLimitMiddleware.php<commit_after><?php
/**
 * Created by PhpStorm.
 * User: dany
 * Date: 5/20/16
 * Time: 2:51 AM
 */

namespace Prezto\RateLimit;

use Flintstone\Flintstone;


class RateLimitMiddleware
{
    /**
     * @var null|string
     */
    public $host = null;

    /**
     * @var null|string
     */
    public $port = null;

    /**
     * @var null
     */
    public $pass = null;

    protected $handle = null;

    protected $maxRequests = 1;

    protected $seconds = 10;

    public function __construct($host = 'localhost', $port = '6379', $pass = null)
    {
        $this->host = $host;
        $this->port = $port;
        $this->pass = $pass;

        $this->handle = new \TinyRedisClient(sprintf("%s:%s", $this->host, $this->port));

        if ($this->pass !== null)
            $this->auth();
    }

    public function setRequestsPerSecond($maxRequests, $seconds)
    {
        if (!is_int($maxRequests))
            throw new \InvalidArgumentException;

        if (!is_int($maxRequests))
            throw new \InvalidArgumentException;

        $this->maxRequests = $maxRequests;
        $this->seconds = $seconds;
    }

    /**
     *
     */
    public function auth()
    {
        $this->handle->auth($this->pass);
    }

    public function __invoke($request, $response, $next)
    {
        if (count($this->handle->keys(sprintf("%s*", str_replace('.', '', $_SERVER['REMOTE_ADDR'])))) >= $this->maxRequests)
            $response = $response->withStatus(429);
        else {
            $key = sprintf("%s%s", str_replace('.', '', $_SERVER['REMOTE_ADDR']), mt_rand());
            $this->handle->set($key, time());
            $this->handle->expire($key, $this->seconds);
        }

        $response = $next($request, $response);

        return $response;
    }
}

{'fraction_non_alphanumeric': 0.16530740276035133, 'fraction_numerical': 0.011606022584692597, 'mean_word_length': 2.3184183142559833, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, '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': '4524891', 'n_tokens_mistral': 1023, 'n_tokens_neox': 973, 'n_words': 199}